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.Timer Task;
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.addWindowList ener(new WindowAdapter() {
public void windowClosing(W indowEvent e) {System.exit(0) ;}
});
JApplet applet = new Shapes();
f.getContentPan e().add("Center ", applet);
applet.init();
f.pack();
f.setSize(new Dimension(550,1 00));
f.show();
}
}
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.Timer Task;
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.addWindowList ener(new WindowAdapter() {
public void windowClosing(W indowEvent e) {System.exit(0) ;}
});
JApplet applet = new Shapes();
f.getContentPan e().add("Center ", applet);
applet.init();
f.pack();
f.setSize(new Dimension(550,1 00));
f.show();
}
}
Comment