Code Comments
Programming Forum and web based access to our favorite programming groups.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);
}
}
Post Follow-up to this messagetry 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.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.