Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...

  1. #1
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...

    I am using the size of a JPanel to control the bounds of display for a circle drawn in a program I'm writing. In short, the circle cannot move outside the width or height of the JPanel. However, I'm having trouble discerning the JPanel's width and height...

    The JPanel is created using this code:
          displayPanel = new JPanel()
          {
     
             public void paintComponent(Graphics g)
             {
     
                //Construct the graphics
                Graphics2D g2 = (Graphics2D)g;
                //Fill the background black
                g2.setPaint(getBackground());
                g2.fillRect(0, 0, getWidth(), getHeight());
                //Draw the circle
                g2.setPaint(Color.BLUE);
                g2.fillOval((int)ball.getX(), (int)ball.getY(), ball.getRadius() * 2, ball.getRadius() * 2);
     
             }
     
          };
          displayPanel.setBackground(Color.BLACK);
     
          //... "displayPanel" is added to a JPanel called "panel" by this code ...
     
          panel = new JPanel(new GridBagLayout());
          GridBagConstraints c = new GridBagConstraints();
          c.fill = GridBagConstraints.BOTH;
          c.anchor = GridBagConstraints.CENTER;
          c.weightx = 0.5;
          c.weighty = 0.5;
          c.gridx = 0;
          c.gridy = 0;
          c.gridwidth = 3;
          c.gridheight = 2;
          c.ipady = 300;
          panel.add(displayPanel, c);

    Later, however, when I try to derive the width and height of displayPanel, it does not work:
          //Ball is an object that manages the location of the circle
          ball = new Ball(7, 30, 45, 20, 20, displayPanel.getWidth(), displayPanel.getHeight());
    However, when ball's constructor calls displayPanel.getWidth() and displayPanel.getHeight(), is receives 0 for both.

    Can anyone provide me with a solution to this annoyance? Any help would be greatly appreciated! Thanks!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...

    Ugh, I feel stupid.

    As it so surprisingly turns out (sarcasm dripping from my mouth) you have to set the top level container to visible before Components are lined up and given dimensions. Using that knowledge, I fixed my problem.

    I no longer require any help on this matter!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. Problem with getWidth() and getHeight()
    By dcwang3 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 2nd, 2011, 12:22 AM
  2. JPanel flickering
    By Ghosth in forum AWT / Java Swing
    Replies: 1
    Last Post: April 30th, 2011, 10:52 AM
  3. how to destroy JPanel?
    By amahara in forum AWT / Java Swing
    Replies: 2
    Last Post: February 28th, 2010, 03:41 AM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM

Tags for this Thread