By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,032 Members | 825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,032 IT Pros & Developers. It's quick & easy.

refreshing 2D Graphics screen problem

P: n/a
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();
}

}

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Look in RemindTask run method:

"edward hage" <ed**@xs4all.nl> wrote in message
news:40*********************@news.xs4all.nl...
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();
double prev = showvalue;
showvalue = Double.valueOf(d);
if (showvalue != prev) repaint();
}

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

}

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.