import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Racing extends JFrame {
private static final long serialVersionUID = -198172151996959655L;
//makes the screen size
final int WIDTH = 900, HEIGHT = 650;
//keeps track of player speed
double plSpeed = .5;
//numbers that represent direction
final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3, STOP = 5, START = 6;
//keeps track of player direction
int p1Direction = START;
//makes player 1's car
Rectangle p1 = new Rectangle ( 100, 325, 30, 30 );
Rectangle foreground = new Rectangle( 500, 500, 200, 200 );
//constructor
public Racing() {
//define defaults for the JFrame
super ("Racing");
setSize( WIDTH, HEIGHT );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setBackground(Color.BLACK);
//start the inner class
Move1 m1 = new Move1();
m1.start();
}
//draws the cars and race track
public void paint(Graphics g) {
super.paint(g);
//draw p1
g.setColor(Color.RED);
g.fillRect(p1.x,p1.y,p1.width,p1.height);
g.setColor(Color.GREEN);
g.fillRect(foreground.x,foreground.y,foreground.width,foreground.height);
}
private class Move1 extends Thread implements KeyListener {
public void run() {
//makes the key listener "wake up"
addKeyListener(this);
//should be done in an infinite loop, so it repeats
while (true) {
//make try block, so it can exit if it errors
try {
//refresh screen
repaint();
//makes car increase speed a bit
if (plSpeed <= 7) {
plSpeed += .2;
}
//lets the car stop
if (plSpeed==0) {
p1Direction = STOP;
}
//moves player based on direction
if (p1Direction==UP) {
p1.y -= (int) plSpeed;
}
if (p1Direction==DOWN) {
p1.y += (int) plSpeed;
}
if (p1Direction==LEFT) {
p1.x -= (int) plSpeed;
}
if (p1Direction==RIGHT) {
p1.x += (int) plSpeed;
}
if (p1Direction==STOP) {
plSpeed = 0;
}
//delays refresh rate
Thread.sleep(75);
}
catch(Exception e) {
//if an error, exit
break;
}
}
}
//have to input these (so it will compile)
public void keyPressed(KeyEvent event) {
try {
//makes car increase speed a bit
if (event.getKeyChar()=='w' ||
event.getKeyChar()=='a' ||
event.getKeyChar()=='s' ||
event.getKeyChar()=='d') {
plSpeed += .2;
repaint();
}
} catch (Exception I) {}
}
public void keyReleased(KeyEvent event) {}
//now, to be able to set the direction
public void keyTyped(KeyEvent event) {
if (plSpeed > 0) {
if (event.getKeyChar()=='a') {
if (p1Direction==RIGHT) {
p1Brake();
} else {
if (p1Direction==LEFT) {
} else {
p1Direction = LEFT;
}
}
}
if (event.getKeyChar()=='s') {
if (p1Direction==UP) {
p1Brake();
} else {
if (p1Direction==DOWN) {
} else {
p1Direction = DOWN;
}
}
}
if (event.getKeyChar()=='d') {
if (p1Direction==LEFT) {
p1Brake();
} else {
if (p1Direction==RIGHT) {
} else {
p1Direction = RIGHT;
}
}
}
if (event.getKeyChar()=='w') {
if (p1Direction==DOWN) {
p1Brake();
} else {
if (p1Direction==UP) {
} else {
p1Direction = UP;
}
}
}
if (event.getKeyChar()=='z') {
p1Brake();
}
}
}
public void p1Brake () {
try {
while (plSpeed != 0) {
plSpeed -= .2;
Thread.sleep(75);
}
} catch (Exception e) {
plSpeed = 0;
}
}
}
//finally, to start the program
public static void main(String[] args) {
Racing frame = new Racing();
frame.setVisible( true );
frame.setLocationRelativeTo( null );
frame.setResizable( false );
}
这是我的代码的SSCCE(Short, Self Contained, Correct Example)版本。如果我在类内部添加super.paint(g);,则会出现闪烁问题。如果不加这个语句,那么每当您移动玩家时,就会创建一条玩家行进路径的线条-而不进行重绘。我需要知道如何在哪里进行重绘。我在这里找到了离答案最近的地方:
http://www.java-forums.org/awt-swing/37406-repaint-without-flashing.html
但他们使用了一个小程序(我以前从未接触过,假定将代码从小程序转换为框架可能相当棘手)。有人能帮我吗?
备注:
我不知道您可以使用AWT创建一个框架,因为我对Swing很熟悉并且适应它,所以我不想改变。抱歉。您可以看到,无论我绘制什么都会闪烁,不仅仅是玩家。
Andrew,这是我的屏幕截图:
哦,它没有注册P2。
p1Brake
的情况下,也永远不要在EDT上使用Thread.sleep(n)
。;) - Andrew Thompson