refreshing 2D Graphics screen problem

From: edward hage (edha_at_xs4all.nl)
Date: 01/13/04

  • Next message: Silvio Bierman: "Re: refreshing 2D Graphics screen problem"
    Date: Tue, 13 Jan 2004 23:38:26 +0100
    
    

    The following program is an applet that reads a number from a file 10
    times per second. It needs to be shown graphically.
    The file monitors a procesvariable and changes continuously. It is read
    okay, and also displays correct on screen (this in the form of a box
    that is filled with a certain % corresponding to the value in the file.)

    However, the screen only gets the initial value right. The problem I
    encounter is that the display is not updated. This is because the method
      paint is only performed at init, and for example when I drag the
    window (appletviewer) of the screen. That part of the screen is updated.

    My question: How can I make sure that every time a value is written,
    that the method paint is performed. Can I make a link in class
    RemindTask to paint. I tried some things but this did not work. I am a
    newbie at Java. Can somebody help me out?

    Thanks in advance, Edward Hage

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.Timer;
    import java.util.TimerTask;

    public class Shapes extends JApplet {
         Timer timer;
         Double showvalue;
       // some more things //

         public void init() {
                 timer = new Timer();
            timer.schedule(new RemindTask(), 0, 100);
         }

         class RemindTask extends TimerTask {

            public void run() {
            String d;
            d = ReadTopLine();
            showvalue = Double.valueOf(d);
            }

            private String ReadTopLine() {
            File inputFile = new File("showfile.txt");
            // etc.
            return string read from file
            //
            }

           public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D)g;
            // etc. draw a box filled with
            value showvalue //
            }

         public static void main(String s[]) {
             JFrame f = new JFrame("Shapes");
             f.addWindowListener(new WindowAdapter() {
                 public void windowClosing(WindowEvent e) {System.exit(0);}
             });

            JApplet applet = new Shapes();
             f.getContentPane().add("Center", applet);
             applet.init();
            f.pack();
             f.setSize(new Dimension(550,100));
             f.show();
         }

    }


  • Next message: Silvio Bierman: "Re: refreshing 2D Graphics screen problem"

    Relevant Pages

    • Re: Simple Problem - Question from Book
      ... Sonia, ... >>painting is handled by the paint method, which is turn calls things like ... > that will help an elementary school student learn multiplication. ... > on the applet and ask another multiplication question. ...
      (comp.lang.java.help)
    • Re: JPanel not visible in JApplet
      ... drawing is done by paint() and not init(). ... called when your applet is first invoked. ... the the Panel was painted. ...
      (comp.lang.java)
    • Re: how and when components get their own size?
      ... First access of the applet is working fine. ... without closing the browser) and hitting again the applet's page the applet crashes because ... Displaying applet's page always causes init() and start, ... them in paint() or paintComponentmethod of custom component. ...
      (comp.lang.java.gui)
    • Re: Double buffer help
      ... "Painting in AWT and Swing": ... I simply duplicated the code from my paint() method in the update ... applet and the new improved applet can both be seen in action. ... Then both paintand updatemethods merely call my new displayRedraw() ...
      (comp.lang.java)