简体   繁体   English

如何更新不断变化的GUI而不导致update(Graphics g)闪烁?

[英]How to update a constantly changing GUI without flicker from update(Graphics g)?

I'm working on trying to get some seven segment displays going in java, but I cannot find a way to keep the GUI objects updating, without it flickering. 我正在尝试在Java中运行大约七个分段显示,但是我找不到一种方法来保持GUI对象更新,而不会闪烁。 Here is the code below: 这是下面的代码:

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

Most of the code is just adding points, and setting up the type for the seven segment display, but I can't find a good way of having it update if something changes. 大多数代码只是添加点,并设置七段显示的类型,但是我找不到找到改变它的更新方式的好方法。

Thanks 谢谢

You should use double buffering to prevent flicker. 您应该使用双重缓冲来防止闪烁。 See this SO question: 看到这样的问题:
Java: how to do double-buffering in Swing? Java:如何在Swing中进行双缓冲?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM