943,634 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1663
  • Java RSS
May 5th, 2009
0

painting in a nested panel

Expand Post »
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
Java Syntax (Toggle Plain Text)
  1. public class MainFrame extends JFrame {
  2.  
  3. public static void main(String[] args) {
  4. java.awt.EventQueue.invokeLater(new Runnable(){
  5. public void run(){
  6. new MainFrame();
  7.  
  8. }});
  9. }
  10.  
  11. public MainFrame(){
  12.  
  13. super("Hangman Game");
  14.  
  15. setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  16. setPreferredSize(new Dimension(600,600));
  17. setResizable(false);
  18. setAlwaysOnTop(true);
  19.  
  20.  
  21. add(new paintHang());//able to paint
  22. //add(new myPanel()); unable to paint thru nested
  23.  
  24. pack();
  25. setVisible(true);
  26.  
  27. }

This is my painting class.
Java Syntax (Toggle Plain Text)
  1. public class paintHang extends JPanel{
  2.  
  3. public paintHang(){
  4.  
  5. setBorder(BorderFactory.createLineBorder(Color.black));
  6. }
  7. public void paintHang(Graphics g){
  8. g.fillRect(10, 250, 150, 20);
  9. g.fillRect(40,70,10,200);
  10. g.fillRect(40,70,60,10);
  11. g.setColor(Color.yellow);
  12. g.fillRect(95,70,5,25);
  13. }
  14.  
  15. @Override
  16. public void paintComponent(Graphics g){
  17. super.paintComponent(g);
  18. g.drawString("This is my custom Panel!",10,20);
  19. paintHang(g);
  20. }
  21.  
  22. }
Last edited by number87; May 5th, 2009 at 1:27 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
May 5th, 2009
0

Re: painting in a nested panel

I don't think I understand. There is no myPanel class (presumably it extends JPanel and you haven't posted it). I see no difference between the two lines below other than the fact that the two classes have different names. There must be something in myPanel that is different from paintHang, but since you haven't posted it, I can't speculate what the problem is. The code you posted works, and it worked for you. Please post the myPanel class so we can see what DOESN'T work. I also am unclear what you are referring to by "nested". What is nested in what?

Java Syntax (Toggle Plain Text)
  1. add(new paintHang());//able to paint
  2. //add(new myPanel()); unable to paint thru nested
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
May 5th, 2009
0

Re: painting in a nested panel

i did not post myPanel class because I know that it is working perfectly when I tested it. I cant possibly post my whole code here, unless someone wants to do my homework for me. There is a difference.

add(new myPainthang());

calls directly the class which extends JPanel and does painting.

add(new myPanel());

calls the panel where I nest the class/panel which extends JPanel and does paiting
Last edited by number87; May 5th, 2009 at 2:12 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
May 5th, 2009
0

Re: painting in a nested panel

Click to Expand / Collapse  Quote originally posted by number87 ...
i did not post myPanel class because I know that it is working perfectly when I tested it. I cant possibly post my whole code here, unless someone wants to do my homework for me. There is a difference.

add(new myPainthang());

calls directly the class which extends JPanel and does painting.

add(new myPanel());

calls the panel where I nest the class/panel which extends JPanel and does paiting
They both call constructors of classes which extend JPanel. I don't understand the nesting part. Are you trying to add an instance of myPaintHang to an instance of myPanel, then add that instance of myPanel to an instance of MainFrame? So you have a JPanel within a JPanel within a JFrame? Is that the goal?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
May 5th, 2009
0

Re: painting in a nested panel

yes I knida want to have a Jpanel which paints inside a Jpanel in a JFrame. The painting works when inside just a JFrame but doesnt when I put it inside a JPanel
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
May 5th, 2009
0

Re: painting in a nested panel

Click to Expand / Collapse  Quote originally posted by number87 ...
yes I knida want to have a Jpanel which paints inside a Jpanel in a JFrame. The painting works when inside just a JFrame but doesnt when I put it inside a JPanel
Perhaps have something like this:

Java Syntax (Toggle Plain Text)
  1. myPanel mypanel = new myPanel ();
  2. mypanel.setLayout(new GridLayout (1,1));
  3. mypanel.add (new paintHang ());
  4. this.getContentPane().add (mypanel);

instead of this?

        add(new paintHang());
        //add(new myPanel());

If the paintHang object is supposed to be inside of the myPanel object, add it to the myPanel object rather than the MainFrame object.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
May 5th, 2009
0

Re: painting in a nested panel

I guess I gotta post my whole code to let u guys understand....

Main Frame code:
Java Syntax (Toggle Plain Text)
  1. public class MainFrame extends JFrame {
  2.  
  3. public static void main(String[] args) {
  4. java.awt.EventQueue.invokeLater(new Runnable(){
  5. public void run(){
  6. new MainFrame();
  7.  
  8. }});
  9. }
  10.  
  11. public MainFrame(){
  12.  
  13. super("Hangman Game");
  14.  
  15. setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  16. setPreferredSize(new Dimension(600,600));
  17. setResizable(false);
  18. setAlwaysOnTop(true);
  19.  
  20. add(new MainPanel());
  21. pack();
  22. setVisible(true);
  23.  
  24. }
  25.  
  26.  
  27. }

Main Panel Code:

Java Syntax (Toggle Plain Text)
  1. public class MainPanel extends JPanel implements ActionListener{
  2. private Button a[];
  3. private JButton newGame, loadFileDir, quit;
  4. private JRadioButton onePlayer, twoPlayer;
  5. private JComboBox diff;
  6.  
  7. private paintHang draw = new paintHang();
  8.  
  9. public MainPanel (){
  10.  
  11. super();
  12.  
  13. //create menu panel
  14. JPanel menuPanel = new JPanel();
  15. menuPanel.setBorder(BorderFactory.createTitledBorder("Menu"));
  16. menuPanel.setBackground(Color.orange);
  17. menuPanel.setPreferredSize(new Dimension(150, 550));
  18.  
  19. //create game display panel
  20. JPanel gamePanel = new JPanel();
  21. gamePanel.setBorder(BorderFactory.createTitledBorder("Game Display"));
  22. gamePanel.setBackground(Color.white);
  23. gamePanel.setPreferredSize(new Dimension(400, 550));
  24.  
  25. //create game status panel
  26. JPanel statusPanel = new JPanel();
  27. statusPanel.setBorder(BorderFactory.createTitledBorder("Game Status"));
  28. statusPanel.setBackground(Color.yellow);
  29. statusPanel.setPreferredSize(new Dimension(150, 150));
  30.  
  31. //main panel bg color
  32. setBackground(Color.black);
  33.  
  34. //Text labels for game status
  35. JLabel difficulty = new JLabel("Difficulty: ");
  36. JLabel tries = new JLabel("Tries Remaining: ");
  37. JLabel hint = new JLabel("Hint: ");
  38. JLabel currLevel = new JLabel("Level: ");
  39.  
  40. //create game buttons
  41. newGame = new JButton("New Game");
  42. loadFileDir = new JButton("Load Filedir..");
  43. quit = new JButton("Quit");
  44. newGame.setPreferredSize(new Dimension(100,20));
  45. newGame.setToolTipText("Start New Game");
  46. newGame.addActionListener(this);
  47. loadFileDir.setPreferredSize(new Dimension(100,20));
  48. loadFileDir.setToolTipText("Choose File dir to load words");
  49. loadFileDir.addActionListener(this);
  50. quit.setPreferredSize(new Dimension(100,20));
  51. quit.setToolTipText("Exit game");
  52. quit.addActionListener(this);
  53.  
  54. //radio buttons for one/two player modes
  55. onePlayer = new JRadioButton ("One Player", true);
  56. twoPlayer = new JRadioButton ("Two Players");
  57. onePlayer.addActionListener(this);
  58. twoPlayer.addActionListener(this);
  59.  
  60. //group radio buttons
  61. ButtonGroup group = new ButtonGroup();
  62. group.add (onePlayer);
  63. group.add (twoPlayer);
  64.  
  65. //combo box for difficulty settings
  66. String[] level = {"Level 1", "Level 2", "Level 3", "AutoLevel"};
  67. diff = new JComboBox (level);
  68. diff.setSelectedIndex(-1);
  69. diff.setPreferredSize(new Dimension(20,20));
  70. diff.setSize(new Dimension(20,20));
  71.  
  72. //create a button keyboard in game display
  73. a = new Button[26];
  74.  
  75. StringBuffer buffer;
  76. for (int i = 0; i <26; i++) {
  77. buffer = new StringBuffer();
  78. buffer.append((char)(i+65));
  79. a[i] = new Button(buffer.toString());
  80. a[i].addActionListener(this);
  81. gamePanel.add(a[i]);
  82.  
  83. }
  84.  
  85. //menu panel, buttons, box layout
  86. menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.PAGE_AXIS));
  87. menuPanel.add (Box.createRigidArea(new Dimension (0, 5)));
  88. menuPanel.add (newGame);
  89. menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
  90. menuPanel.add (loadFileDir);
  91. // menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
  92. // menuPanel.add (difficulty);
  93. // menuPanel.add (diff);
  94. menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
  95. menuPanel.add (onePlayer);
  96. menuPanel.add (twoPlayer);
  97. menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
  98. menuPanel.add (quit);
  99. menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
  100.  
  101. //game status panel with labels, box layout
  102. statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.PAGE_AXIS));
  103. statusPanel.add (currLevel);
  104. statusPanel.add (Box.createRigidArea(new Dimension (0, 5)));
  105. statusPanel.add (hint);
  106. statusPanel.add (Box.createRigidArea(new Dimension (0, 5)));
  107. statusPanel.add (tries);
  108. statusPanel.add (Box.createVerticalStrut(320));
  109.  
  110. gamePanel.add(draw);
  111.  
  112. menuPanel.add(statusPanel);
  113. add(menuPanel);
  114. add(gamePanel);
  115.  
  116. }

and finally the painting class:

Java Syntax (Toggle Plain Text)
  1. public class paintHang extends JPanel{
  2.  
  3. public paintHang(){
  4.  
  5. setBorder(BorderFactory.createLineBorder(Color.black));
  6. }
  7. public void paintHang(Graphics g){
  8. g.fillRect(10, 250, 150, 20);
  9. g.fillRect(40,70,10,200);
  10. g.fillRect(40,70,60,10);
  11. g.setColor(Color.yellow);
  12. g.fillRect(95,70,5,25);
  13. }
  14.  
  15. @Override
  16. public void paintComponent(Graphics g){
  17. super.paintComponent(g);
  18. g.drawString("This is my custom Panel!",10,20);
  19. paintHang(g);
  20. }
  21.  
  22. }

So now that my whole code is here, someone tell me why I dont see any graphics painted in my nested panel.
Last edited by number87; May 5th, 2009 at 2:48 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
May 5th, 2009
0

Re: painting in a nested panel

It almost certainly has to do with the fact that you have not selected a LayoutManager for gamePanel and you have not selected a minimum size for hangPanel, so it's setting the size of hangPanel to be very small. Pick a layout manager for gamePanel. I'd split gamePanel in two: one panel for the buttons, one panel for painting what you want to paint (hangPanel). Use a BorderLayout and add the button panel to the top of gamePanel, then the larger bottom part of gamePanel would be hangPanel. You can set vertical sizes that way that will be respected by the LayoutManager. Not specifying a LayoutManager can cause default behavior that you don't want.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: making a quiz game
Next Thread in Java Forum Timeline: bucketsort





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC