2017-04-21 84 views
1

我有一個簡單的例子,應該創建一組綠球,而只是創建一個。我想創建一個ArrayList來保存球,但有些錯誤。請幫忙。用戶界面不是創建形狀

import javax.swing.JPanel; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.geom.*; 
import java.util.ArrayList; 
import java.util.Random; 
import javax.security.auth.x500.X500Principal; 
import javax.swing.*; 

public class MyBall extends JPanel{ 

    Random rand = new Random(); 
    int xr = rand.nextInt(400); 
    int yr = rand.nextInt(400); 
    int size = 10 ; 
    int x = xr ; 
    int y = yr ; 

    Ellipse2D.Double ball = new Ellipse2D.Double(0, 0, 30, 30); 
    ArrayList<Bubbles> balls = new ArrayList<Bubbles>(); 
    Bubbles blobsOb = new Bubbles(x, y , size , Color.GREEN); 


    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     Graphics2D g2 =(Graphics2D)g; 


     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
      RenderingHints.VALUE_ANTIALIAS_ON); 
     g2.setColor(Color.BLUE); 
     g2.fill(ball); 
     g2.setColor(Color.green); 

     for (int j = 0 ; j < 10 ; j++)]{ 
      for(int i = 0 ; i < 10; i++){ 
       balls.add(blobsOb); 
       g.setColor(Color.green); 
       g.fillOval(x, y, size, size); 

      }   
     } 
    } 

} 

//SECOND CLASS 
import javax.swing.*; 

public class Main { 

    public static void main(String[] args) { 

     MyBall p = new MyBall(); 
     JFrame f = new JFrame(); 

     f.add(p); 
     f.setVisible(true); 
     f.setLocation(200,200); 
     f.setSize(400, 420); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    } 

} 


//Third Class 

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Graphics; 

// this class is for the properties of green balls 
public class Bubbles extends Component { 
    public int x; 
    public int y; 
    public int size; 
    public Color color; 
    public static Bubbles blob = new Bubbles(250,250,100,Color.BLUE); 

    Bubbles(int x, int y, int size, Color c){ 
     this.x = x; 
     this.y = y; 
     this.size = size; 
     this.color = c; 
    } 

    public void paint(Graphics g){ 
     g.setColor(color); 
     g.fillOval(x, y, size, size); 
    } 

} 
+0

你是什麼確切的問題,以創建新的實例?這個問題很廣泛。 – coinbird

+0

@coinbird我不知道如何創建數組列表來持有球連續創建 –

+0

您只需將它列爲一個列表,然後您不必使用大小進行初始化。 List list = new ArrayList <>(); – coinbird

回答

1

我想創建持有綠球,但問題 是我剛剛得到一個綠色的球,而不是(10)以上

首先數組列表,這應該是裏面的循環:

Bubbles blobsOb = new Bubbles(x, y , size , Color.GREEN); 

然後您還需要在循環內插入以下代碼,以確保在每次迭代時都有一個新生成的隨機值。

int xr = rand.nextInt(400); 
int yr = rand.nextInt(400); 
int size = 10; 
int x = xr ; 
int y = yr ; 

例如:

for(int j = 0; j < 10; j++){ 

    for(int i = 0 ; i < 10; i++) 
    { 
     int xr = rand.nextInt(400); 
     int yr = rand.nextInt(400); 
     int size = 10; 
     Bubbles blobsOb = new Bubbles(xr, yr , size , Color.GREEN); 
     balls.add(blobsOb); 
     g.setColor(Color.green); 
     g.fillOval(x, y, size, size); 

    } 

} 

,你必須撥打.setVisible(true)後,所有的組件都被添加到frame

MyBall p = new MyBall(); 
JFrame f = new JFrame(); 
f.add(p);  
f.setLocation(200,200); 
f.setSize(400, 420); 
f.setVisible(true); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

最後但並非最不重要,你已經添加了所有生成的Bubble對象到ArrayList中ArrayList<Bubbles> balls = new ArrayList<Bubbles>(),但是,你有沒有使用balls ArrayList的呢。

+0

什麼都沒發生 –

+0

你真是太棒了....好吧你知道我怎麼能每次打一個球?例如每隔3秒就有一個球出現並且非常感謝你 –

+0

Mohammed你已經將所有的氣泡對象添加到了ArrayList **球中**但是你還沒有使用ArrayList。 –

2

你需要在每個週期

for(int j = 0; j < 10; j++){ 

    for(int i = 0 ; i < 10; i++) 
    { 
     // ... 
     balls.add(new Bubbles(xr, yr , size , Color.GREEN)); 

    } 

}