如何在不更新闪烁(图形g)的情况下更新不断变化的GUI?

时间:2014-06-18 07:28:43

标签: java swing jframe paint thread-sleep

我正在努力尝试在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);          
    }
}

大多数代码只是添加点,并为七段显示设置类型,但是如果有变化,我找不到更新它的好方法。

由于

1 个答案:

答案 0 :(得分:1)

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