Apple Applet update issues
I have a Java applet (JApplet). This applet uses JComponent (PanelAux) to display values ββand images that change periodically. But the applet is not updated. What can I do to update my applet?
//--------------------------------------------RUN
public void run()
while (true) {
try {
myThread.sleep(1000);
}
catch (InterruptedException e){
}
repaint();
}
}
//--------------------------------------------PAINT
public void paint(Graphics g)
{
dim = new Dimension(image.getWidth(this)+50,image.getHeight(this)+50);
this.resize(dim);
this.setMaximumSize(dim);
this.setMinimumSize(dim);
PanelAux panel = new PanelAux(image); //JComponent
add(panel);
super.paint(g);
}
thank
As a general tip, the Painting in AWT and Swing article describes how you should handle overdrawing in AWT or Swing.
Since you mentioned that you are using JApplet
, a Painting in Swing section will be added here .
In particular, with Swing, instead of overloading a method paint(Graphics g)
, you should use a method instead paintComponent(Graphics g)
, with a call to a superclass method paintComponent
. Quoting from the Paint Methods " section :
Swing programs should override
paintComponent()
instead of overridepaint()
.
This is because the method itself is paint
split into three separate methods, overriding the method paint
means that it will prevent calls paintComponent
to paintBorder
both the paintChildren
current class and its ancestor classes.
Also, to call a method, run()
yours JApplet
must implement Runnable
, and also have a new Thread
one called somewhere inside your applet. (Probably in methods init
or start
.)
Edit:
It should also be noted that the method paintComponent
will be called anytime the screen needs to be refreshed. The method paintComponent
will be called multiple times, as saua points out , it would not be a good idea to create new objects in paintComponent
the method itself.
Also, it looks like the design has a separate thread (since the applet appears to implement the interface Runnable
, which is implied by the presence of the method run
), updating the JComponent
state can take place in the method itself, run
with a method call repaint
if needed.
source to share