[英]Java Applet flickering when running? (not everybody sees flickering)
我有一個Java Applet,它在每次重繪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);
}
}
這是因為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
的類實例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獲得自動雙重緩沖...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.