Programming and Webmasters forum
HomeSearchRecent PostsLoginRegister Contact Us

Username  
Password
Announcing 14th Weekly Contest - From 25 July To 01 August.

Win every week on this forum.

Chek out How To Win?
 

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

Automatically redrawing graphics...?

 
webmaster forum
Anchor  Offline
Contest Points: 100
 
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
*
 
Hacker
Gender: Male
Posts: 1224
Topics: 78
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
Skydive in North East England
 
webmaster forum
Admin  Offline
*
 
Hacker
Location: India
Gender: Male
Posts: 1101
Topics: 94
Technical_Talk
WWW
May 15, 2010, 09:06:03 PM

Hope the information here has helped you.

Watch out for the latest Weekly Contests | Contest Rules
A Game - Say "Hello"
We are looking for Global Moderator
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC


Google visited last this page July 27, 2010, 08:12:00 AM

Valid XHTML 1.0 Transitional     Valid XHTML 1.0 Transitional