2017-04-21 82 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)); 

    } 

}