Results 1 to 4 of 4
Thread: getWidth and getHeight of JPanel
- 10-10-2012, 03:03 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 15
- Rep Power
- 0
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
and here's the main methodJava 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 }
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 }
- 10-10-2012, 03:11 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 10-10-2012, 03:22 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 15
- Rep Power
- 0
Re: getWidth and getHeight of JPanel
thanks :)
- 10-10-2012, 03:47 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Similar Threads
-
Jpanel.getHeight() is returning zero
By mkj in forum AWT / SwingReplies: 8Last Post: 02-14-2012, 12:46 PM -
getHeight() giving two different valpues for the same JPanel
By peterhabe in forum New To JavaReplies: 4Last Post: 02-13-2012, 10:54 PM -
JPanel getSize() getWidth(); also force maximum size?
By AcousticBruce in forum New To JavaReplies: 3Last Post: 02-23-2011, 04:22 AM -
getWidth(),GetHeight() using POI
By Zeus in forum New To JavaReplies: 0Last Post: 08-02-2010, 12:28 PM -
getWidth && getHeight
By courteous in forum New To JavaReplies: 1Last Post: 01-18-2009, 10:39 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks