wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes need help using the paintComponent method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "need help using the paintComponent method" Watch "need help using the paintComponent method" New topic
Author

need help using the paintComponent method

Todd Swift
Greenhorn

Joined: Apr 11, 2006
Posts: 5
Hi, I'm reading the Head First Java book and am having problems using the example regarding the paintComponent method (pg. 364 chapter 12). I've made a subclass of JPanel but cannot seem to call the paintComponent method. I've pasted my code below; any tips would be appreciated!

-----------------------------------------------------------
package mydrawpaneltest;

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

public class MyJPanel extends JPanel {

/** Creates a new instance of MyJPanel */
public MyJPanel() {
}

public void paintComponent(Graphics g) {

g.setColor(Color.orange);
g.fillRect(20, 50, 100, 100);

}
}


----------------------------------------------------------

package mydrawpaneltest;

public class Main {

public Main() {
}

public static void main(String[] args) {

MyJPanel myJPan = new MyJPanel();
myJPan.repaint();
}
}
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
in the paintComponent(Graphics g)

add.

super.paintComponent(g);


Justin


You down with OOP? Yeah you know me!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if that is all of your code, the panel needs to be in a frame/contentPane
and for the frmae to be visible.

try this for your Main()

Todd Swift
Greenhorn

Joined: Apr 11, 2006
Posts: 5
Thanks for your help; putting the panel in the frame worked! Thanks to all for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help using the paintComponent method
 
Similar Threads
Printing a JPanel
problem with paint methods
KeyListener() is sometimes deaf
Applet Login via database
JTable problem: not visible