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

时间:2016-05-04 19:57:55

标签: java graphics jpanel draw

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

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

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

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

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

DrawPanel类

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

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

2 个答案:

答案 0 :(得分:0)

问题是你正在分配两个随机长度的数组,但是然后使用第一个数组的长度迭代两个数组。 引用:

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 ];

答案 1 :(得分:0)

通过做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);
}