| | |
painting in a nested panel
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
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
This is my painting class.
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)
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.
Java Syntax (Toggle Plain Text)
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); } }
Last edited by number87; May 5th, 2009 at 1:27 am.
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
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)
add(new paintHang());//able to paint //add(new myPanel()); unable to paint thru nested
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
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
. 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.
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
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
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
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
Java Syntax (Toggle Plain Text)
myPanel mypanel = new myPanel (); mypanel.setLayout(new GridLayout (1,1)); mypanel.add (new paintHang ()); 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.
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
I guess I gotta post my whole code to let u guys understand....
Main Frame code:
Main Panel Code:
and finally the painting class:
So now that my whole code is here, someone tell me why I dont see any graphics painted in my nested panel.
Main Frame code:
Java Syntax (Toggle Plain Text)
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 MainPanel()); pack(); setVisible(true); } }
Main Panel Code:
Java Syntax (Toggle Plain Text)
public class MainPanel extends JPanel implements ActionListener{ private Button a[]; private JButton newGame, loadFileDir, quit; private JRadioButton onePlayer, twoPlayer; private JComboBox diff; private paintHang draw = new paintHang(); public MainPanel (){ super(); //create menu panel JPanel menuPanel = new JPanel(); menuPanel.setBorder(BorderFactory.createTitledBorder("Menu")); menuPanel.setBackground(Color.orange); menuPanel.setPreferredSize(new Dimension(150, 550)); //create game display panel JPanel gamePanel = new JPanel(); gamePanel.setBorder(BorderFactory.createTitledBorder("Game Display")); gamePanel.setBackground(Color.white); gamePanel.setPreferredSize(new Dimension(400, 550)); //create game status panel JPanel statusPanel = new JPanel(); statusPanel.setBorder(BorderFactory.createTitledBorder("Game Status")); statusPanel.setBackground(Color.yellow); statusPanel.setPreferredSize(new Dimension(150, 150)); //main panel bg color setBackground(Color.black); //Text labels for game status JLabel difficulty = new JLabel("Difficulty: "); JLabel tries = new JLabel("Tries Remaining: "); JLabel hint = new JLabel("Hint: "); JLabel currLevel = new JLabel("Level: "); //create game buttons newGame = new JButton("New Game"); loadFileDir = new JButton("Load Filedir.."); quit = new JButton("Quit"); newGame.setPreferredSize(new Dimension(100,20)); newGame.setToolTipText("Start New Game"); newGame.addActionListener(this); loadFileDir.setPreferredSize(new Dimension(100,20)); loadFileDir.setToolTipText("Choose File dir to load words"); loadFileDir.addActionListener(this); quit.setPreferredSize(new Dimension(100,20)); quit.setToolTipText("Exit game"); quit.addActionListener(this); //radio buttons for one/two player modes onePlayer = new JRadioButton ("One Player", true); twoPlayer = new JRadioButton ("Two Players"); onePlayer.addActionListener(this); twoPlayer.addActionListener(this); //group radio buttons ButtonGroup group = new ButtonGroup(); group.add (onePlayer); group.add (twoPlayer); //combo box for difficulty settings String[] level = {"Level 1", "Level 2", "Level 3", "AutoLevel"}; diff = new JComboBox (level); diff.setSelectedIndex(-1); diff.setPreferredSize(new Dimension(20,20)); diff.setSize(new Dimension(20,20)); //create a button keyboard in game display a = new Button[26]; StringBuffer buffer; for (int i = 0; i <26; i++) { buffer = new StringBuffer(); buffer.append((char)(i+65)); a[i] = new Button(buffer.toString()); a[i].addActionListener(this); gamePanel.add(a[i]); } //menu panel, buttons, box layout menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.PAGE_AXIS)); menuPanel.add (Box.createRigidArea(new Dimension (0, 5))); menuPanel.add (newGame); menuPanel.add (Box.createRigidArea(new Dimension (0, 10))); menuPanel.add (loadFileDir); // menuPanel.add (Box.createRigidArea(new Dimension (0, 10))); // menuPanel.add (difficulty); // menuPanel.add (diff); menuPanel.add (Box.createRigidArea(new Dimension (0, 10))); menuPanel.add (onePlayer); menuPanel.add (twoPlayer); menuPanel.add (Box.createRigidArea(new Dimension (0, 10))); menuPanel.add (quit); menuPanel.add (Box.createRigidArea(new Dimension (0, 10))); //game status panel with labels, box layout statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.PAGE_AXIS)); statusPanel.add (currLevel); statusPanel.add (Box.createRigidArea(new Dimension (0, 5))); statusPanel.add (hint); statusPanel.add (Box.createRigidArea(new Dimension (0, 5))); statusPanel.add (tries); statusPanel.add (Box.createVerticalStrut(320)); gamePanel.add(draw); menuPanel.add(statusPanel); add(menuPanel); add(gamePanel); }
and finally the painting class:
Java Syntax (Toggle Plain Text)
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); } }
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.
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
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.
![]() |
Other Threads in the Java Forum
- Previous Thread: making a quiz game
- Next Thread: bucketsort
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component coordinates database defaultmethod development dice dragging ebook eclipse educational error exception formatingtextintooltipjava fractal game givemetehcodez graphics gui hql html ide image infinite ingres input integer invokingapacheantprogrammatically j2me java javaprojects jni jpanel jtextarea julia linux list loop looping map method methods mobile mysql netbeans newbie openjavafx parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads tree websites windows






