通过单击鼠标按钮绘制和存储对象



我试图每次点击都绘制圆形对象,然后将每个圆形对象存储到Arraylist中,我不知道为什么我的程序不工作!如果我删除了创建新圆对象的数组列表和线,程序就会工作。如何使程序将所有电路对象存储到数组列表中?

 import javax.swing.JPanel;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.ArrayList;
    import java.util.Random;
    public class CircleObj extends JPanel {
        private int rColor;
        private int gColor;
        private int bColor;
        private int radius;
        private Random rand = new Random();
        private int xStart;
        private int yStart;
        ArrayList <Circle> xxx ;
        public CircleObj () {
        xxx =  new ArrayList<Circle>();
        addMouseListener(new MouseAdapter() {
            public void mouseClicked (MouseEvent e) {
            xStart = e.getX();
            yStart = e.getY();
            rColor = rand.nextInt(256);
            gColor = rand.nextInt(256);
            bColor = rand.nextInt(256);
            radius = rand.nextInt(20);

            repaint();
            }
        }); // end addMouseListener
        }
        public void paintComponent (Graphics g) {
        super.paintComponent(g);
        g.setColor(new Color(rColor, gColor, bColor));
        g.fillOval(xStart, yStart, radius, radius);
        xxx.add(new Circle());
        }
        private class Circle {
            private int x;
            private int y;
            private int r;
            private int rcol;
            private int gcol;
            private int bcol;
            public Circle()
                {
                x=xStart;
                y=yStart;
                r=radius;
                rcol= rColor;
                gcol= gColor;
                bcol= bColor;
                }


        }
    }

======

import javax.swing.JFrame;
import java.awt.BorderLayout;
public class HW3 {
    public static void main (String[] arg) {
    JFrame frame = new JFrame("Circles");
    CircleObj canvas = new CircleObj();
    frame.add(canvas, BorderLayout.CENTER);
    frame.setBounds(250, 98, 600, 480);
    //frame.setLayout(new BorderLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    } // end main
} //end HW3

不要在paintComponent方法中添加新形状,paintComponent可以因多种原因调用,其中许多原因您无法控制,而是在触发mouseClicked事件时创建它。。。

public void mouseClicked (MouseEvent e) {
    xStart = e.getX();
    yStart = e.getY();
    rColor = rand.nextInt(256);
    gColor = rand.nextInt(256);
    bColor = rand.nextInt(256);
    radius = rand.nextInt(20);
    xxx.add(new Circle(xStart, yStart, new Color(rColor, gColor, bColor), radius));
    repaint();
}

然后在你的paintComponent中,循环通过ArrayList并画出圆圈。。。

public void paintComponent (Graphics g) {
    super.paintComponent(g);
    for (Circle c : xxx) {
        g.setColor(c.getColor());
        g.fillOval(c.getX(), c.getY(), c.getRadius(), c.getRadius());
    }
}

现在,您必须修改Circle类,以提供CircleObj可以使用的getter,以便实际绘制圆。。。

或者,您可以使用Java中提供的Shape的API。。。有关更多详细信息,请参阅"使用几何体"。。。