• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need help using the paintComponent method

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the paintComponent(Graphics g)

add.

super.paintComponent(g);


Justin
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help; putting the panel in the frame worked! Thanks to all for your help.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Dunn wrote: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()



what difference is there between

and

i have just started to read head first java.that is why i a asking to clarify my understanding.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't hijack old postings. This posting is about "painting". Your question has nothing to do with painting.

Read the section from the Swing tutorial on Using Top Level Containers which will help explain the structure of a JFrame and its child components. The Swing tutorial is probably a better reference than Head First Java because it is more current.

In the first case you are replacing the content pane with your panel.

In the second case you are adding your panel to the content pane.

Effectively they are the same because you can still add components to the frame however you want.

Look at the examples in the Swing tutorial to see which approach is used most often.
 
reply
    Bookmark Topic Watch Topic
  • New Topic