运行时,Java小程序闪烁? (不是大家看到闪烁) [英] Java Applet flickering when running? (not everybody sees flickering)

查看:209
本文介绍了运行时,Java小程序闪烁? (不是大家看到闪烁)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不断每次GUI重绘闪烁的时间对我来说是Java小程序。大多数人看不到闪烁。其他人能够在没有任何所有的问题运行小程序,但我不是。

我不知道这是在我重绘UI或者这是与我的JRE或有什么问题的方式有问题。我已经用尽自己仰视别人的类似的问题,但我还没有看到此处适用的答案。

我已经缩短我运行到一个相对较小的位小程序。这只是火箭飞船被移动到右侧,pretty简单,(对我来说)它不断地闪烁。

 进口java.applet.Applet中;
进口java.awt.Button中;
进口java.awt.Color中;
进口java.awt.Graphics;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口java.awt.event.KeyEvent中;
进口java.awt.event.KeyListener;
公共类兰德扩展的Applet实现的KeyListener,ActionListener的{按钮启动;
船舶(S);私有静态最后的serialVersionUID长1L =;
公共无效的init(){
    S =新船(10,50);
    启动=新按钮(START);
    this.setSize(800,400);
    this.setBackground(Color.black);
    this.add(开始);
    start.addActionListener(本);
}公共无效漆(图形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 = javax.swing.Timer中新(30岁,新的ActionListener(){
    公共无效的actionPerformed(ActionEvent的五){        s.setx(s.getx()+ 2);
        重绘();
    }
});公共无效键pressed(KeyEvent的E){
}公共无效调用keyReleased(KeyEvent的为arg0){
}公共无效的keyTyped(KeyEvent的为arg0){
}公共类船舶{
    诠释XCOR,ycor;    公共船(INT X,int y)对{
        XCOR = X; ycor = Y;
    }
    公共无效SETX(INT X){
        XCOR = X;
    }
    公共无效SETY(int y)对{
        ycor = Y;
    }
    公众诠释的getX(){
        返回XCOR;
    }
    公众诠释的getY(){
        返回ycor;
    }
}@覆盖
公共无效的actionPerformed(ActionEvent的X){
    如果(x.getSource()==开始)
        重置();
        t.start();}
公共无效复位(){
    s.setx(10); s.sety(50);
}}


解决方案

这是结果的 Applet的不会开始缓冲以任何方式。

您应该使用某种双缓冲...

例如...

  @覆盖
公共无效漆(图形G){
    BufferedImage的双向=新的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(双向,0,0,这一点);}

现在,因为它是pretty安全的假设小程序的大小不会发生很大的变化,您可以创建的BufferedImage 和重用的类实例它,例如...

 私人的BufferedImage backingBuffer;@覆盖
公共无效无效(){
    backingBuffer = NULL;
    super.invalidate();
}@覆盖
公共无效漆(图形G){
    如果(backingBuffer == NULL){
        backingBuffer =新的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,这一点);}

我也质疑是否有必要/使用从 Applet的延伸。如果你使用 JApplet的,你可以使用的JP​​anel 为你基地的画布,覆盖它的的paintComponent 方法,并通过了Swing API获得自动双缓冲...

I have a Java Applet that is flickering constantly for me every time the GUI is redrawn. Most people don't see the flickering. Others are able to run the applet without any problems at all but I am not.

I am not sure if this is a problem with the way in which I am redrawing the UI or if this is a problem with my JRE or what. I've exhausted myself looking up other people's similar problems but I have not yet seen an answer that applies here.

I have shortened the applet I am running down to a relatively small bit. This is just a rocket ship being moved to the right, pretty simple and (for me) it flickers constantly.

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);
}

}

解决方案

This is a result of the Applet not begin buffered in any way.

You should employ some kind of double buffering...

For example...

@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);

}

Now, because it's pretty safe to assume the size of the applet won't change a lot, you could create a class instance of the BufferedImage and reuse it, for example...

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);

}

I would also question the need/use of extending from an Applet. If you were to use JApplet, you could use a JPanel as you base canvas, override it's paintComponent method and gain automatic double buffering via the Swing API...

这篇关于运行时,Java小程序闪烁? (不是大家看到闪烁)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆