Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: getWidth and getHeight of JPanel

  1. #1
    Kareem Mesbah is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default getWidth and getHeight of JPanel

    hi, what's up all ?
    I've question that is related to methods getWidth(); and getHeight(); of class JPanel.
    actually I was trying to draw rainbow using fillArc(); method
    then I already created my JFrame object that will hold this JPanel object and set its size to 400*400
    I've noticed something, that when I getWidth and getHeight of the JPanel I get384, 362 respectively. they're not equal to the width and the height of the JFrame which I set earlier.
    and that's ok, they're different objects and each one has its own attributes.
    but I want to know depending on what the JPanel sets its size?

    I've discovered that the JPanel's width and height are always less than the JFrame's width and height by 16 and 38 respectively.

    here's my code

    Java Code:
    import java.awt.Color;import java.awt.Graphics;
    
    
    import javax.swing.JPanel;
    
    
    
    
    public class DrawingArcs extends JPanel {
        private static final Color VIOLET = new Color (128, 0, 128);
        private static final Color INDIGO = new Color (75, 0, 130);
        private Color[] colors = {Color.WHITE, Color.WHITE, VIOLET,
                                 INDIGO,Color.BLUE, Color.GREEN,
                                 Color.YELLOW, Color.ORANGE, Color.RED};
        
        public DrawingArcs () {
            setBackground(Color.WHITE);
        }// end constructor
        
        @Override
        protected void paintComponent(Graphics g) {
            
            super.paintComponent(g);
            
            int radius = 20;
            int centerX = getWidth()/2;
            int centerY = getHeight()-10;
    
    
            for (int counter = colors.length; counter > 0; counter--) {
                
                g.setColor(colors[counter-1]);//setting color
                g.fillArc(centerX-counter*radius, centerY-counter*radius, counter*radius*2, counter*radius*2,0,180);//drawing rainbow
                
            }// end for
    
    
        }// end paintComponent
        
    }
    and here's the main method
    Java Code:
    import javax.swing.JFrame;
    
    
    
    public class RainbowTest {
    
    
        
        public static void main(String[] args) {
            
            DrawingArcs arcs = new DrawingArcs();
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 250);
            frame.add(arcs);
            frame.setVisible(true);
    
    
        }// end main
    
    
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: getWidth and getHeight of JPanel

    The frame is always going to be bigger than the panel you add because the frame will include things like the title bar.

    The actual size of components depends on the LayoutManager of whatever container they are added to. Use of layout managers in this way enables a gui to be "fluid" - that is, components will be change their size and position depending on the overall size of the gui.

    If you haven't seen it before there is a whole section on Creating a GUI (including layout) in Oracle's Tutorial.
    Kareem Mesbah likes this.

  3. #3
    Kareem Mesbah is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default Re: getWidth and getHeight of JPanel

    thanks :)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Similar Threads

  1. Jpanel.getHeight() is returning zero
    By mkj in forum AWT / Swing
    Replies: 8
    Last Post: 02-14-2012, 01:46 PM
  2. Replies: 4
    Last Post: 02-13-2012, 11:54 PM
  3. JPanel getSize() getWidth(); also force maximum size?
    By AcousticBruce in forum New To Java
    Replies: 3
    Last Post: 02-23-2011, 05:22 AM
  4. getWidth(),GetHeight() using POI
    By Zeus in forum New To Java
    Replies: 0
    Last Post: 08-02-2010, 01:28 PM
  5. getWidth && getHeight
    By courteous in forum New To Java
    Replies: 1
    Last Post: 01-18-2009, 11:39 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
  •