aspose file tools
The moose likes Swing / AWT / SWT and the fly likes repaint() method problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "repaint() method problem" Watch "repaint() method problem" New topic
Author

repaint() method problem

kedar parundekar
Ranch Hand

Joined: May 10, 2006
Posts: 40
/*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

Joined: Jun 09, 2003
Posts: 4632
public void paint(Graphics g)
{
super.paint(g);//<------------------------
g.drawString(str,50,50);
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24041
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
didn't realize JFrame had a paintComponent()
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
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
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
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
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
Sorry, I mean that the only familiar one is JPanel

The others;

AbstractButton, BasicInternalFrameTitlePane, Box, Box.Filler, JColorChooser, JComboBox, JFileChooser, JInternalFrame, JInternalFrame.JDesktopIcon, JLabel, JLayeredPane, JList, JMenuBar, JOptionPane, JPanel, JPopupMenu, JProgressBar, JRootPane, JScrollBar, JScrollPane, JSeparator, JSlider, JSpinner, JSplitPane, JTabbedPane, JTable, JTableHeader, JTextComponent, JToolBar, JToolTip, JTree, JViewport
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: repaint() method problem
 
Similar Threads
Paint in swing
Urgent Help Needed!!!!!! Homework due in 1 hour
Is it a jdk1.4 focus problem, please help !!
problems with repainting the screen
Problem when repainting rectangles