2017-02-08 4 views
0

私が作成した四角形を移動したいのですが、私が描画した四角形をすべて移動する方法にはまっています。現時点では、私は描画する四角形だけを動かしているので、動くことから引いたすべての前の四角形が停止します。どのように私はそれを回避するのですか?私は、四角形を格納する配列を使用する必要があると思うが、私はタイマーによって使用される私のforループにどのように挿入するだろうか?タイマーを使って四角形を移動するにはどうすればいいですか?

package me; 

import java.awt.*; 
import java.awt.event.*; 
import java.util.ArrayList; 
import java.util.Random; 
import javax.swing.Timer; 
import javax.swing.*; 

public class Dots1panel extends JPanel 
{ 
private static final long serialVersionUID = -7762339485544442517L; 
private final int SIZE = 10; 
private ArrayList<Point> pointList; 
private int move; 
private final int Delay=50,WIDTH=500, HEIGHT=400; 
private int[] points = new int[1000]; 
private int[] points2 = new int[1000]; 
Random count = new Random(); 
private Point point; 
private Timer timer=null; 
// private int x, y; 
//--------------------------------------------------------------------------  --------------------------------------------------------------------------------------------------------// 
public Dots1panel() 
{ 
    timer= new Timer(Delay, new ActListener()); 
    pointList = new ArrayList<Point>(); 
    //x =0; 
    //y =50; 
    setBackground (Color.GRAY); 
    setPreferredSize (new Dimension(WIDTH, HEIGHT)); 
    addMouseListener (new DotsListener()); 
    for (move = 0; move < 1000; move++) 
    { 
     points[move] = count.nextInt(10) + 1; 
     points2[move] = count.nextInt(10) + 1; 
    } 
    timer.start(); 
} 
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------// 
public void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 
    int R = (int) (Math.random()*256); //randomizes colors 
    int G = (int)(Math.random()*256); 
    int B= (int)(Math.random()*256); 
    Color randomColor = new Color(R, G, B); 
    //randomize color of the rectangles// 
    g.setColor(randomColor); 
    {for (Point spot : pointList) 
    g.fillRect (spot.x-SIZE, spot.y-SIZE, SIZE*2, SIZE*2); 
    g.drawString ("Count: " + pointList.size(), 10, 15);} 
} 
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------// 
private class DotsListener implements MouseListener 
{ 
    public void mousePressed(MouseEvent event) 
    { 
    pointList.add(event.getPoint()); 
    repaint(); 
    } 
     public void mouseClicked (MouseEvent event) {} 
     public void mouseReleased (MouseEvent event) {} 
     public void mouseEntered (MouseEvent event) {} 
     public void mouseExited (MouseEvent event) {} 
} 
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------// 
private class ActListener 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) 
       point = spot; 
      for(move = 0; move < pointList.size(); move++) 
      { 
      point.x += points[move]; 
      point.y += points2[move]; 

      if (point.x <= 0 || point.x >= WIDTH - SIZE) 
       points[move]=points[move] * -1; 
      if (point.y <= 0 || point.y >= HEIGHT - SIZE) 
       points2[move]=points2[move] * -1; 
      timer.restart(); 
      } 
      repaint(); 
     } 
    } 
} 

私は私のアクションリスナー

+1

あなたはpsudoループのような 'Timer'を考えなければなりません。各tickはループの繰り返しです。そして' ActionListener'でタイマーを再起動しません。独自の – MadProgrammer

+0

あなたのコードはconsufsingです。あなたは更新しているポイント情報を保持すると思われる 'points'と' points2'の2つの配列を持っていますが、 'paintComponent'メソッドでは' pointList'を使用しています。あなたが実際にペイントしたいのは何ですか? – MadProgrammer

+0

私はそれを見ています。私はどのように私は描くすべての単一の形に行くことを周りに行くだろうか? – Daisy

答えて

2

オーケーを持っている場所ですので、いくつかの頭のスクラッチの後、私は私はあなたが何をしようとして理解すると思います。基本的には、あなたのTimerに、あなたは私が疑うれ、すべての点にEVERY動きベクトルを適用している

ではなく、各ポイントは、それ自身のベクトルを持っている必要があり、それに対して

を更新する必要があり、あなたが本当に何をしたいです私はいくつかは、何と一緒に現在の位置とベクトルを維持して、簡単な可動オブジェクトを作成し、それはまた、それはより多くの自己

public class MovableObject { 

    private Point point; 
    private Point vector; 

    public MovableObject(Point point, Point vector) { 
     this.point = point; 
     this.vector = vector; 
    } 

    public void paint(Graphics2D g2d) { 
     g2d.fillRect(point.x - SIZE, point.y - SIZE, SIZE * 2, SIZE * 2); 
    } 

    public void move(Dimension bounds) { 
     point.x += vector.x; 
     point.y += vector.y; 
     if (point.x - SIZE <= 0) { 
      vector.x *= -1; 
      point.x = 0; 
     } else if (point.x + SIZE > bounds.width) { 
      vector.x *= -1; 
      point.x = bounds.width - SIZE; 
     } 
     if (point.y - SIZE <= 0) { 
      vector.y *= -1; 
      point.y = 0; 
     } else if (point.y + SIZE >= bounds.width) { 
      vector.y *= -1; 
      point.y = bounds.height - SIZE; 
     } 
    } 

} 

と作業それの実行可能な例を含んで作り、動きや絵をカプセル化するアイデアを簡素化... 。

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Point; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Random; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test extends JPanel { 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new Test()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    private static final long serialVersionUID = -7762339485544442517L; 
    private final int SIZE = 10; 
    private ArrayList<MovableObject> pointList; 
    private int move; 
    private final int Delay = 50, WIDTH = 500, HEIGHT = 400; 

    Random count = new Random(); 
    private Timer timer = null; 
//--------------------------------------------------------------------------  --------------------------------------------------------------------------------------------------------// 

    public Test() { 
     timer = new Timer(Delay, new ActListener()); 
     pointList = new ArrayList<MovableObject>(); 
     //x =0; 
     //y =50; 
     setBackground(Color.GRAY); 
     setPreferredSize(new Dimension(WIDTH, HEIGHT)); 
     addMouseListener(new DotsListener()); 
     timer.start(); 
    } 
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------// 

    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2d = (Graphics2D) g.create(); 
     int R = (int) (Math.random() * 256); //randomizes colors 
     int G = (int) (Math.random() * 256); 
     int B = (int) (Math.random() * 256); 
     Color randomColor = new Color(R, G, B); 
     //randomize color of the rectangles// 
     g2d.setColor(randomColor); 
     for (MovableObject spot : pointList) { 
      spot.paint(g2d); 
     } 
     g2d.drawString("Count: " + pointList.size(), 10, 15); 
     g2d.dispose(); 
    } 
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------// 

    private class DotsListener implements MouseListener { 

     public void mousePressed(MouseEvent event) { 
     } 

     public void mouseClicked(MouseEvent event) { 
      pointList.add(new MovableObject(new Point(event.getPoint()), new Point(count.nextInt(10) + 1, count.nextInt(10) + 1))); 
     } 

     public void mouseReleased(MouseEvent event) { 
     } 

     public void mouseEntered(MouseEvent event) { 
     } 

     public void mouseExited(MouseEvent event) { 
     } 
    } 
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------// 

    private class ActListener 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 (MovableObject obj : pointList) { 
       obj.move(getSize()); 
      } 
      repaint(); 
     } 
    } 

    public class MovableObject { 

     private Point point; 
     private Point vector; 

     public MovableObject(Point point, Point vector) { 
      this.point = point; 
      this.vector = vector; 
     } 

     public void paint(Graphics2D g2d) { 
      g2d.fillRect(point.x - SIZE, point.y - SIZE, SIZE * 2, SIZE * 2); 
     } 

     public void move(Dimension bounds) { 
      point.x += vector.x; 
      point.y += vector.y; 
      if (point.x - SIZE <= 0) { 
       vector.x *= -1; 
       point.x = 0; 
      } else if (point.x + SIZE > bounds.width) { 
       vector.x *= -1; 
       point.x = bounds.width - SIZE; 
      } 
      if (point.y - SIZE <= 0) { 
       vector.y *= -1; 
       point.y = 0; 
      } else if (point.y + SIZE >= bounds.width) { 
       vector.y *= -1; 
       point.y = bounds.height - SIZE; 
      } 
     } 

    } 
} 
+0

:) – Daisy

+0

@Daisy Gladそれは助けることができました;) – MadProgrammer

+1

@デイジー*「ありがとうございます!! :)」*は[受け入れ](http://meta.stackexchange.com/questions/5234/)です。 5235#5235)あなたの問題を解決するのに役立つ答えと十分な評判が得られたらアップウィンドーするC: – Frakcool

関連する問題