【问题标题】:Java Applet Polygon arrayJava Applet 多边形数组
【发布时间】:2012-11-02 01:09:38
【问题描述】:

我有一个功能程序,它通过每次鼠标点击来收集坐标,然后使用这些坐标绘制一个多边形。我添加了一个功能,这样当您完成绘制多边形并填充它时,您可以擦除屏幕并重新开始使用新形状。我遇到的问题是想办法重置坐标值。

我现在拥有的是在我的 actionPerformed 方法中我将我的两个数组(XCoordinates 和 YCoordinates)清零。现在用户可以从新坐标开始,但现在第一个坐标是 (0,0)。每次我画一个形状都是从左上角开始的哈哈。

我想将数组的值设置为我最初初始化两个数组时的值。我尝试使用 actionPerformed 重新初始化数组,但没有成功,而且我确信这是非常糟糕的编程习惯。

有什么想法吗?

【问题讨论】:

  • 您可能希望显示一些代码以配合您的描述。否则,我们怎么知道你做错了什么?
  • 一个Point 类型的数组,比并行x-y 数组更面向对象。
  • 我自己我会使用ArrayList<Point>,然后在需要时调用clear() 来重置它。
  • @Hovercraft & M.M. 的建议在概念上很有吸引力;它们可以与Polygon 组合,以利用后者对Shape 接口的实现。

标签: java japplet


【解决方案1】:

直接操作Polygon 坐标数组字段很诱人,但您只能通过公共API 进行操作。特别是看看这些方法:

  • invalidate(),“应在对坐标进行任何直接操作后调用。”

  • reset(),使多边形为空。

  • addPoint(),保持内部一致状态。

有一个相关的例子here

【讨论】:

    【解决方案2】:

    您不提供任何代码会使其变得困难,但这里有两个想法......

    使用多边形

    这基本上使用Polygon 并不断向其添加点,直到您按下回车键...

    public class PolyPainter {
    
        public static void main(String[] args) {
            new PolyPainter();
        }
    
        public PolyPainter() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new PolyPane());
                    frame.setSize(400, 400);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        protected class PolyPane extends JPanel {
    
            private Polygon poly;
            private Point lastPoint;
    
            public PolyPane() {
    
                poly = new Polygon();
    
                InputMap im = getInputMap();
                im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "clear");
                ActionMap am = getActionMap();
                am.put("clear", new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        poly = new Polygon();
                        repaint();
                    }
                });
    
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        lastPoint = e.getPoint();
                        poly.addPoint(e.getX(), e.getY());
                        repaint();
                    }
                });
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g); 
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.draw(poly);
                if (lastPoint != null) {
                    g2d.setColor(Color.RED);
                    g2d.fillOval(lastPoint.x - 5, lastPoint.y - 5, 10, 10);
                }
                g2d.dispose();
            }
        }
    }
    

    使用点列表

    这基本上使用了一个点列表

    public class PolyPainter1 {
    
        public static void main(String[] args) {
            new PolyPainter1();
        }
    
        public PolyPainter1() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new PolyPane());
                    frame.setSize(400, 400);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        protected class PolyPane extends JPanel {
    
            private List<Point> poly;
            private Point lastPoint;
    
            public PolyPane() {
    
                poly = new ArrayList<Point>(25);
    
                InputMap im = getInputMap();
                im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "clear");
                ActionMap am = getActionMap();
                am.put("clear", new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        poly.clear();
                        repaint();
                    }
                });
    
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        lastPoint = e.getPoint();
                        poly.add(lastPoint);
                        repaint();
                    }
                });
    
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g); 
                Graphics2D g2d = (Graphics2D) g.create();
                Polygon pg = new Polygon();
                for (Point p : poly) {
                    pg.addPoint(p.x, p.y);
                }
                g2d.draw(pg);
                if (lastPoint != null) {
                    g2d.setColor(Color.RED);
                    g2d.fillOval(lastPoint.x - 5, lastPoint.y - 5, 10, 10);
                }
                g2d.dispose();
            }
        }
    }
    

    就个人而言,第一个更有效,因为它不需要在每次重绘时都构造一个新的Polygon 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 2014-09-22
      • 1970-01-01
      • 2021-06-20
      相关资源
      最近更新 更多