For Programmers: Free Programming Magazines  


Home > Archive > Java Help > June 2005 > applet paint() refresh problems









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author applet paint() refresh problems
foo

2005-05-27, 8:58 pm

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

2005-06-02, 4:01 am

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.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com