Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with drawing Shapes on a jFrame

843810Jan 26 2004 — edited Feb 5 2004
I am doing a program that needs to draw multiple rectangels and circles of the same size. So i placed a loop inside "void paint(Graphics g)" method. What happened is that when i resize the frame, or place another window on top of the shapes drawn on the screen, they disapear. The funny thing is that if a shape is drawn outside the loop, resizing the window or placing something on top of it will not effect it.

The following is a sample code to demonstrate the problem, in case i faled to explaine the problem in a good maner. (forgive me if i made any spelling or gramar mistakes. English is not my native language).

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DrawingFrametest extends JFrame{

int x = 25;
int y = 50;
int distance = 10;

public void paint( Graphics g ){
//These rect's are getting erased.
for(int i=0; i<=10; i++){
g.drawRect(x, y, 20, 20);
g.fillRect(x, y, 20, 20);
y= y+distance+30;
}
//This rect is having no problem
g.drawRect(190, 100, 47, 47);
}

public static void main (String args[]){
JFrame frame = new DrawingFrametest();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize( 950, 700 );
frame.show();
}
}

Any help will be appreciated.
Thank you.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 4 2004
Added on Jan 26 2004
3 comments
128 views