Programming, website development forum Get latest updates by RSS Follow TechnicalTalk on Twitter Follow TechnicalTalk on Facebook 
HomeSearchRecent PostsLoginRegisterContact Us

Username  
Password    
  Forgot your password?  

Pages: [1]   Go Down
 
  Email this topic  |  Print
0 Members and 1 Guest are viewing this topic.

Automatically redrawing graphics...?

 
webmaster forum
Anchor  Offline
Activity
0%
 
New Coder
Posts: 20
Topics: 16
January 14, 2009, 09:45:04 AM

I need to make an applet that draws a shape and then automatically draws another shape and then automatically do another of a different design. I don't know what the "automatically" is supposed to mean but I'm guessing a timer? Would that make sense? I've been looking and I've read that some event needs to happen to start the timer. I don't get what the "event" would be to start a timer in this case. Is there any way to make a simple timer that doesn't need anything to happen to run it?
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
January 14, 2009, 10:30:23 AM

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.

Code:
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

« Last Edit: January 14, 2009, 10:32:10 AM by polas »

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
Admin  Offline
*
 
Code Guru
Location: India
Gender: Male
Posts: 1387
Topics: 105
NaviBuster NaviBuster
WWW
May 15, 2010, 09:06:03 PM

Hope the information here has helped you.
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.15 | SMF © 2011, Simple Machines


Google visited last this page January 13, 2012, 07:03:46 AM