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 4 of 4

Thread: Getting the width and height of a frame without borders

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Getting the width and height of a frame without borders

    So im using the this.getContentPane().getSize(); to set a Dimension variable called borderlessSize (because its supposed to contain the size of the frame excluding the borders) and then im setting an int borderlessWidth and an int borderlessHeight, to borderlessSize.getWidth/Height. However when i print out borderlessWidth, and borderlessHeight, theyre both showing up as 0?? please help!! Heres my code.




    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
     
    import javax.swing.JFrame;
     
     
    public class Frame extends JFrame
    {
    	int xCoord = 50; 
    	int yCoord = 50;
    	private final int ovalWidth = 25;
    	private final int ovalHeight = 25;
    	private final int WIDTH = 800;
    	private final int HEIGHT = WIDTH / 5 * 4;
    	private Dimension borderlessSize = this.getContentPane().getSize();
    	private final int borderlessWidth = (int)borderlessSize.getWidth();
    	private final int borderlessHeight = (int)borderlessSize.getHeight();
    	private Image dbImage;
    	private Graphics dbG;
    	private Dimension d = new Dimension(WIDTH, HEIGHT);
     
    	Frame()
    	{
    		addKeyListener(new AL());
    		setTitle("Frame");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(d);
    		setResizable(false);
    		setVisible(true);
    		setBackground(Color.WHITE);
     
    		System.out.println(borderlessWidth + " " + borderlessHeight);
    	}
     
    	public void paint(Graphics g)
    	{
    		dbImage = createImage(getWidth(), getHeight());
    		dbG = dbImage.getGraphics();
    		paintComponent(dbG);
    		g.drawImage(dbImage, 0, 0, this);
     
    	}
     
    	public void paintComponent(Graphics g)
    	{
    		g.fillOval(xCoord, yCoord, ovalWidth, ovalHeight);
     
    		repaint();
    	}
     
    	public class AL extends KeyAdapter
    	{
    		public void keyPressed(KeyEvent e)
    		{
    			int keyCode = e.getKeyCode();
     
    			if(keyCode == e.VK_LEFT)
    			{
    				if(xCoord <= 0)
    				{
    					xCoord = 0;
    				}
    				else
    				{
    					xCoord += -15;
    				}
    			}
    			if(keyCode == e.VK_RIGHT)
    			{
    				if(xCoord >= borderlessWidth - ovalHeight)
    				{
    					xCoord = borderlessWidth - ovalHeight;
    				}
    				else
    				{
    					xCoord += 15;
    				}
    			}
    			if(keyCode == e.VK_UP)
    			{
    				if(yCoord <= 0)
    				{
    					System.out.println("top collision");
    					yCoord = 0;
    				}
    				else
    				{
    					yCoord += -15;
    				}
    			}
    			if(keyCode == e.VK_DOWN)
    			{
    				if(yCoord >= borderlessHeight - ovalHeight)
    				{
    					yCoord = borderlessHeight - ovalHeight;
    				}
    				else
    				{
    					yCoord += 15;
    				}
    			}
    		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting the width and height of a frame without borders

    Are you getting the dimensions BEFORE the frame is shown?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Getting the width and height of a frame without borders

    Where/when you declare and initialise your variables probably means that they are given values before the frame is constructed. Try giving them values after everything has been set up.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Getting the width and height of a frame without borders

    I am declaring the borderlessDimension before I declare the frame and set it Visible, let me try declaring it after

    --- Update ---

    Dude thanks so much! I feal like an idiot now haha

Similar Threads

  1. Problem with borders appearing
    By michael.duffy31 in forum AWT / Java Swing
    Replies: 5
    Last Post: July 18th, 2013, 11:24 PM
  2. Replies: 1
    Last Post: January 19th, 2012, 03:44 PM
  3. [SOLVED] Shapes: How to Outline with Thick Borders?
    By snowguy13 in forum AWT / Java Swing
    Replies: 2
    Last Post: January 4th, 2012, 05:16 PM
  4. JButton Width & Height
    By Howdy_McGee in forum Java Theory & Questions
    Replies: 4
    Last Post: May 22nd, 2010, 07:12 AM
  5. Reading the size (height and width) of word documents
    By pradeep_das in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 7th, 2010, 09:58 AM

Tags for this Thread