Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

repaint?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Does anyone know how to make a class (that extends Canvas) to keep repainting automatically (in order to display new values) whenever someone invoke that action from the main class.

I have part of the code here which doesnt really work:
------------------------------------------------------
public class Diagram extends Canvas
{ ....

public void paint(Graphics g)
{
new Diagram(n.getName());
display(g);
}
....
}

------------------------------------------------------

When i debug, it does retrieve the new values each time someone invoke that action but it doesnt repaint the canvas with the new values, the initial values are always stay visible.

Thank you.

 
(Sorry for any bugs in the following, but I can't test it at the moment.)
Try something like:
(??? is the class of n)
Code:
public class Diagram extends Canvas
{
 Thread paintThread;
 ??? nVal;

 ...

 public void paint(Graphics g)
 {
  // create  and start the painting thread
  // if it does not exist
  if (paintThread==null || !paintThread.isAlive())
  {

   // the painting thread
   paintThread = new Thread()
   {
    boolean doRun = true;
    public void run()
    {
     while(doRun)
     {
      paint(g);
      try { sleep(100); } catch(Exception eSleep) {}
     }
    }
    public void endPaint()
    {
     doRun = false;
    }
   };

   paintThread.start();

  }

  // otherwise paint the values if the
  // name is new

  else if (nVal==null ||
    !(nVal.getName()).equals(n.getName()))
  {
   new Diagram(n.getName());
   display(g);
  }

  // method to stop the painting
  public void stopPaint()
  {
   paintThread.endPaint();
  }
 ....
}

I hope this helps.
-HavaTheJut
 
Already found one :)

Code:
else if (nVal==null ||
    !(nVal.getName()).equals(n.getName()))
  {
   new Diagram(n.getName());
   display(g);
nVal = n;
Code:
  }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top