【问题标题】:How to update a constantly changing GUI without flicker from update(Graphics g)?如何更新不断变化的 GUI 而不会从更新中闪烁(图形 g)?
【发布时间】:2014-08-08 09:24:28
【问题描述】:

我正在努力尝试在 java 中实现一些七段显示,但我找不到保持 GUI 对象更新而不闪烁的方法。下面是代码:

import java.awt.*;
import javax.swing.*;

public class PPP extends JFrame{  
    static digit atp_hundred = new digit();  //Amount to pay
    static digit atp_ten = new digit();
    static digit atp_unit = new digit();
    static digit atp_tenth = new digit();
    static digit atp_hundreth = new digit(); 
    static digit ltr_ten = new digit();     //Litres
    static digit ltr_unit = new digit();
    static digit ltr_tenth = new digit();
    static digit ppl_hundred = new digit(); //Pence per litre
    static digit ppl_ten = new digit();
    static digit ppl_unit = new digit();
    static digit ppl_tenth = new digit();

    public static void init() {           
        PPP frame = new PPP();   
        frame.setSize(1280,800);
        frame.setVisible(true);
        frame.addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                    System.exit(0);
                }
            });
    }

    public static void main(String arg[]){
        init();
        while (true) {
            for (int i = 0; i < 10; i++) {
                atp_hundred.value = i;
                atp_hundred.update();
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                }
            }
        }
    }

    public PPP(){
        super();
    }

    public void update(Graphics g) {
        paint(g);
    }

    public void paint(Graphics g){
        //this.redraw();
        g.setFont(new Font("Copperplate Gothic Light", Font.PLAIN, 30));
        String title = "Pete's Petrol Pump Simulation vAlpha0.1";
        g.setColor(Color.white);
        g.clearRect(0,0,1280,800);
        g.setColor(Color.black);
        g.drawString(title, (int)(640 - (g.getFontMetrics().getStringBounds(title, g).getWidth() / 2)), 75);
        g.drawRoundRect(20,100,625,400,5,5);
        g.setFont(new Font("Gill Sans MT", Font.PLAIN, 20));
        g.drawString("Amount to Pay: ", 40, 160);
        atpCreate(g, 180, 110);
        //update(g);
        //repaint();
        //System.out.println((g.getFontMetrics().getStringBounds("Amount to Pay", g).getWidth()));
    }

    public void atpCreate(Graphics g, int x, int y) {
        atp_hundred.create(x, y);
        drawPolygons(g, atp_hundred);
        atp_ten.create(x+50,y);
        drawPolygons(g, atp_ten);
        atp_unit.create(x+100, y);
        drawPolygons(g, atp_unit);
        g.drawOval(x+145, y+60, 10,10);
        atp_tenth.create(x+160, y);
        drawPolygons(g, atp_tenth);
        atp_hundreth.create(x+210, y);
        drawPolygons(g, atp_hundreth);
    }

    public void drawPolygons(Graphics g, digit d) {
        if (d.top) {
            g.setColor(Color.green);
            g.fillPolygon(d.ptop);
        } else {
            g.setColor(Color.black);
            g.drawPolygon(d.ptop);
        }
        if (d.topleft) {
            g.setColor(Color.green);
            g.fillPolygon(d.ptopleft);
        } else {
            g.setColor(Color.black);
            g.drawPolygon(d.ptopleft);
        }
        if (d.topright) {
            g.setColor(Color.green);
            g.fillPolygon(d.ptopright);
        } else {
            g.setColor(Color.black);
            g.drawPolygon(d.ptopright);
        }
        if (d.mid) {
            g.setColor(Color.green);
            g.fillPolygon(d.pmid);
        } else {
            g.setColor(Color.black);
            g.drawPolygon(d.pmid);
        }
        if (d.botleft) {
            g.setColor(Color.green);
            g.fillPolygon(d.pbotleft);
        } else {
            g.setColor(Color.black);
            g.drawPolygon(d.pbotleft);
        }
        if (d.botright) {
            g.setColor(Color.green);
            g.fillPolygon(d.pbotright);
        } else {
            g.setColor(Color.black);
            g.drawPolygon(d.pbotright);
        }
        if (d.bot) {
            g.setColor(Color.green);
            g.fillPolygon(d.pbot);
        } else {
            g.setColor(Color.black);
            g.drawPolygon(d.pbot);
        }  
        g.setColor(Color.black);
    }
}

class digit {
    int value = 0;
    boolean top = false;
    boolean topleft = false;
    boolean topright = false;
    boolean mid = false;
    boolean botleft = false;
    boolean botright = false;
    boolean bot = false;
    public void update() {
        switch(value) {
            case 0: top = false;
            topleft = false;
            topright = false;
            mid = false;
            botleft = false;
            botright = false;
            bot = false;
            break;
            case 1: top = false;
            topleft = false;
            topright = true;
            mid = false;
            botleft = false;
            botright = true;
            bot = false;
            break;
            case 2: top = true;
            topleft = false;
            topright = true;
            mid = true;
            botleft = true;
            botright = false;
            bot = true;
            break;
            case 3: top = true;
            topleft = false;
            topright = true;
            mid = true;
            botleft = false;
            botright = true;
            bot = true;
            break;
            case 4: top = false;
            topleft = true;
            topright = true;
            mid = true;
            botleft = false;
            botright = true;
            bot = false;
            break;
            case 5: top = true;
            topleft = true;
            topright = false;
            mid = true;
            botleft = false;
            botright = true;
            bot = true;
            break;
            case 6: top = true;
            topleft = true;
            topright = false;
            mid = true;
            botleft = true;
            botright = true;
            bot = true;
            break;
            case 7: top = true;
            topleft = false;
            topright = true;
            mid = false;
            botleft = false;
            botright = true;
            bot = false;
            break;
            case 8: top = true;
            topleft = true;
            topright = true;
            mid = true;
            botleft = true;
            botright = true;
            bot = true;
            break;
            case 9: top = true;
            topleft = true;
            topright = true;
            mid = true;
            botleft = false;
            botright = true;
            bot = true;
            break;                    
        }
    }
    Polygon ptop = new Polygon();
    Polygon ptopleft = new Polygon();
    Polygon ptopright = new Polygon();
    Polygon pmid = new Polygon();
    Polygon pbotleft = new Polygon();
    Polygon pbotright = new Polygon();
    Polygon pbot = new Polygon();
    public void create(int x, int y) {
        ptop.addPoint(x+10, y);
        ptop.addPoint(x+30, y);
        ptop.addPoint(x+35, y+5);
        ptop.addPoint(x+30, y+10);
        ptop.addPoint(x+10, y+10);
        ptop.addPoint(x+5, y+5);
        ptopleft.addPoint(x+5, y+5);
        ptopleft.addPoint(x+10, y+10);
        ptopleft.addPoint(x+10, y+30);
        ptopleft.addPoint(x+5, y+35);
        ptopleft.addPoint(x, y+30);
        ptopleft.addPoint(x, y+10);   
        ptopright.addPoint(x+35, y+5);
        ptopright.addPoint(x+40, y+10);
        ptopright.addPoint(x+40, y+30);
        ptopright.addPoint(x+35, y+35);
        ptopright.addPoint(x+30, y+30);
        ptopright.addPoint(x+30, y+10);     
        pmid.addPoint(x+10, y+30);
        pmid.addPoint(x+30, y+30);
        pmid.addPoint(x+35, y+35);
        pmid.addPoint(x+30, y+40);
        pmid.addPoint(x+10, y+40);
        pmid.addPoint(x+5, y+35);  
        pbotleft.addPoint(x+5, y+35);
        pbotleft.addPoint(x+10, y+40);
        pbotleft.addPoint(x+10, y+60);
        pbotleft.addPoint(x+5, y+65);
        pbotleft.addPoint(x, y+60);
        pbotleft.addPoint(x, y+40);
        pbotright.addPoint(x+35, y+35);
        pbotright.addPoint(x+40, y+40);
        pbotright.addPoint(x+40, y+60);
        pbotright.addPoint(x+35, y+65);
        pbotright.addPoint(x+30, y+60);
        pbotright.addPoint(x+30, y+40);            
        pbot.addPoint(x+10, y+60);
        pbot.addPoint(x+30, y+60);
        pbot.addPoint(x+35, y+65);
        pbot.addPoint(x+30, y+70);
        pbot.addPoint(x+10, y+70);
        pbot.addPoint(x+5, y+65);          
    }
}

大部分代码只是添加点,并设置七段显示的类型,但如果发生变化,我找不到让它更新的好方法。

谢谢

【问题讨论】:

  • 使用 Swing TImer 代替 Thread.sleep(1000);,不要直接在 JFrame 上绘制 JPanel 并覆盖paintComponent() 而不是paint(),paintComponent( 中的第一个代码行)/paint() 应该是 super.paintComponent()/paint(),否则绘制累积和/或背景不会重新绘制
  • 阅读2D Graphics
  • 不要覆盖 update() 和 paint()。这是在 AWT 中进行自定义绘制时使用的一种技术。 Swing 不使用这种方法。
  • 如何让它不断更新?

标签: java swing jframe paint thread-sleep


【解决方案1】:

您应该使用双缓冲来防止闪烁。请参阅这个 SO 问题:
Java: how to do double-buffering in Swing?

【讨论】:

  • Swing JComponents默认是双缓冲的,闪烁的原因是1.paint()到JFrame,2.通过Thread.sleep(int)杀死paint(),3.使用来自互联网的史前代码(在史前JDK编译的情况下应该可以工作)
  • 我的意思是必须在 BufferedImage 上完成绘图,然后在组件上绘制 BufferedImage,就像这里所做的那样 stackoverflow.com/a/9367756/671988
  • 我认为JFrame默认没有双缓冲。
  • 是的,JFrame 默认有双缓冲。问题是 OP 错误地覆盖了 paint() 方法,正如 mKorbel 所指出的那样。
猜你喜欢
  • 2017-01-29
  • 2011-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-25
  • 1970-01-01
  • 2012-10-27
相关资源
最近更新 更多