2017-02-21 1 views
-1

私は、すべての研究にもかかわらず、材料を理解できないようなので、クラスの宿題に取り組んでいます。私は初心者であり、Javaの方法ではあまり知らない。また、これは私の最初の投稿ですので、あなたがこれを読んでいるときには許してください。Javaで異なる速度で移動する複数のオブジェクトをペイントするにはどうすればよいですか?

最近の過去の宿題を更新した教科書からソースコードを作成していますが、今では複数の四角形を描画し、それらのオブジェクトを独立して異なる速度で移動するクラスを生成しようとしています。彼らはすべて壁からも跳ね返る必要があります。私は指示に従って、1と10の間のランダムなxとyの値を保持する2つの配列を作成しました。しかし、私は配列に苦労し、正しく行っていないと確信しています。だから、私はそれが正しく設定されているかどうかを確認するためにいくつかのフィードバックを愛するだろう。

私は引き上げJPanelの描画を持ち、そして限り、正方形の1があるとして、それが壁に跳ね返る正常に動作しているが、私は複数を描く際に物事が変化します。独立して動かず、同じ速度を共有します。いくつかは時々消える。これは本当に私を捨てた。私は助けていただきありがとうございます!要するに

、私はすべてが異なる方向に異なる速度で移動する新しい四角形を描画しようとしています。私たちが想定している指示に従って、xとyの値を扱う2つの配列を作成して使用します。ここで

は、私がこれまで持っているものです。

public class DotsPanel extends JPanel 
{ 
private int delay = 15; 
private final int SIZE = 7, IMAGE_SIZE = 3; // radius of each dot 
private Timer timer; 
private int x, y, i; 
private ArrayList<Point> pointList; 
static int [] xarray = new int [1000]; 
static int [] yarray = new int [1000]; 
Random rand = new Random(); 
    //----------------------------------------------------------------- 
    // Constructor: Sets up this panel to listen for mouse events. 
    //----------------------------------------------------------------- 
    public DotsPanel() 
    { 
     pointList = new ArrayList<Point>(); 
     int [] xarray = new int [1000]; 
     int [] yarray = new int [1000]; 

     timer = new Timer(delay, new ReboundListener()); 
     addMouseListener (new DotsListener()); 
     addMouseMotionListener (new DotsListener()); 

     setBackground(Color.gray); 
     setPreferredSize(new Dimension(700, 500)); 
     for(int i = 0; i < xarray.length; i++) 
     { 
     xarray[i] = rand.nextInt(7); 
     yarray[i] = rand.nextInt(7); 
     } 
     timer.start();   
    } 

    //----------------------------------------------------------------- 
    // Draws all of the dots stored in the list. 
    //----------------------------------------------------------------- 
    public void paintComponent(Graphics page) 
    { 
     super.paintComponent(page); 
     page.setColor(Color.BLUE); 

     for (Point spot : pointList) 
     { 
     page.fillRect(spot.x-SIZE, spot.y-SIZE, 25, 25); 
     page.drawString("Count: " + pointList.size(), 5, 15); 
     } 
    } 



//***************************************************************** 
    // Represents the listener for mouse events. 
    //***************************************************************** 
    private class DotsListener implements MouseListener, MouseMotionListener 
    { 
     //-------------------------------------------------------------- 
     // Adds the current point to the list of points and redraws 
     // the panel whenever the mouse button is pressed. 
     //-------------------------------------------------------------- 
     public void mousePressed(MouseEvent event) 
     { 
      pointList.add(event.getPoint());  
      repaint(); 
     } 
     public void mouseDragged(MouseEvent event) 
     { 
     // initially I had two xarray and yarray in here just like in 
     // mouseClicked 
     // but it did not change anything when removed 
     } 
     //-------------------------------------------------------------- 
     // Provide empty definitions for unused event methods. 
     //-------------------------------------------------------------- 
     public void mouseClicked(MouseEvent event) 
     { 
      xarray[i] = rand.nextInt(7); 
      yarray[i] = rand.nextInt(7);    
     } 
     public void mouseReleased(MouseEvent event) {} 
     public void mouseEntered(MouseEvent event) {} 
     public void mouseExited(MouseEvent event) {} 
     public void mouseMoved(MouseEvent e) {} 
    } 
    private class ReboundListener implements ActionListener 
    { 
     //-------------------------------------------------------------- 
     // Updates the position of the image and possibly the direction 
     // of movement whenever the timer fires an action event. 
     //-------------------------------------------------------------- 
     public void actionPerformed(ActionEvent event) 
     { 


      for (Point spot : pointList) 
      { 
       spot.x += xarray[i]; 
       spot.y += yarray[i]; 
       if (spot.x <= 0 || spot.x >= 700) 
        xarray[i] = xarray[i] * -1; 
       if (spot.y <= 0 || spot.y >= 500) 
        yarray[i] = yarray[i] * -1; 
       repaint(); 
      } 
     } 
    } 
} 

答えて

3

はしかし、私は配列に苦労し、私はそれを正しくやっておりませんことを確信しています。

私は、配列を使用することはありません。

代わりに、Ballオブジェクトは、自身の状態を管理しています。次に、Ballごとに異なる色、速度、サイズなどを持つことができます。その後、Timerが発砲したときには、新しい位置を計算してBallを再ペイントするだけです。

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(500); 

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

     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 timer = new Timer(75, this); 
      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; 
     } 
    } 

} 

あなたの配列が唯一のあなたは各点がで移動しなければならない速さについての情報を持っていないペイントしたいポイントを含んでいるので:ここ

はあなたが始めるために一例です。あなたができる最善の方法は、その場所が変更されるたびに各ポイントを移動させるべきランダムな量を作成することです。これは、ポイントを移動するたびに距離がランダムになるたびに不規則な動きをします。

スピードを一定にするには、毎回各ポイントが移動する距離を格納するために2番目の配列を作成する必要があります。

これは、塗りつぶしたいオブジェクトの新しいプロパティを一意にするたびに、新しい配列を作成するのが面倒です。そのため、複数のプロパティを持つカスタムオブジェクトを作成する方法を管理する方が簡単です。

+0

ありがとうございました!あなたのコードは、私が指示でやるべきことより確かに論理的です。しかし、私がガイドラインを守らないと、私はその資料をさらに遅らせることができるのではないかと心配しています。同様に、この宿題のために同じ資料を含むかもしれない将来のプロジェクトの準備をしてはいけません。 xとyの動きの速度に対応するために、さらに2つの配列を作成する必要がありますか?または、描かれている各四角形を処理する配列を生成できますか?助けてくれてありがとう! – Jfwhyte8

+0

@ Jfwhyte8、私はあなたの指針や例が教えようとしていることを理解していないので、私はあなたの質問に答えることができません。 – camickr

+0

お手数をおかけしていただきありがとうございます。申し訳ありませんが、今返信しています。人生は控えめに言って忙しかったです。 私の目的は、タイマーを使ってアニメーションを追加して、描画時に四角形がすべて動くようにすることです。私は各方形をランダムな速度で(x方向とy方向に)移動させる必要があり、四角形を画面の4辺すべてから適切に「バウンス」させる必要があります。さらに、私の先生は、Xの動きとYの動きに対して1〜10のランダムな値を保持するには、サイズ1000の配列を2つ使用する必要があると言っていました。これにより、長方形が独立した移動速度を持つことが可能になるはずです。 – Jfwhyte8

関連する問題