[英]Moving Rectangle With Arrow Keys
Basically, I am trying to make a program that moves a "platter" one pixel to the left when the left arrow key is pressed, and one to the right when the right arrow key is pressed. 基本上,我试图制作一个程序,当按下左箭头键时,将“拼版”向左移动一个像素,而当按下右箭头键时,将“拼写”向左移动一个像素。 Currently, nothing is drawing on my output window when I compile and run, and I am not too sure what I am doing wrong here or where exactly the code isn't doing what I want.
目前,在编译和运行时,输出窗口上没有任何内容,而且我不太确定自己在这里做错了什么,或者代码在哪里没有真正完成我想要的工作。 Any help is appreciated, thank you!
任何帮助表示赞赏,谢谢!
import java.awt.*;
import java.net.*;
import java.util.*;
import java.applet.Applet;
public class game extends Applet
{
Thread loopThread;
boolean left = false;
boolean right = false;
int platPos = 50;
public void run()
{
Graphics g = null;
int i, j;
long startTime;
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
startTime = System.currentTimeMillis();
while (Thread.currentThread() == loopThread)
{
updatePlatter(g);
}
}
public void updatePlatter(Graphics g)
{
if(left)
{
g.setColor(new Color(255,255,255));
g.fillRect(50+platPos, 200, 100, 20);
platPos--;
g.setColor(new Color(100,100,100));
g.fillRect(50+platPos,200, 100,20);
}
if(right)
{
g.setColor(new Color(255,255,255));
g.fillRect(50+platPos,200,100,20);
platPos++;
g.setColor(new Color(100,100,100));
g.fillRect(50+platPos,200,100,20);
}
}
public boolean keyDown(Event e, int key)
{
if (key == Event.LEFT)
left = true;
if (key == Event.RIGHT)
right = true;
return true;
}
}
I deleted my answer as it's impossible to fix your existing code without rewriting everything, here is a good starting point for you to learn : 我删除了我的答案,因为无法在不重写所有内容的情况下修复您现有的代码,这是您学习的好起点:
http://www.cs.stir.ac.uk/~sbj/examples/Java-examples-basic/Move/Move.java http://www.cs.stir.ac.uk/~sbj/examples/Java-examples-basic/Move/Move.java
Basically it does what you want but using buttons. 基本上,它可以执行您想要的操作,但是使用按钮即可。
Also as @Ben stated, you should consider using something else than Applet, it's pretty dead.. 另外,正如@Ben所说,您应该考虑使用Applet之外的其他工具,这已经死了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.