2017-03-08 67 views
-1

我正在製作一個雨模擬器。我的想法是,每一滴都應該是一個對象,但由於某種原因,它們不會顯示在JFrame上。如果我更改矩形的座標值,但是如果我隨機化數字,則不會。什麼導致對象不出現?Java - 對象的隨機放置

import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 

    public class Rain extends RainDrop implements ActionListener{ 

    //Change the following 2 lines of variables to change the graphics 
    static int width = 1000, height = 600; 
    int dropAmount = 650, speed = 10; 

    Timer tm = new Timer(speed, this); 
    RainDrop[] RainDrop = new RainDrop[650]; 

    public Rain(){ 
     for(int i = 0; RainDrop.length > i; i++){ 
      RainDrop[i] = new RainDrop(); 
      RainDrop[i].generateRain(); 
      add(RainDrop[i]); 
     } 
     repaint(); 
    } 

    public void paintComponent(Graphics g){ 
     tm.start(); 
    } 

    public void actionPerformed(ActionEvent e){ 
     repaint(); 
    } 

    public static void main(String[] args) { 
     JFrame f = new JFrame(); 
     f.setLocation(100, 100); 
     f.setSize(width, height); 
     f.setTitle("Rain"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Rain Rain = new Rain(); 
     f.add(Rain); 
     f.setResizable(true); 
     f.setVisible(true); 
    } 
    } 

    import java.awt.Color; 
    import java.awt.Graphics; 
    import javax.swing.JPanel; 

public class RainDrop extends JPanel{ 

//Drop [0] is used to store x values 
//Drop [1] is used to store y values 
//Drop [2] is used to store height values 
//Drop [3] is used to store width values 
//Drop [4] is used to store velocity values 

private int[] Drop = new int [5]; 
private int width = 1000, height = 600; 

public RainDrop(){ 
} 

public int[] generateRain(){ 
     Drop [0] = (int) (Math.random() * width);  
     Drop [1] = (int) (Math.random() * height); 
     Drop [2] = (int) (Math.random() * 13); 
     Drop [3] = (int) (Math.random() * 4); 
     Drop [4] = (int) (Math.random() * 6); 

     if(Drop [2] < 5) Drop [2] += 9; 

     if(Drop [3] < 3) Drop [3] += 3; 

     if(Drop [3] == 5) Drop [3] = 4; 

     if(Drop [4] < 3) Drop [4] += 3; 

    return Drop; 
} 

public void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    g.setColor(Color.red); 

    g.fillRect(Drop [0], Drop [1], 5, 20); 
} 
}  

回答

0

我不能讓你的代碼,以顯示任何東西,如果我走了隨機量,但我不知道你實際上並沒有畫的雨滴。

在Rain.java:

public void paintComponent(Graphics g) { 
    for (int i = 0; RainDrop.length > i; i++) { 
     RainDrop[i].generateRain(); 
     RainDrop[i].paintComponent(g); 
    } 
} 

,並把tm.start();在構造函數。您不需要在每次重新繪製時啓動定時器

+0

「generateRain」方法返回一個數組,其中包含x和y等值。不應該paintComponent使用這些值,因爲我已經使用Drop [0]和Drop [1]作爲x和y。 –

+0

我會如何畫他們? –

+0

您的繪畫組件方法確實使用這些值。這就是爲什麼我要說這種方法 – mike

2

基本設計是錯誤的。 Rain不應該延伸RainDrop

Rain應該只是一個帶有paintComponent()方法的JPanel,該方法將繪製一個RainDrop對象。

創建一個ArrayList以容納RainDrop對象。然後你可以在Rain面板上啓動計時器。定時器觸發時,您可以迭代ArrayList中的所有對象,並更改每個RainDrop的位置,然後重新繪製()面板。

以下是顯示此基本方法的示例。它展示瞭如何在面板上移動球:

import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.*; 
import java.util.*; 
import javax.swing.*; 
import javax.swing.Timer; 

public class BallAnimation4 
{ 
    private static void createAndShowUI() 
    { 
     BallPanel panel = new BallPanel(50); 

     JFrame frame = new JFrame("BallAnimation4"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
     frame.add(panel); 
     frame.pack(); 
     frame.setVisible(true); 

     panel.startAnimation(); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowUI(); 
      } 
     }); 
    } 

    static class BallPanel extends JPanel implements ActionListener 
    { 
     private ArrayList<Ball> balls = new ArrayList<Ball>(); 
     private BufferedImage bi; 
     private Timer timer = new Timer(75, this); 

     public BallPanel(int ballCount) 
     { 
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 

      setLayout(null); 
      setBackground(Color.BLACK); 

      Random random = new Random(); 

      for (int i = 0; i < ballCount; i++) 
      { 
       Ball ball = new Ball(); 
       ball.setRandomColor(true); 
       ball.setLocation(random.nextInt(screenSize.width), random.nextInt(screenSize.height)); 
       ball.setMoveRate(32, 32, 1, 1, true); 
       ball.setSize(32, 32); 
       balls.add(ball); 
      } 
     } 

     @Override 
     public void paintComponent(Graphics g) 
     { 
      super.paintComponent(g); 

      for (Ball ball: balls) 
      { 
       ball.draw(g); 
      } 
     } 

     public void startAnimation() 
     { 
      timer.start(); 
     } 

     public void actionPerformed(ActionEvent e) 
     { 
      move(); 
      repaint(); 
     } 

     private void move() 
     { 
      for (Ball ball : balls) 
      { 
       ball.move(this); 
      } 
     } 
    } 

    static class Ball 
    { 
     public Color color = Color.BLACK; 

     public int x = 0; 
     public int y = 0; 
     public int width = 1; 
     public int height = 1; 

     private int moveX = 1; 
     private int moveY = 1; 
     private int directionX = 1; 
     private int directionY = 1; 
     private int xScale = moveX; 
     private int yScale = moveY; 

     private boolean randomMove = false; 
     private boolean randomColor = false; 
     private Random myRand = null; 

     public Ball() 
     { 
      myRand = new Random(); 
      setRandomColor(randomColor); 
     } 

     public void move(JPanel parent) 
     { 
      int iRight = parent.getSize().width; 
      int iBottom = parent.getSize().height; 

      x += 5 + (xScale * directionX); 
      y += 5 + (yScale * directionY); 

      if (x <= 0) 
      { 
       x = 0; 
       directionX *= (-1); 
       xScale = randomMove ? myRand.nextInt(moveX) : moveX; 
       if (randomColor) setRandomColor(randomColor); 
      } 

      if (x >= iRight - width) 
      { 
       x = iRight - width; 
       directionX *= (-1); 
       xScale = randomMove ? myRand.nextInt(moveX) : moveX; 
       if (randomColor) setRandomColor(randomColor); 
      } 

      if (y <= 0) 
      { 
       y = 0; 
       directionY *= (-1); 
       yScale = randomMove ? myRand.nextInt(moveY) : moveY; 
       if (randomColor) setRandomColor(randomColor); 
      } 

      if (y >= iBottom - height) 
      { 
       y = iBottom - height; 
       directionY *= (-1); 
       yScale = randomMove ? myRand.nextInt(moveY) : moveY; 
       if (randomColor) setRandomColor(randomColor); 
      } 
     } 

     public void draw(Graphics g) 
     { 
      g.setColor(color); 
      g.fillOval(x, y, width, height); 
     } 

     public void setColor(Color c) 
     { 
      color = c; 
     } 

     public void setLocation(int x, int y) 
     { 
      this.x = x; 
      this.y = y; 
     } 

     public void setMoveRate(int xMove, int yMove, int xDir, int yDir, boolean randMove) 
     { 
      this.moveX = xMove; 
      this.moveY = yMove; 
      directionX = xDir; 
      directionY = yDir; 
      randomMove = randMove; 
     } 

     public void setRandomColor(boolean randomColor) 
     { 
      this.randomColor = randomColor; 

      switch (myRand.nextInt(3)) 
      { 
       case 0: color = Color.BLUE; 
         break; 
       case 1: color = Color.GREEN; 
         break; 
       case 2: color = Color.RED; 
         break; 
       default: color = Color.BLACK; 
         break; 
      } 
     } 

     public void setSize(int width, int height) 
     { 
      this.width = width; 
      this.height = height; 
     } 
    } 

}