我正在努力沿“网格”移动形状。我似乎终于搞清楚了,但我有一个小问题;无论我试图获得每次运动之间的具体时间间隔有多近,似乎仍然离开了网格。我想使用我正在使用的当前方法,因为我理解thigs如何以这种方式工作。我知道有时会出现随机运动“故障”,因为如果我将它向后移动并且第四次它应该被锁定在网格上,无论如何。这是我的代码(抱歉缺少评论和我只是测试代码的奇怪位置):
主类:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JFrame;
public class WhyOhWhy {
public int x;
public int y;
public static void main(String[] args) {
JFrame f = new JFrame();
InputHandler input = new InputHandler();
f.add(input);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800,600);
input.doStuff();
}
}
InputHandler类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class InputHandler extends JPanel implements ActionListener, KeyListener {
Timer t = new Timer(5, this);
int x = 0, y = 0, velX = 0, velY = 0;
int i = 0, j = 0;
TimeKeeper timeStart;
public void doStuff(){
velX = 0;
velY = 0;
}
public InputHandler() {
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.fill(new Ellipse2D.Double(x, y, 32, 32));
for(int i = 0;i <500;i+=32){
g2.drawRect(i, j, 32, 32);
for(int j=0;j<500;j+=32){
g2.drawRect(i, j, 32, 32);
}
}
}
public void actionPerformed(ActionEvent e) {
repaint();
x += velX;
y += velY;
if(TimeKeeper.isFinished() == true){
System.out.println("DONE");
TimeKeeper.resetTimer(false);
velX = 0;
velY = 0;
setEnabled(true);
}
}
public void up() {
//System.out.println("Moving up");
timeStart = new TimeKeeper(185);
velY = -1;
velX = 0;
setEnabled(false);
}
public void down() {
//System.out.println("Moving down");
setEnabled(false);
timeStart = new TimeKeeper(185);
velY = 1;
velX = 0;
}
public void left() {
//System.out.println("Moving left");
setEnabled(false);
timeStart = new TimeKeeper(185);
velY = 0;
velX = -1;
}
public void right() {
//System.out.println("Moving right");
setEnabled(false);
timeStart = new TimeKeeper(185);
velY = 0;
velX = 1;
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_W) {
up();
}
if (code == KeyEvent.VK_S) {
down();
}
if (code == KeyEvent.VK_A) {
left();
}
if (code == KeyEvent.VK_D) {
right();
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
velX = 0;
velY = 0;
}
}
TimeKeeper类:
import java.util.Timer;
import java.util.TimerTask;
public class TimeKeeper {
Timer timer;
public static boolean isDone = false;
public TimeKeeper(int seconds) {
timer = new Timer();
isDone = false;
timer.schedule(new RemindTask(), seconds );
}
class RemindTask extends TimerTask {
public void run() {
//System.out.println("Time's up!");
isDone = true;
timer.cancel(); //Terminate the timer thread
}
}
public static boolean isFinished() {
return isDone;
}
public static void resetTimer(boolean done) {
isDone = done;
}
}
谢谢!
答案 0 :(得分:0)
没关系,想通了。需要使用%运算符来查看形状移动的数字是否在x和y方向上每步可被32整除。