help - draw objects doesn't appear on JFrame
i got this code from a book...
import javax.swing.*;
import java.awt.*;
class Ch5SampleGraphics {
public static void main( String[] args ) {
JFramewin;
Container contentPane;
Graphics g;
win = new JFrame("My First Rectangle");
win.setSize(300, 200);
win.setLocation(100,100);
win.setVisible(true);
contentPane = win.getContentPane();
g = contentPane.getGraphics();
g.drawRect(50,50,100,30);
}
}
the problem is when i run the app, the frame doesn't always show the rect drawn. sometimes it shows it, but usualy not. if it does show the rect, once i click on minimize, then reopen it from the taskbar, the rect will disappear again and will never show up.
can anyone help me? i've been searching for answers but to no avail. thanks 4 reading, hope someone will offer some help.

