简体   繁体   English

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

[英]Drawing and storing objects by clicking the mouse button

I am trying to draw circle objects with each click and then store every circle object into an Arraylist, I don't know why my program is not working! 我试图每次单击都绘制圆形对象,然后将每个圆形对象存储到Arraylist中,但我不知道为什么我的程序无法正常工作! If I removed the arraylist and the line that create a new circle object, the program will work. 如果删除了arraylist和创建新圆对象的线,则该程序将起作用。 How would I make my program store all circuit objects into an 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

Don't add the new shape inside the paintComponent method, paintComponent can be called for any number of reasons, many of which you don't control, instead, create it when the mouseClicked event is triggered... 不要在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();
}

And then in your paintComponent , loop through the ArrayList and paint the circles... 然后在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());
    }
}

Now, you're going to have to modify you Circle class to provide getters which the CircleObj can use in order to actually paint the circles... 现在,您将必须修改Circle类,以提供CircleObj可以用来实际绘制圆圈的吸气剂...

Alternatively, you could make use of the Shape s API provided within Java...Have a look at Working with Geometry for more details... 或者,您可以使用Java中提供的Shape s API ...有关更多信息,请参阅使用几何 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM