Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

applet paint() refresh problems

Thread view: 
foo - 27 May 2005 21:48 GMT
the following applet is supposed to draw random lines on the screen, but
doesn't. does anyone know how to make it refresh?

thanks in advance

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.text.*;
import java.lang.*;

public class UI2 extends Applet{
 private int x1,y1,x2,y2;

 public void init() {
   for(;;) {
     x1=(int)(Math.random()*100);
     y1=(int)(Math.random()*100);
     x2=(int)(Math.random()*100);
     y2=(int)(Math.random()*100);
     repaint();
   }
 }

 public void paint(Graphics g) {
   g.setColor(Color.black);

   g.drawLine(x1, y1, x2, y2);
 }

 public void update(Graphics g) {
   paint(g);
 }

}
sanjay manohar - 02 Jun 2005 02:08 GMT
try doing your for loop in a new thread. at the moment, it blocks the
initialisation of the applet.

public void init(){
Thread t = new Thread(this);
 t.start();
}
public void run(){
for(;;){
 // etc....
}
}

PS - do you want lines that have been drawn to remain on the screen or
not?
if not, call super.paint(g) in the paint method before you draw.
if so, you may want to store the coordinates of the lines and repaint
them.
Dave - 23 Jun 2005 18:46 GMT
Adding a few more details to Sanjay's response...

Like Sanjay said, the endless loop in your init() method prevents init()
from completeing.  This prevents a number of things from happening,
including preventing your paint() method from being called by the Applet
code.

Your Applet is not fully instantiated until init() runs to completion.  
Until the Applet is instantiated, paint() can not be called.  Once
init() is done, the Applet class code will call your start() method.  
Only you do not have a start method (you should have one).

Rename your init() method to start(), and it will work.

Without understanding how Applets work this is a hard problem to
understand.  Try reading the file java.applet.Applet.html (included in
your JDK).  Grab some Applet example code from the web to learn about
other things you need to know and do.  Or a book.  Whatever.

To debug things like this yourself, try adding println() calls to the
begining and end of every method (when you have a small number of
methods), like this:
  System.out.println("-> UI2 init");  // Going into init()
  System.out.println("<- UI2 init");  // Leaving init()

Carefully reading the output can help you understand what went wrong, so
you can debug code faster on your own.

Add as many println() calls as you need to help figure it out.

--Dave

Java__Dave@NOSPAM_Hotmail.com

Remove NOSPAM_ to reply...
The 2 underscores in Java__Dave are required...


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.