gpt4 book ai didi

java - 如何停止此代码中的闪烁

转载 作者:行者123 更新时间:2023-11-29 07:07:59 25 4
gpt4 key购买 nike

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

/*
<applet height=800 width=600 code="RaceApplet.java"></applet>
*/

public class RaceApplet extends JApplet implements KeyListener
{
private Image player;
private Image bg;
private int nx = 800;
private int ny = 0;
private Rectangle rect;

private void loadPicture()
{
bg = new ImageIcon("RaceBack.png").getImage();
player = new ImageIcon("KD//KDE.png").getImage();
}

public void init()
{
loadPicture();
rect = new Rectangle(250, 93, 50, 50);
this.addKeyListener(this);
}

public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.green);
g.fillRect(0, 0, 34567, 34567);
g.drawImage(bg, nx - 800, ny, null);
g.drawImage(player, rect.x, rect.y, null);

}

public void keyPressed(KeyEvent e)
{
if( e.getKeyCode() == KeyEvent.VK_RIGHT )
{
nx = nx - 20;
player = new ImageIcon("KD//KDE.png").getImage();
}
if( e.getKeyCode() == KeyEvent.VK_LEFT )
{
nx = nx + 20;
player = new ImageIcon("KD//KDW.png").getImage();
}
if( e.getKeyCode() == KeyEvent.VK_UP )
{
ny = ny + 20;
player = new ImageIcon("KD//KDN.png").getImage();
}
if( e.getKeyCode() == KeyEvent.VK_DOWN )
{
ny = ny - 20;
player = new ImageIcon("KD//KDS.png").getImage();
}

repaint();
}

public void keyReleased(KeyEvent e)
{

}

public void keyTyped(KeyEvent e)
{

}

}

问题是当我在小程序屏幕上移动我的车时它会闪烁。有什么解决办法吗。我怎样才能让它不闪烁屏幕。我已经在大多数网站上进行了搜索,但他们显示的方式并没有那么奏效

提前感谢帮助

最佳答案

您应该在开始时加载所有图像,而不是每次需要时都实例化它们。

关于java - 如何停止此代码中的闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17568408/

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