Paint Method Help

Hello Everyone,

I am working for the first time with applets. And I'm trying to understand the

paint method. I am trying to create an applet that draws 100 lines, randomly placed throughout the applet boundaries, pausing between each one. When I call repaint(); at the end, is that erasing the line that i just drew to redraw another one? How do I make them all stay on the screen? Below is my code.. don't make fun haha

import java.awt.*;

import java.util.*;

import java.lang.*;

publicclass assn3extends java.applet.Applet{

int randx, randy, randx2, randy2, count = 0;

Random generator =new Random();

/** Initialization method that will be called after the applet is loaded

* into the browser.

*/

publicvoid init(){

}

publicvoid paint(Graphics g){

g.setColor(Color.red);

if (count < 100){

randx = generator.nextInt(700);

randy = generator.nextInt(700);

randx2 = generator.nextInt(700);

randy2 = generator.nextInt(700);

try{

Thread.sleep(7000);

}catch ( InterruptedException e ){

// do nothing

}

g.drawLine(randx, randy, randx2, randy2);

count++;

}

else{

g.setColor(Color.white);

g.fillRect(0, 0, bounds().width, bounds().height);

count = 0;

}

repaint();

}

}

[2353 byte] By [EnSanitya] at [2007-11-27 2:33:44]
# 1

Firstly, change your IF into a WHILE

Change this:

if (count < 100)

into this:

while (count < 100)

###############################

import java.awt.*;

import java.util.*;

import java.lang.*;

public class assn3 extends java.applet.Applet {

int randx, randy, randx2, randy2, count = 0;

Random generator = new Random();

/** Initialization method that will be called after the applet is loaded

* into the browser.

*/

public void init() {

}

public void paint(Graphics g) {

g.setColor(Color.red);

while (count < 100){

randx = generator.nextInt(700);

randy = generator.nextInt(700);

randx2 = generator.nextInt(700);

randy2 = generator.nextInt(700);

try {

Thread.sleep(7000);

} catch ( InterruptedException e ) {

// do nothing

}

g.drawLine(randx, randy, randx2, randy2);

count++;

}

/*

else{

g.setColor(Color.white);

g.fillRect(0, 0, bounds().width, bounds().height);

count = 0;

}

*/

repaint();

}

}

Java_Lavaa at 2007-7-12 2:50:37 > top of Java-index,Desktop,Core GUI APIs...
# 2
awesome, yes the loop works... however i was wondering.. is there a way to do it without the loop, by just recalling paint?
EnSanitya at 2007-7-12 2:50:37 > top of Java-index,Desktop,Core GUI APIs...