不能:一起绘制椭圆和矩形,只能单独绘制

Can't: draw Ovals and Rectangles together, only individually

我在这里要做的是随机生成 5 个椭圆和矩形。如果我删除

for(MyOval oval : ovals){
                    oval.draw(g);
 }

从第一个class开始,它将随机显示5个矩形。如果我删除

for(MyRectangle rectangle : rectangles){       
                rectangle.draw(g);                         
            }  

它将显示 5 个随机椭圆。如果我什么都不删除,它就不起作用。我做错了什么?

绘图面板Class

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);                              
        }                                                      
    }                                                
}     

主要Class

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 Class

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);
    }
}

我的矩形Class

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);
}