【问题标题】:Java Applet flickering when running? (not everybody sees flickering)Java Applet 运行时闪烁? (不是每个人都看到闪烁)
【发布时间】:2013-09-03 12:26:45
【问题描述】:

我有一个 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);
}

}

【问题讨论】:

  • 1) 为什么选择 AWT 而不是 Swing?请参阅Swing extras over AWT 上的此答案,因为有很多放弃使用 AWT 组件的充分理由。如果您需要支持较旧的基于 AWT 的 API,请参阅 Mixing Heavyweight and Lightweight Components。 2) 为什么要编写小程序?如果是由于规范。老师请转至Why CS teachers should stop teaching Java applets
  • @AndrewThompson 这实际上是我从高中编程老师那里得到的代码。这显然是他们目前正在开展的一项计划的一部分。我看了看,无法让它停止闪烁。我从未使用过或被教过使用 Applet,这也是我不熟悉这些问题的部分原因。谢谢你的资源!

标签: java applet flicker


【解决方案1】:

这是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 作为基础画布,覆盖它的paintComponent 方法并通过Swing API 获得自动双缓冲...

【讨论】:

  • 好的,谢谢,我会调查一下,看看是否能解决问题。
  • @leigero 对不起,正在整理一个例子:P
  • 我想,我相信您过去已经回答了我的一些问题,而且它们通常都充满了有用的信息。 =]
  • 希望对您有所帮助 ;)
  • 是的,这正是我所需要的。再次感谢。
猜你喜欢
  • 2017-03-17
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-08
  • 1970-01-01
相关资源
最近更新 更多