Results 1 to 2 of 2
  1. #1
    courteous is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Question getWidth && getHeight

    Hello! My first question here (I did 'search':)):
    Shouldn't
    Java Code:
    X = getWidth()/2;
    Y = getHeight()/2;
    along with
    Java Code:
    g.drawOval(X, Y, r1, r1)
    put a circle in the center of JFrame (no matter what its setLocation and setSize are)? (The code is put in the JPanel, which is then added to JFrame.)

    What "width" and "height" exactly do the methods
    Java Code:
    getWidth()
    getHeight()
    return (yes, I've checked the API documentation)?

    Feeling I'm missing something basic here... :o

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Not quite. The X and Y set the upper left corner of the box that holds the oval. If you want to center it, you'll need to do some rudimentary algebra that involves your x, y, and r1. Also, your r1 represents the diameter rather than radius.

    I did something like so in my app:
    Java Code:
        int x = (mainPanel.getWidth() - DIAMETER)/2;
        int y = (mainPanel.getHeight() - DIAMETER)/2;
        Graphics2D g2 = (Graphics2D)g;
        // smooth out the drawings jagged edges
        g2.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(Color.red);
        g2.drawOval(x, y, DIAMETER, DIAMETER);
    Last edited by Fubarable; 01-18-2009 at 11:47 PM.

Similar Threads

  1. Help with getWidth andgetHeight please.
    By Neomus in forum New To Java
    Replies: 8
    Last Post: 11-25-2010, 01:33 PM
  2. getHeight() problem
    By sapata in forum AWT / Swing
    Replies: 1
    Last Post: 05-18-2008, 05:16 AM
  3. Img.getWidth(null); gives -1 why?
    By willemjav in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 03:06 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
  •