我有一个 Java Applet,每次重绘 GUI 时它都会不断闪烁。大多数人看不到闪烁。其他人能够毫无问题地运行该小程序,但我却不能。
我不确定这是否是我重绘 UI 的方式的问题,或者这是我的 JRE 的问题还是其他什么问题。我已经筋疲力尽地查找其他人的类似问题,但我还没有看到适用于此的答案。
我已经将我正在运行的小程序缩短到相对较小的位置。这只是一艘向右移动的火箭飞船,非常简单,而且(对我来说)它不断闪烁。
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Lander extends Applet implements KeyListener,ActionListener{
Button start;
Ship s;
private static final long serialVersionUID = 1L;
public void init(){
s = new Ship(10,50);
start = new Button("START");
this.setSize(800,400);
this.setBackground(Color.black);
this.add(start);
start.addActionListener(this);
}
public void paint(Graphics g){
g.setColor(Color.LIGHT_GRAY);
g.fillRoundRect(s.getx(),s.gety(), 10, 30, 10, 10);
g.setColor(Color.GREEN);
g.fillRoundRect(s.getx()-5, s.gety()+10, 5, 20, 10, 10);
g.fillRoundRect(s.getx()+10, s.gety()+10, 5, 20, 10, 10);
}
javax.swing.Timer t = new javax.swing.Timer(30, new ActionListener(){
public void actionPerformed(ActionEvent e){
s.setx(s.getx()+2);
repaint();
}
});
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
public class Ship{
int xcor,ycor;
public Ship(int x,int y){
xcor=x;ycor=y;
}
public void setx(int x){
xcor=x;
}
public void sety(int y){
ycor=y;
}
public int getx(){
return xcor;
}
public int gety(){
return ycor;
}
}
@Override
public void actionPerformed(ActionEvent x) {
if(x.getSource()==start)
reset();
t.start();
}
public void reset(){
s.setx(10);s.sety(50);
}
}
最佳答案
这是 Applet
未以任何方式开始缓冲的结果。
你应该采用某种双缓冲...
例如...
@Override
public void paint(Graphics g) {
BufferedImage bi = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
super.paint(g2d);
g2d.setColor(Color.LIGHT_GRAY);
g2d.fillRoundRect(s.getx(), s.gety(), 10, 30, 10, 10);
g2d.setColor(Color.GREEN);
g2d.fillRoundRect(s.getx() - 5, s.gety() + 10, 5, 20, 10, 10);
g2d.fillRoundRect(s.getx() + 10, s.gety() + 10, 5, 20, 10, 10);
g2d.dispose();
g.drawImage(bi, 0, 0, this);
}
现在,因为可以安全地假设小程序的大小不会发生太大变化,所以您可以创建 BufferedImage
的类实例并重用它,例如......
private BufferedImage backingBuffer;
@Override
public void invalidate() {
backingBuffer = null;
super.invalidate();
}
@Override
public void paint(Graphics g) {
if (backingBuffer == null) {
backingBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
}
Graphics2D g2d = backingBuffer.createGraphics();
super.paint(g2d);
g2d.setColor(Color.LIGHT_GRAY);
g2d.fillRoundRect(s.getx(), s.gety(), 10, 30, 10, 10);
g2d.setColor(Color.GREEN);
g2d.fillRoundRect(s.getx() - 5, s.gety() + 10, 5, 20, 10, 10);
g2d.fillRoundRect(s.getx() + 10, s.gety() + 10, 5, 20, 10, 10);
g2d.dispose();
g.drawImage(backingBuffer, 0, 0, this);
}
我还会质疑从 Applet
扩展的需要/使用。如果您要使用 JApplet
,您可以使用 JPanel
作为基础 Canvas ,重写它的 paintComponent
方法并通过 Swing 获得自动双缓冲API...
关于Java Applet运行时闪烁? (不是每个人都看到闪烁),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18584009/