私が作成した四角形を移動したいのですが、私が描画した四角形をすべて移動する方法にはまっています。現時点では、私は描画する四角形だけを動かしているので、動くことから引いたすべての前の四角形が停止します。どのように私はそれを回避するのですか?私は、四角形を格納する配列を使用する必要があると思うが、私はタイマーによって使用される私の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();
}
}
}
私は私のアクションリスナー
あなたはpsudoループのような 'Timer'を考えなければなりません。各tickはループの繰り返しです。そして' ActionListener'でタイマーを再起動しません。独自の – MadProgrammer
あなたのコードはconsufsingです。あなたは更新しているポイント情報を保持すると思われる 'points'と' points2'の2つの配列を持っていますが、 'paintComponent'メソッドでは' pointList'を使用しています。あなたが実際にペイントしたいのは何ですか? – MadProgrammer
私はそれを見ています。私はどのように私は描くすべての単一の形に行くことを周りに行くだろうか? – Daisy