我在这里要做的是生成5个随机的椭圆和矩形。如果我删除
for(MyOval oval : ovals){
oval.draw(g);
}
从第一类开始,它将显示5个随机矩形。如果我删除
for(MyRectangle rectangle : rectangles){
rectangle.draw(g);
}
它将显示5个随机的椭圆。如果我什么都不删除,它就不起作用。我做错了什么?
绘图面板类
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class DrawPanel extends JPanel{
private Random randomNumbers = new Random();
private MyOval[] ovals;
private MyRectangle[] rectangles;
public DrawPanel(){
setBackground(Color.WHITE);
ovals = new MyOval[ 5 + randomNumbers.nextInt(5)];
rectangles = new MyRectangle [ 5 + randomNumbers.nextInt(5)];
for (int count = 0; count <ovals.length; count++){
int x1 = randomNumbers.nextInt(300);
int y1 = randomNumbers.nextInt(300);
int x2 = randomNumbers.nextInt(300);
int y2 = randomNumbers.nextInt(300);
Color color = new Color (randomNumbers.nextInt(256), randomNumbers.nextInt(256), randomNumbers.nextInt(256));
ovals[count] = new MyOval(x1, y1, x2, y2, color);
rectangles[count] = new MyRectangle(x1, y1, x2, y2, color);
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
for(MyRectangle rectangle : rectangles){
rectangle.draw(g);
}
for(MyOval oval : ovals){
oval.draw(g);
}
}
}
主要类别
import javax.swing.JFrame;
public class TestDraw {
public static void main(String[] args) {
DrawPanel panel = new DrawPanel();
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(300,300);
application.setVisible(true);
}
}
MyOval类
import java.awt.Color;
import java.awt.Graphics;
public class MyOval {
private int x1;
private int y1;
private int x2;
private int y2;
private Color myColor;
public MyOval(int x1, int y1, int x2, int y2, Color color){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
myColor = color;
}
public void draw(Graphics g){
g.setColor(myColor);
g.drawOval(x1, y1, x2, y2);
}
}
MyRectangle类
import java.awt.Color;
import java.awt.Graphics;
public class MyRectangle {
private int x1;
private int y1;
private int x2;
private int y2;
private Color myColor;
public MyRectangle(int x1, int y1, int x2, int y2, Color color){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
myColor = color;
}
public void draw(Graphics g){
g.setColor(myColor);
g.drawRect(x1, y1, x2, y2);
}
}
问题是,您分配了两个具有随机长度的数组,但随后使用第一个数组的长度来遍历这两个数组。报价:
ovals = new MyOval[ 5 + randomNumbers.nextInt(5)];
rectangles = new MyRectangle [ 5 + randomNumbers.nextInt(5)];
for (int count = 0; count <ovals.length; count++){
int x1 = randomNumbers.nextInt(300);
int y1 = randomNumbers.nextInt(300);
int x2 = randomNumbers.nextInt(300);
int y2 = randomNumbers.nextInt(300);
Color color = new Color (randomNumbers.nextInt(256), randomNumbers.nextInt(256), randomNumbers.nextInt(256));
ovals[count] = new MyOval(x1, y1, x2, y2, color);
rectangles[count] = new MyRectangle(x1, y1, x2, y2, color);
}
解决方案是分别初始化每个数组中的元素,直到它们的长度,或者如果您希望两个数组的长度相同,则可以在分配数组之前选择一个随机长度。后期修复如下所示:
int len = 5 + randomNumbers.nextInt(5);
ovals = new MyOval[ len ];
rectangles = new MyRectangle [ len ];
按照Farrukh所说的,我将得到使用相同变量的椭圆和矩形,这意味着它们将在彼此内部。谢谢你的帮助,我找到了另一种方法来解决我的问题,现在它生成了独立的椭圆和矩形
ovals = new MyOval[ 5 + randomNumbers.nextInt(5)];
rectangles = new MyRectangle [ 5 + randomNumbers.nextInt(5)];
for (int count = 0; count <ovals.length; count++){
int x1 = randomNumbers.nextInt(300);
int y1 = randomNumbers.nextInt(300);
int x2 = randomNumbers.nextInt(300);
int y2 = randomNumbers.nextInt(300);
Color color = new Color (randomNumbers.nextInt(256), randomNumbers.nextInt(256), randomNumbers.nextInt(256));
ovals[count] = new MyOval(x1, y1, x2, y2, color);
}
for (int count = 0; count <rectangles.length; count++){
int x1 = randomNumbers.nextInt(300);
int y1 = randomNumbers.nextInt(300);
int x2 = randomNumbers.nextInt(300);
int y2 = randomNumbers.nextInt(300);
Color color = new Color (randomNumbers.nextInt(256), randomNumbers.nextInt(256), randomNumbers.nextInt(256));
rectangles[count] = new MyRectangle(x1, y1, x2, y2, color);
}