我想移动我创建的方形,但我仍然坚持如何移动我绘制的所有方块。截至目前,我只是移动我绘制的每个方格,因此,它停止了我从移动中提取的所有先前方块。我该如何解决这个问题。我想我必须使用存储方块的数组,但是如何将它插入到计时器使用的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();
}
}
}
这是我有动作监听器的地方
答案 0 :(得分:3)
好吧,经过一番头疼之后,我想我明白你要做什么。
基本上,在你的Timer
中,你将每个运动矢量应用到每个点,我怀疑你真正想要做什么,相反,每个点应该有它自己的向量,应该对它进行更新< / p>
我简化了一些想法并创造了一个简单的可移动物体,它将当前位置和矢量保持在一起,它还封装了运动和绘画,使其更加独立
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;
}
}
}
}