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

时间:2014-10-08 00:42:37

标签: java swing draw

我试图通过每次点击绘制圆形对象然后将每个圆形对象存储到一个Arraylist中,我不知道为什么我的程序不起作用!如果我删除了arraylist和创建新圆对象的行,该程序将起作用。如何让我的程序将所有电路对象存储到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

1 个答案:

答案 0 :(得分:2)

不要在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 ...有关详细信息,请查看Working with Geometry ...