2016-05-04 87 views
0

我在這裏要做的是生成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類

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

} 

回答

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

解決方案是初始化每個數組中的元素分別高達它們的長度,或者如果你意兩個陣列是相同的長度則可以選擇一個隨機長度你分配陣列之前。後者的解決將如圖6-8所示:

int len = 5 + randomNumbers.nextInt(5); 
ovals = new MyOval[ len ]; 
rectangles = new MyRectangle [ len ]; 
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);   
} 

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