Results 1 to 7 of 7
  1. #1
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default weird location of ovals

    I made a class called EyePanel that extends JPanel. This class is supposed to draw two ovals one at at 100, 100 and the other at 200, 100 both with size 100 by 100. As far as I know, these would be the ovals that fill the rectangle that has the upper left corner at 100, 100 and has sides of length 100. I then added this panel to my client program's JFrame, which has dimensions of 400 by 300. The thing is though, the EyePanel's ovals don't match up perfectly with the new frame. They are a bit lower when they should be centered. I know this because I added an action listener that is supposed to make the pupils look upwards when the getY is less than 100, and make them look downwards when it it is greater than 200. This part works perfectly but it has shown me that the eyeballs themselves are not at the correct y coordinates. I'm not really sure why this is happening or how to fix this - any help would be greatly appreciated.
    Last edited by diggitydoggz; 12-23-2008 at 05:36 PM.

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

    Default

    You may wish to post compilable code so we can see the code and the results for ourselves. Hopefully you're drawing on a JPanel and then adding that JPanel to the JFrame's contentPane and not drawing directly on the JFrame, correct?

  3. #3
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You may wish to post compilable code so we can see the code and the results for ourselves. Hopefully you're drawing on a JPanel and then adding that JPanel to the JFrame's contentPane and not drawing directly on the JFrame, correct?
    Will do. And yeah, I'm drawing onto the JPanel and adding it to the JFrame. Here's the code for the JPanel:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class EyePanel extends JPanel {
    private Point p1;
    private Point p2;

    public EyePanel() {
    p1 = new Point(135, 135);
    p2 = new Point(235, 135);
    }

    public void setEyes(int y) {
    p1.setLocation(135, y);
    p2.setLocation(235, y);
    repaint();
    }

    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.drawOval(100, 100, 100, 100);
    g.fillOval(p1.x, p1.y, 30, 30);
    g.drawOval(200, 100, 100, 100);
    g.fillOval(p2.x, p2.y, 30, 30);
    }
    Here's the code for the client:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;

    public class EyePanelClient {
    public static void main(String[] args) {
    JFrame frame = new JFrame("Crazy Eyes");
    final EyePanel eyes = new EyePanel();
    eyes.setBackground(Color.WHITE);
    frame.setSize(new Dimension(400, 300));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.add(eyes);
    frame.setVisible(true);

    frame.addMouseMotionListener(new MouseInputAdapter() {
    public void mouseMoved(MouseEvent event) {
    if(event.getY() < 100) {
    eyes.setEyes(100);
    }
    else if(event.getY() > 100 && event.getY() < 200) {
    eyes.setEyes(135);
    }
    else if (event.getY() > 200){
    eyes.setEyes(170);
    }
    }
    });
    }
    }

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

    Default

    I think the problem is that your setting the size of the JFrame rather than the JPanel. Since the JFrame also includes a title bar this will seem to push your eyes down. There are two ways I see to fix this, the simplest being for you to set the size of the JPanel (I prefer to use use of setPreferredSize, not setSize and then pack the JFrame). Something like so:
    Java Code:
        final EyePanel eyes = new EyePanel();
        eyes.setBackground(Color.WHITE);
        eyes.setPreferredSize(new Dimension(400, 300)); //sets size
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(eyes);
        frame.pack(); // invokes the layout managers
        frame.setLocationRelativeTo(null); // centers the JFrame
        frame.setVisible(true);
    Edit: oh, and for the same reason, add your mouselistener to the eyes JPanel, NOT to the JFrame. Good luck and HTH!
    Last edited by Fubarable; 12-24-2008 at 12:44 AM.

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

    Default

    One other trick to make your circles smoother, use a Graphics2D object to set the RenderingHints to use anti-aliasing when drawing. Something like so:
    Java Code:
      public void paintComponent(Graphics g)
      {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(
          RenderingHints.KEY_ANTIALIASING, 
          RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.BLACK);
        g.drawOval(100, 100, 100, 100);
        g.fillOval(p1.x, p1.y, 30, 30);
        g.drawOval(200, 100, 100, 100);
        g.fillOval(p2.x, p2.y, 30, 30);
      }
    A last suggestion: get rid of as many "magic" numbers as possible and instead use combination of constants. If you do this it will make debugging and extending your projects MUCH easier. Again, good luck!

  6. #6
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    Wow thanks. You were really helpful. The setPreferredSize thing fixed the problem... I didn't even know that was a command.

    One last question - I was playing around with the commands a bit and it only worked when I used "setPreferredSize" rather than the command "setSize." I've been searching online but I'm having a hard time finding a detailed explanation of the differences of the two. When I do setSize and pack it, the whole window just becomes tiny. Could you please clarify this real quick?

    Thanks again, you've been super helpful.

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

    Default

    The difference is in the layout managers themselves, and what information they use to set sizes and positions. Most layout managers use the preferred size to do this, not the size, and so when you call pack on the JFrame its layout manager (which by default is BorderLayout) asks all the components it holds for their preferred size. If none have been set, the manager will make the JFrame very small.

    If on the other hand you use no layout manager -- setLayout(null), then size is what is important and preferred size isn't.

Similar Threads

  1. Weird problem upon calling same function twice
    By alin_ms in forum New To Java
    Replies: 2
    Last Post: 12-20-2008, 07:14 PM
  2. Demonstration of drawing ovals in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 10:28 PM
  3. need help, weird question kinda.
    By carlos123 in forum New To Java
    Replies: 6
    Last Post: 01-22-2008, 04:19 AM
  4. Weird data output
    By Shaolin in forum New To Java
    Replies: 12
    Last Post: 12-11-2007, 05:27 PM
  5. weird looking shape JButton is it possible?
    By unhurt in forum AWT / Swing
    Replies: 8
    Last Post: 11-03-2007, 10:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •