gpt4 book ai didi

java - 如何防止闪烁?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:23:44 25 4
gpt4 key购买 nike

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。如果我添加 super.paint(g);最重要的是,在类里面,一切都变得浮华。如果我不考虑它,那么每当你移动玩家时,它都会创建一条玩家去过的地方的线 - 无需重新绘制。我需要知道如何 - 以及在哪里重新粉刷。我在这里最接近我的答案:

http://www.java-forums.org/awt-swing/37406-repaint-without-flashing.html

但是他们有一个小程序(我以前从未处理过,并且假设将代码从小程序转换为框架会相当棘手)。有人可以帮我解决这个问题吗?

注意事项:

我不知道你可以用 awt 制作框架,因为我很高兴并且熟悉 swing,所以我不想改变。对于那个很抱歉。如您所见,无论我绘制什么都会闪烁,而不仅仅是播放器。

安德鲁,这是我的截图: the game

哦,它没有注册 P2。

最佳答案

Racing UI

我做了一些改变。以下是我能记忆起的一些内容。

  1. 将自定义绘画从顶级容器重构到 JPanel,并将绘画移动到 paintComponent()
  2. 删除了 ThreadThread.sleep(n) 并替换为 Timer/ActionListener。<
  3. 在 EDT 上构建了 GUI。
  4. 删除了设置 JFrame 的大小。而是为 JPanel(实际绘图区域)设置首选大小并调用 JFrame.pack() 以获得正确的整体大小。
  5. 使用 setLocationByPlatform(true) 代替 very splash-screen like setLocationRelativeTo(null)

仔细检查代码以获取更多提示。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Racing extends JPanel implements KeyListener {
private static final long serialVersionUID = -198172151996959655L;

//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, 25, 30, 30 );

//constructor
public Racing() {
//define defaults for the JFrame
setBackground(Color.BLACK);

//makes the screen size
setPreferredSize(new Dimension(400,50));

//makes the key listener "wake up"
addKeyListener(this);
setFocusable(true);

ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//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;
}
}
};

Timer t = new Timer(75,al);
t.start();
}

//draws the cars and race track
@Override
public void paintComponent(Graphics g) {

super.paintComponent(g);
//draw p1
g.setColor(Color.RED);
g.fillRect(p1.x,p1.y,p1.width,p1.height);

}

//have to input these (so it will compile)
public void keyPressed(KeyEvent event) {
System.out.println(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;
}
} catch (Exception e) {
plSpeed = 0;
}
}

//finally, to start the program
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
JFrame f = new JFrame("Racing");
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.add(new Racing());
f.pack();
f.setLocationByPlatform(true);
f.setResizable( false );
f.setVisible( true );
}
});
}
}

关于java - 如何防止闪烁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8905376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com