用对象重新绘制我的面板

时间:2014-10-01 04:24:32

标签: java swing repaint

我创建了一个程序,应该在每次点击时绘制一个圆圈(随机大小和随机颜色),每个圆圈都将成为一个对象。我不知道发生了什么,但我的代码不起作用,我很确定我差不多了。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class Circle
{

    private JFrame frame;
    CircleObj circleObj;
    Random  rand;
    int rColor;
    int gColor;
    int bColor;
    int radius;



    public static void main (String [] arg)
        {

        frame = new JFrame("Circles");
        frame.setBounds(200, 100, 600, 480);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        JPanel top = new JPanel();
        rand = new Random();

        top.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent e)
                {
                int xstart = e.getX();
                int ystart = e.getY();
                rColor = rand.nextInt(256);
                gColor = rand.nextInt(256);
                bColor = rand.nextInt(256);
                radius = rand.nextInt(20);
                circleObj = new CircleObj(xstart, ystart, rColor, gColor, bColor, radius);

                repaint();

                }
        });
                frame.add(top, BorderLayout.CENTER);
                frame.setVisible(true);

        }


}

这是我的CircleObj类

import javax.swing.*;
import java.awt.*;

public class CircleObj extends JPanel

{
    private int xVal;
    private int yVal;
    private int red;
    private int green;
    private int blue;
    private int circleRadius;


    public CircleObj (int x, int y, int r, int g, int b, int rad)
        {
        xVal = x;
        yVal = y;
        red = r;
        green= g;
        blue = b;
        circleRadius = rad;

        }



        public void paintComponent (Graphics g) {
        super.paintComponent(g);
        g.setColor(new Color(red, green, blue));
        g.fillOval(xVal,yVal,circleRadius,circleRadius);
        }

}

2 个答案:

答案 0 :(得分:2)

您将MouseListener添加到顶部JPanel,但是何时将顶部JPanel添加到顶级窗口,GUI? ---在 MouseListener 中!所以这将永远不会起作用,因为顶级JPanel最初不是GUI的一部分,只有在触发添加到它的MouseListener时才会添加。 MouseListener仅适用于可见且在GUI上的组件。

接下来,您将创建一个CircleObj对象,但不会在任何JComponent或JPanel的paiintComponent方法中使用它绘制。

  

我很确定我几乎就在那里。

所以不,不完全。你需要重新开始并且:

  • 创建一个具有相当paintComopnent方法覆盖的JPanel,
  • 将此JPanel从头开始添加到GUI 的JFrame
  • ArrayList<CircleObj>添加到此图纸JPanel
  • 更改您的CircleObj类,使其不扩展JPanel,但确实有一个public void draw(Grpahics g)方法可以绘制它。
  • 将MouseListener添加到控制此绘图的JPanel。在MouseListener中,将新的CircleObj添加到ArrayList并调用repaint()以获取绘图JPanel。
  • 在JPanel的paintComponent覆盖中,遍历ArrayList<CircleObj>调用每个对象的draw(g)方法。
  • 阅读Swing graphics tutorials

答案 1 :(得分:2)

这是第二种方法......我如何能够调整它将每个圆圈存储在一个对象(对象的Arraylist)中?

public class Circle {
        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 Circle

CircleObj Class

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;

    public CircleObj () {
    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);
        System.out.println("xstart : " + xStart + ", ystart : " + yStart + ", rColor : " + rColor + ", gColor = " +
                gColor + ", bColor: " + bColor + ", radius: " + radius);

        repaint();
        }
    }); // end addMouseListener
    }

    public void paintComponent (Graphics g) {
    super.paintComponent(g);
    g.setColor(new Color(rColor, gColor, bColor));
    g.fillOval(xStart, yStart, radius, radius);
    }

}