Results 1 to 13 of 13
  1. #1
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Setting a JPanel height after a menu has been added?

    Hello again folks

    Hopefully this will be the last question for this round of deveopment. So here is my issue. I have a JPanel that I draw some graphics on. I tell the panel to draw draw the graphics based on the available height and width of the available dimentions after the menu has been added, but it doesn't do this. The size of the available panel doesn't dynamically change after the menu has been added. Here is the code for the panel:

    Java Code:
    public class Game extends JFrame
    {
    
        private final int WIDTH = 500;
        private final int HEIGHT = 500;
        private court court;
        protected JMenuBar menu;
        protected JMenu m;
        protected JMenuItem exit;
        protected JMenuItem start;
    
        public Game(String title)
        {
            super(title);
            setSize(WIDTH, HEIGHT);
            this.setResizable(false);
            setLayout(new BorderLayout());
            setVisible(true);
            court = new court(getWidth(), getHeight());
            court.setBackground(Color.black);
            add(court);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            menu = new JMenuBar();
            m = new JMenu("Options");
            exit = new JMenuItem("Exit");
            start = new JMenuItem("New Game");
            m.add(exit);
            m.add(start);
            menu.add(m);
            MenuWatcher mw = new MenuWatcher();
            exit.addActionListener(mw);
            start.addActionListener(mw);
            setJMenuBar(menu);
    
        }
        
    
        private int getHeight()
        {
            return getHeight() - getInsets().top - getInsets().bottom;
        }
    	private int getWidth()
        {
            return getWidth() - getInsets().left - getInsets().right;
        }
        
        
        private class court extends JPanel 
        {
            private Court myCourt;
            public court(int width, int height)
            {
                myCourt = new Court(width, height);
                setSize(width, height);
            }
            public void paint (Graphics g)
            {
             super.paint(g);   
            }
    
           
            public void paintComponent(Graphics g) 
            {
                super.paintComponent(g);
                myCourt.draw(g); 
                myCourt.drawCourt(g);
                myCourt.ball(g);
                myCourt.drawBatMask();
                myCourt.ballMask();
                myCourt.lineMask();
                myCourt.drawString(g);
    
            public void updateCourtState()
            {
               
                myCourt.updateCourtState();
                myCourt.bx = bpx;
                myCourt.by = bpy
                CourtLength = myCourt.CourtLength;
                CourtY=myCourt.CourtY;
            }
         
         }
         
    }
    And to draw the graphics
    Java Code:
    public class Court
    {
        public Court(int width, int height)
        {
           
            this.width = width;
            this.height = height;
        }
       
        public void updateCourtState()
        {
                CourtY = CourtPos;
                bx = bpx;
                by = bpy
        }
    
        public void draw(Graphics g)
        {
            g.setColor(Color.white);
            g.fillRect(0, 0, width, breadth);
    
            g.setColor(Color.white);
            g.fillRect(0, (height-breadth), width, breadth);  
        }
        <snip>
        }
    }
    So the method getHeight() doesn't 'see' that the menu has been added, and therefore the graphics drop off the bottom of the panel. Any ideas?

    Thanks

  2. #2
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Setting a JPanel height after a menu has been added?

    Ok, I think I'm supposed to use pack() but not sure where to place it. I could be wrong though.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Setting a JPanel height after a menu has been added?

    a) Does that even compile? Never write methods that conflict with a parent method unless you intend to, especially if you change the visibility of said method which should raise a compile time error (eg getHeight/getWidth() )
    b) Post an SSCCE. Are you setting the preferred size, or just calling setHeight/width? Where do you do this in the code you posted? Hint: an SSCCE would demonstrate this.
    c) Are you adding components dynamically? Read the API for the add method and it explains the problems associated with this.

  4. #4
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Setting a JPanel height after a menu has been added?

    Hi doWhile

    Probably doesn't compile as I sniped out the bits I didn't think were relevant. Will come back later with another version.

  5. #5
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Setting a JPanel height after a menu has been added?

    Ok, try again

    Java Code:
    package SimpleGame;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.WindowConstants;
    public class SimpleGame extends JFrame
    {
        private final int FRAME_WIDTH = 500;
        private final int FRAME_HEIGHT = 500;
        private Court court;
        protected JMenuBar menu;
        protected JMenu m;
        protected JMenuItem ex;
        protected JMenuItem nGame;
        private boolean start;
        private int sleep = 100;
        
           public SimpleGame(String title)
        {
            super(title);
            setSize(FRAME_WIDTH, FRAME_HEIGHT);
            this.setResizable(false);
            setLayout(new BorderLayout());
            setVisible(true);
            menu = new JMenuBar();
            m = new JMenu("file");
            ex= new JMenuItem("Close");
            nGame = new JMenuItem("Start Again");
            m.add(ex);
            m.add(nGame);
            menu.add(m);
            MenuWatcher mw = new MenuWatcher();
            ex.addActionListener(mw);
            nGame.addActionListener(mw);
            setJMenuBar(menu);
            menu.setVisible(true);
            court = new Court(getTheWidth(), getTheHeight());
            court.setBackground(Color.black);
            add(court);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.addKeyListener(new Move());
    
        }
        private int getTheWidth()
        {
            return getWidth() - (getInsets().left - getInsets().right);
        }
    
        private int getTheHeight()
        {   
            return getHeight() - getInsets().top - getInsets().bottom;
        }
    
        public void update()
        { 
            while(true)
            {    
                while(start)
                {
                    this.court.updatePictureState();
                    repaint();    
                    {
                        try
                          {
                             Thread.sleep(sleep);
                          }
                             catch(InterruptedException e)
                          {
                             System.exit(0);
                          }
                    }
                }
            }
        }
    
        private class Court extends JPanel
        {
            private Court court;
            public Court(int width, int height)
            {
                court = new Court(width, height);
                setSize(width, height);  
            }
            public void paint (Graphics g)
            {
             super.paint(g); 
            
            }
            public void paintComponent(Graphics g) 
            {
                super.paintComponent(g);
                
            }
    
            public void updatePictureState()
            {
                
            }
            public void moveX()
            {
    
            }
            public void ballY()
            {
    
            }
           
        }
    
        public class Move extends KeyAdapter
        {
            public void keyPressed (KeyEvent k)
            {
                
                if(k.getKeyCode() ==  KeyEvent.VK_UP)
                    {
                       
                     }
    
                {
                    if(k.getKeyCode() == KeyEvent.VK_DOWN)
                    {
                        
                    }
                }
                            
            }
        }
         private class MenuWatcher implements ActionListener
         {
          public void actionPerformed(ActionEvent e)
             {
              if(e.getSource()==ex)
             {
                  System.exit(0);
              }
                   
          }
         }
         
    }
    And to draw the graphics

    Java Code:
    package SimpleGame;
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class Court
    {
    
    
        private int width;
        private int height;
        private int circ;
        public int length;
        public int breadth;
        public int btY;
        private int btX;
        public int pos;
        public int bY;
        public int bX;
        public int bPX;
        public int bPY;
    
        public Court(int width, int height)
        {
           
            this.width = width;
            this.height = height;
            this.breadth = 10;
            this.circ= 50;
            this.length = 100;
        }
    
        public void updatePictureState()
        {
                
                btY = pos;
                bX = bPX;
                bY = bPY;
        }
        public void draw(Graphics g)
        {
            
            g.setColor(Color.white);
            g.fillRect(0, 0, width, breadth);
    
            g.setColor(Color.white);
            g.fillRect(0, (height-breadth), width, breadth);
    
            g.setColor(Color.BLUE);
            g.fillRect(btX, btY, breadth, length);
    
             g.setColor(Color.yellow);
             g.fillOval(bX, bY,circ,circ);
           
        }      
    }
    Should compile. I've removed most of the functionality as it's not relevant to the thread (hopefully fulfils the SSCCE criteria). So my main question is how do I get the JPanel to recognise that I've added a menu and therefore the drawing panel size is reduced by the width of the menu?

    Cheers

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Setting a JPanel height after a menu has been added?

    You've got two classes named Court -- one a private internal class and the other a stand alone. this is confusing -- so which do we use?

  7. #7
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Setting a JPanel height after a menu has been added?

    I wouldn't qualify that as an SSCCE, but like I said above, read the API for the add method. If you are adding things to a JFrame when creating it, don't add them after setting the JFrame to visible, otherwise a call to validate will be required. And again, don't name methods that could conflict with parent methods (I am now referring to update - while its method signature isn't identical to the parent method its name is, and this can cause not only confusion but also potentially cause minor changes in the future to have drastic impacts if that change causes the method to override the parent). Lastly, I'm not sure what the method update is supposed to be doing as its never called - but do not call this from the EDT.

  8. #8
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Setting a JPanel height after a menu has been added?

    I swear I'm loosing the will to live. Sorry guys, what seemed like a simple question has turned into a nightmare. Try once again.

    Java Code:
    package SimpleGame;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.WindowConstants;
    public class SimpleGame extends JFrame
    {
        private final int FRAME_WIDTH = 500;
        private final int FRAME_HEIGHT = 500;
        private Court court;
        protected JMenuBar menu;
        protected JMenu m;
        protected JMenuItem ex;
        protected JMenuItem nGame;
        private boolean start;
        private int sleep = 100;
        
           public SimpleGame(String title)
        {
            super(title);
            setSize(FRAME_WIDTH, FRAME_HEIGHT);
            this.setResizable(false);
            setLayout(new BorderLayout());
            setVisible(true);
            menu = new JMenuBar();
            m = new JMenu("file");
            ex= new JMenuItem("Close");
            nGame = new JMenuItem("Start Again");
            m.add(ex);
            m.add(nGame);
            menu.add(m);
            MenuWatcher mw = new MenuWatcher();
            ex.addActionListener(mw);
            nGame.addActionListener(mw);
            setJMenuBar(menu);
            menu.setVisible(true);
            court = new Court(getTheWidth(), getTheHeight());
            court.setBackground(Color.black);
            add(court);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.addKeyListener(new Move());
    
        }
        private int getTheWidth()
        {
            return getWidth() - (getInsets().left - getInsets().right);
        }
    
        private int getTheHeight()
        {   
            return getHeight() - getInsets().top - getInsets().bottom;
        }
    
        public void update()
        { 
            while(true)
            {    
                while(start)
                {
                    this.court.updatePictureState();
                    repaint();    
                    {
                        try
                          {
                             Thread.sleep(sleep);
                          }
                             catch(InterruptedException e)
                          {
                             System.exit(0);
                          }
                    }
                }
            }
        }
    
        private class NewCourt extends JPanel
        {
            private NewCourt court;
            public NewCourt(int width, int height)
            {
                court = new NewCourt(width, height);
                setSize(width, height);  
            }
            public void paint (Graphics g)
            {
             super.paint(g); 
            
            }
            public void paintComponent(Graphics g) 
            {
                super.paintComponent(g);
                
            }
    
            public void updatePictureState()
            {
                
            }
            public void moveX()
            {
    
            }
            public void ballY()
            {
    
            }
           
        }
    
        public class Move extends KeyAdapter
        {
            public void keyPressed (KeyEvent k)
            {
                
                if(k.getKeyCode() ==  KeyEvent.VK_UP)
                    {
                       
                     }
    
                {
                    if(k.getKeyCode() == KeyEvent.VK_DOWN)
                    {
                        
                    }
                }
                            
            }
        }
         private class MenuWatcher implements ActionListener
         {
          public void actionPerformed(ActionEvent e)
             {
              if(e.getSource()==ex)
             {
                  System.exit(0);
              }
                   
          }
         }
         
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Setting a JPanel height after a menu has been added?

    And doWhile has already given you excellent advice regarding calling setVisible(true) only after adding components, and I'll add, after calling pack(). The order is:
    1. add components,
    2. pack,
    3. setLocation (if you're doing this),
    4. setVisible(true)

  11. #11
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Setting a JPanel height after a menu has been added?

    Ok guys, thanks for the input. First, not sure why it won't compile for you, i can get it to compile. Second, the update() method invokes the updatePictureState() of the Court class, to redraw the pane depending on your interaction. I've tried to follow your advice, and added the pack() and setVisible(true) at the end of my constructor for SimpleGame, but the results isn't right.

    Setting a JPanel height after a menu has been added?-untitled.png

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Setting a JPanel height after a menu has been added?

    It doesn't compile because you're treating Court as if it extends a JComponent or somesuch and it doesn't extend anything. If you've changed your Court class, then for gosh's sake post it as this is getting a bit frustrating. For pack() to work correctly you don't set the size of a component but rather the preferredSize.

  13. #13
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Setting a JPanel height after a menu has been added?

    Quote Originally Posted by Fubarable View Post
    It doesn't compile because you're treating Court as if it extends a JComponent or somesuch and it doesn't extend anything. If you've changed your Court class, then for gosh's sake post it as this is getting a bit frustrating. For pack() to work correctly you don't set the size of a component but rather the preferredSize.
    Ok Fubarable, it's getting too late for me to concentrate. Thanks for helping but I'm gonna check out now. I'm too tired. You guys are too patient for us noobies. Will try again in the morning (or actually evening as I have to work)
    Goodnight for now.

Similar Threads

  1. Setting text width and height in PDFBox
    By svenskmand in forum Advanced Java
    Replies: 0
    Last Post: 12-21-2010, 12:23 AM
  2. Replies: 0
    Last Post: 07-13-2010, 09:16 AM
  3. Setting/Updating a tabbed JPanel's name
    By dave_gardner in forum AWT / Swing
    Replies: 3
    Last Post: 03-03-2010, 12:49 PM
  4. Replies: 1
    Last Post: 04-01-2009, 02:00 AM
  5. Setting JPanel to a fullscreen
    By hitmen in forum New To Java
    Replies: 3
    Last Post: 03-15-2009, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •