Java小程序多边形数组

Java小程序多边形数组,java,japplet,Java,Japplet,我有一个功能程序,每次鼠标点击都会收集坐标,然后使用这些坐标绘制多边形。我添加了一个功能,这样当你画完多边形并填充它时,你就可以擦除屏幕并重新开始一个新的形状。我遇到的问题是想办法重置坐标值 现在,在actionPerformed方法中,我将两个数组(XCoordinates和YCoordinates)归零。现在用户可以使用新坐标重新开始,但现在第一个坐标是(0,0)。每次我画一个形状,它都从左上角开始,哈哈 我想将数组的值设置为初始化两个数组时的值。我试图用ActionExecuted重新初始

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

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

我想将数组的值设置为初始化两个数组时的值。我试图用ActionExecuted重新初始化数组,但没有成功,我确信这是非常糟糕的编程实践


有什么想法吗?

直接操作
多边形
坐标数组字段很有诱惑力,但您只能通过公共API来操作。特别是,看看这些方法:

  • invalidate()
    ,它“应该在直接操纵坐标后调用。”

  • reset()
    ,使多边形为空

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


这里有一个相关的例子。

你没有提供任何代码,这让事情变得很困难,但是这里有两个关于这个想法的例子

使用多边形

这基本上使用了一个
多边形
,并不断向其添加点,直到按enter键为止

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();
        }
    }
}
公共类多画师1{
公共静态void main(字符串[]args){
新的PolyPainter1();
}
公共合成颜料1(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}捕获(ClassNotFoundException ex){
}catch(实例化异常){
}捕获(非法访问例外){
}捕获(无支持的LookandFeelexception ex){
}
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(新的聚苯胺());
框架。设置尺寸(400400);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
受保护的类PolyPane扩展了JPanel{
私人上市公司;
私人点;
公共聚芳醚(){
poly=新阵列列表(25);
InputMap im=getInputMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),“clear”);
ActionMap am=getActionMap();
am.put(“清除”,新抽象动作(){
@凌驾
已执行的公共无效操作(操作事件e){
poly.clear();
重新油漆();
}
});
addMouseListener(新的MouseAdapter(){
@凌驾
公共无效鼠标按下(MouseEvent e){
lastPoint=e.getPoint();
poly.add(lastPoint);
重新油漆();
}
});
}
@凌驾
受保护组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g.create();
多边形pg=新多边形();
对于(点p:多边形){
pg.addPoint(p.x,p.y);
}
g2d.绘图(pg);
if(lastPoint!=null){
g2d.setColor(Color.RED);
g2d.fillOval(lastPoint.x-5,lastPoint.y-5,10,10);
}
g2d.dispose();
}
}
}

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

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