2016-05-04 7 views
0

Was ich hier zu tun versuche, ist 5 zufällige Ovale und Rechtecke zu generieren. Wenn ich aus der ersten KlasseKann nicht: Ovale und Rechtecke zusammen zeichnen, nur einzeln

entferne, werden 5 zufällige Rechtecke angezeigt. Wenn ich entferne

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

wird es 5 zufällige Ovale angezeigt. Wenn ich nichts entferne, funktioniert es nicht. Was mache ich falsch?

DrawPanel Klasse

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

Hauptklasse

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 Klasse

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 Klasse

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

} 

Antwort

0

Das Problem ist, dass man zwei Arrays mit zufälliger Länge wird Zuteilen, aber dann mit der Länge des ersten Feldes durch beiden Arrays iterieren. Zitat:

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

Lösung ist entweder die Elemente in jedem separat Array zu initialisieren bis zu ihrer Länge, oder wenn Sie beide Arrays bestimmen die gleiche Länge sein, könnten Sie eine zufällige Länge wählen, bevor Sie die Arrays zuweisen. Letztere fix würde wie unten dargestellt:

int len = 5 + randomNumbers.nextInt(5); 
ovals = new MyOval[ len ]; 
rectangles = new MyRectangle [ len ]; 
0

tun, was Farrukh sagte, werde ich Ovalen und Rechtecke bekommen, die die gleichen Variablen verwenden, was bedeutet, dass sie in jedem anderen sein wird. Danke für Ihre Hilfe, ich fand einen anderen Weg, um mein Problem zu lösen, jetzt gerenates unabhängige Ovale und Rechtecke

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