public void paint(Graphics g) { g.drawString(str,50,50); } public static void main(String[] args) { Demoswing obj=new Demoswing(); obj.setSize(100,100); obj.show();
} }
Q : when I press "Rect" button then it displaying "Rectangle" String at 50,50 position & then if I press "Circ" button then it displaying "Circle" String at same position but overwritting on "Rectangle" string. My question is how to clear "Rectangle" String first & then display "Circle" string on same position.
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
public void paint(Graphics g) { super.paint(g);//<------------------------ g.drawString(str,50,50); }
Ernest Friedman-Hill
author and iconoclast
Marshal
Close, although really not the best solution. You shouldn't override paint(), but rather paintComponent(); and when you do, you should call super.paintComponent() first. Overriding paint() in a Swing component just makes a mess of things.
Try to use the following method to cear all the Frame screen: clearRect(int x, int y, int width, int height)
memati bas
Ranch Hand
Joined: Jan 29, 2006
Posts: 85
posted
0
Oh, sorry for forgetting the tell the class that the above method belongs to. It is kept in Graphics class.
memati bas
Ranch Hand
Joined: Jan 29, 2006
Posts: 85
posted
0
Originally posted by Ernest Friedman-Hill:
Close, although really not the best solution. You shouldn't override paint(), but rather paintComponent(); and when you do, you should call super.paintComponent() first. Overriding paint() in a Swing component just makes a mess of things.
I think he is correct by suggesting paint method for drawing. Because, I know that paintComponent(Graphics g) should be used for Java Swing components, not containers. If you draw something on JPanel or JDialog, use paintComponent(Graphics g), otherwise use paint(Graphics g).
memati bas
Ranch Hand
Joined: Jan 29, 2006
Posts: 85
posted
0
Sorry I said that JDialog has paintComponent(Graphics g) method but it does not have. Only, JPanel has paintComponent(Graphics g) method
memati bas
Ranch Hand
Joined: Jan 29, 2006
Posts: 85
posted
0
Sorry, I mean that the only familiar one is JPanel