Drawing Objects Dynamically

I want to draw rectangles dynamically (in a 'for' iterative)I have built this in an applet but when it draws the rectangles, they just vanish.Is this something really fundamental ( I am a business language programmer - new to OOP and Java)!!Cheers
[284 byte] By [yllk] at [2007-9-26 1:26:07]
# 1
How do you draw the rectangles? In the paint()-method?
jsalonen at 2007-6-29 1:08:34 > top of Java-index,Archived Forums,Java Programming...
# 2
Well where else would you paint them?The problem is, that in paint method the screen is cleared, so basically you have to paint them again everytime paint is called, or override update and not clear the screen.
Kayaman at 2007-6-29 1:08:35 > top of Java-index,Archived Forums,Java Programming...
# 3

Well, the OP could be erroneously drawing with the Graphics object returned by getGraphics, which works until repaint() is called. Many make that mistake...

It's really hard to try to figure out the cause of the problem, let alone a solution, without seeing the actual source code (hint!!).

jsalonen at 2007-6-29 1:08:35 > top of Java-index,Archived Forums,Java Programming...
# 4

Sorry - not been able to log on - thank you for your replys. Here's a couple of examples of code - the first one works, the second doesn't.

public class Cpan extends Applet{

public void init(){

setBackground(Color.white);

}

public void paint(Graphics g) {

g.setColor(Color.black);

//Draw Sqaures

g.drawRect(0, 0, 30, 30);

g.drawRect(30, 0, 30, 30);

g.drawRect(60, 0, 30, 30);

g.drawRect(90, 0, 30, 30);

g.drawRect(120, 0, 30, 30);

}

}

Above is extended to complete a 5*5 grid

Second:

public class Cpan2 extends Applet{

int x = 0;

int y = 0;

public void init(){

setBackground(Color.white);

}

public void paint(Graphics g) {

g.setColor(Color.black);

for (int a=0; a<5; a++){

for (int b=0; b<5; b++){

g.drawRect(x, y, 30, 30);

x = x +30;

}

x = 0;

y = y + 30;

}

Any ideas?

yllk at 2007-6-29 1:08:35 > top of Java-index,Archived Forums,Java Programming...
# 5

'y' in the second one is newer reset to 0. Try this:public void paint(Graphics g) {

g.setColor(Color.black);

int x=0; y=0;

for (int a=0; a<5; a++){

for (int b=0; b<5; b++){

g.drawRect(x, y, 30, 30);

x = x +30;

}

x = 0;

y = y + 30;

}

}

jsalonen at 2007-6-29 1:08:35 > top of Java-index,Archived Forums,Java Programming...