我有一个Java小程序,每次重新绘制GUI时,它都会不停地为我闪烁。大多数人没有看到闪烁。其他人可以在没有任何问题的情况下运行applet,但我没有。
我不确定这是我重绘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);
}
}发布于 2013-09-03 04:21:00
这是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);
}现在,由于假定applet的大小不会发生很大的变化是非常安全的,所以可以创建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作为基本画布,重写它的paintComponent方法,并通过Swing API获得自动双缓冲.
https://stackoverflow.com/questions/18584009
复制相似问题