0

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. 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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
immanis
  • 70
  • 1
  • 6
  • 3
    use Swing TImer instead of Thread.sleep(1000);, don't to paint directly to JFrame put there JPanel and to override paintComponent() instead of paint(), 1st code line inside paintComponent() / paint() shoule be super.paintComponent() / paint(), otherwise painting cumulated and/or background isn't repainted – mKorbel Jun 18 '14 at 07:32
  • 3
    read [2D Graphics](http://docs.oracle.com/javase/tutorial/2d/index.html) – mKorbel Jun 18 '14 at 07:33
  • Don't override update() and paint(). That is a technique used when doing custom painting in AWT. Swing does not use that approach. – camickr Jun 18 '14 at 15:04
  • How would I get it to constantly update? – immanis Jun 18 '14 at 18:05

1 Answers1

1

You should use double buffering to prevent flicker. See this SO question:
Java: how to do double-buffering in Swing?

Community
  • 1
  • 1
Mohammad Banisaeid
  • 2,376
  • 27
  • 35
  • 3
    Swing JComponents are double buffered by default, flickering are caused by 1. paint() to JFrame, 2. killing paint() by Thread.sleep(int), 3. using prehistoric code from internet (should be works in the case that is compiled in prehistoric JDK) – mKorbel Jun 18 '14 at 07:37
  • I mean the drawing must be done on a `BufferedImage` and then draw that `BufferedImage` on the component as is done here http://stackoverflow.com/a/9367756/671988 – Mohammad Banisaeid Jun 18 '14 at 07:45
  • And I think `JFrame` has no double buffering by default. – Mohammad Banisaeid Jun 18 '14 at 07:48
  • Yes JFrame has double buffering by default. The problem is the that the OP is overriding the paint() method incorrectly as has been pointed out by mKorbel. – camickr Jun 18 '14 at 15:02