我创建了一个程序,应该在每次点击时绘制一个圆圈(随机大小和随机颜色),每个圆圈都将成为一个对象。我不知道发生了什么,但我的代码不起作用,我很确定我差不多了。
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);
}
}
答案 0 :(得分:2)
您将MouseListener添加到顶部JPanel,但是何时将顶部JPanel添加到顶级窗口,GUI? ---在 MouseListener 中!所以这将永远不会起作用,因为顶级JPanel最初不是GUI的一部分,只有在触发添加到它的MouseListener时才会添加。 MouseListener仅适用于可见且在GUI上的组件。
接下来,您将创建一个CircleObj对象,但不会在任何JComponent或JPanel的paiintComponent方法中使用它绘制。
我很确定我几乎就在那里。
所以不,不完全。你需要重新开始并且:
ArrayList<CircleObj>
添加到此图纸JPanel public void draw(Grpahics g)
方法可以绘制它。repaint()
以获取绘图JPanel。ArrayList<CircleObj>
调用每个对象的draw(g)
方法。答案 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);
}
}