Huuum, well I am not 100% sure exactly what you are trying to achieve, but I will explain some concepts to you and give you some sample code and I think it will help you.
When you have a JComponent or JPanel you can override the method paintComponent. This method is called whenever the component needs to be redrawn (which may be in response to a user action or via repaint.) The signature is public void paintComponent (Graphics g) - and you can put things into this method in order to paint them specifically.
For a timer, you can have a thread which will call the method updateUI() every so often. Instead of trying to explain it I will give you this code I have just written - it will display a window (and probably an applet to, but I havent tested it as an applet, I will leave that to you) and every second it will display something new on the screen, every 10 seconds it changes the background colour.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class test extends JApplet
{
displayer applet;
JFrame f;
DrawingPanel dp;
Container cp;
static boolean fInBrowser =true; // assumes standalone program for now
public static void main(String args[])
{
test t=new test();
}
public test()
{
applet = new displayer ();
dp=new DrawingPanel();
f = new JFrame ("Tester!");
if (!fInBrowser)
{
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
// Add applet to the frame
f.getContentPane ().add (applet);
f.setSize (new Dimension (468,365));
applet.setFocusable(true);
dp.setFocusable(true);
f.setVisible (true);
mytimer a=new mytimer(dp);
applet.setdp(dp);
applet.init ();
a.start();
}
}
class displayer extends JApplet {
DrawingPanel drawing_panel;
public void setdp(DrawingPanel dp)
{
drawing_panel=dp;
}
public void init ()
{
setFocusable(true);
requestFocus();
Container content_pane = getContentPane ();
content_pane.add (drawing_panel);
content_pane.setBackground(Color.black);
}
}
class DrawingPanel extends JPanel
{
int currentstate=0;
DrawingPanel() {}
public void next() {currentstate++;}
public void paintComponent (Graphics g) {
super.paintComponent (g); // Call first to honour other methods
if (currentstate % 100 < 10 || currentstate==0) { setBackground (Color.BLACK);}
if (currentstate % 100 < 20 && currentstate % 100 > 10) { setBackground (Color.RED);}
if (currentstate % 100 < 30 && currentstate % 100 > 20) { setBackground (Color.BLUE);}
if (currentstate % 100 < 40 && currentstate % 100 > 30) { setBackground (Color.WHITE);}
if (currentstate % 100 < 50 && currentstate % 100 > 40) { setBackground (Color.BLUE);}
if (currentstate % 100 < 60 && currentstate % 100 > 50) { setBackground (Color.GRAY);}
if (currentstate % 100 < 70 && currentstate % 100 > 60) { setBackground (Color.GREEN);}
if (currentstate % 100 < 80 && currentstate % 100 > 70) { setBackground (Color.YELLOW);}
if (currentstate % 100 < 90 && currentstate % 100 > 80) { setBackground (Color.RED);}
if (currentstate % 10 == 0) { g.setColor (Color.BLACK);}
if (currentstate % 10 == 1) { g.setColor (Color.RED);}
if (currentstate % 10 == 2) { g.setColor (Color.WHITE);}
if (currentstate % 10 == 3) { g.setColor (Color.BLUE);}
if (currentstate % 10 == 4) { g.setColor (Color.YELLOW);}
if (currentstate % 10 == 5) { g.setColor (Color.GREEN);}
if (currentstate % 10 == 6) { g.setColor (Color.GRAY);}
if (currentstate % 10 == 7) { g.setColor (Color.RED);}
if (currentstate % 10 == 8) { g.setColor (Color.BLUE);}
if (currentstate % 10 == 9) { g.setColor (Color.YELLOW);}
if (currentstate % 5 < 2)
{
g.fill3DRect(40, 100, 400, 50,true);
} else {
g.fill3DRect(200, 60, 50, 200,true);
}
}
}
class mytimer extends Thread
{
private DrawingPanel dp;
public mytimer(DrawingPanel dp)
{
this.dp=dp;
}
public void run()
{
try
{
while(true)
{
dp.next();
dp.updateUI();
sleep(1000); // 1000 = 1 second, 10000 = 10 seconds, 500 = 0.5 seconds, 5000 = 5 seconds etc.....
}
} catch (Exception e) {}
}
}
Its quite basic and, because I haven't got a huge amount of time today, is not great programming style, but it works fine and you get the general idea... in paintComponent you can have whatever you like, which is updated by the thread timer. Change the value in sleep to alter the delay.
Hope this helps, ask if you are unsure about it
Nick