• Post Reply Bookmark Topic Watch Topic
  • New Topic

repaint() method problem

 
kedar parundekar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*Code: */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Demoswing extends JFrame implements ActionListener
{
JButton b1,b2;
String str;
Demoswing()
{
str="";
b1=new JButton("Rect");
b2=new JButton("Circ");

Container cp= getContentPane();
cp.setLayout(new FlowLayout());
b1.addActionListener(this);
b2.addActionListener(this);
cp.add(b1);
cp.add(b2);

}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
str="Rectangle";

}
if(ae.getSource()==b2)
{
str="Circle";
}
repaint();

}

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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void paint(Graphics g)
{
super.paint(g);//<------------------------
g.drawString(str,50,50);
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Dunn:

super.paint(g);//<------------------------


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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
didn't realize JFrame had a paintComponent()
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, sorry for forgetting the tell the class that the above method belongs to.
It is kept in Graphics class.
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!