I need to do paint a graphics into a nested panel. I am able to paint in a frame but when I add the class for painting into another JPanel, I see nothing.
I know how to create nested panels but when I do it with a panel containing my paint method, I dont see any picture.
For eg I add my panel for painting this way because its supposed to be nested in mypanel. All other nested panels without a paint works perfectly.
mypanel.add(new paintHang());
This is my main frame with a call to the paint. It is able to paint in a frame but when I nest it like above, I see no picture
public class MainFrame extends JFrame {
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable(){
public void run(){
new MainFrame();
}});
}
public MainFrame(){
super("Hangman Game");
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(600,600));
setResizable(false);
setAlwaysOnTop(true);
add(new paintHang());//able to paint
//add(new myPanel()); unable to paint thru nested
pack();
setVisible(true);
}
This is my painting class.
public class paintHang extends JPanel{
public paintHang(){
setBorder(BorderFactory.createLineBorder(Color.black));
}
public void paintHang(Graphics g){
g.fillRect(10, 250, 150, 20);
g.fillRect(40,70,10,200);
g.fillRect(40,70,60,10);
g.setColor(Color.yellow);
g.fillRect(95,70,5,25);
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString("This is my custom Panel!",10,20);
paintHang(g);
}
}