Results 1 to 10 of 10
  1. #1
    bdizzle's Avatar
    bdizzle is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Exclamation Find the Middle of a JFrame??

    I've created a JFrame and I need to find the middle of it as to draw something directly in the middle. Can someone help me please?

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    I would suggest adding a JPanel to the JFrame and draw to the JPanel instead. To get the middle it's:

    Java Code:
    int mx = this.getWidth()/2;
    int my = this.getHeight()/2;
    My Hobby Project: LegacyClone

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Test extends JFrame {
        public void paint(Graphics g) {
            int w = getWidth();
            int h = getHeight();
            // Center of frame.
            g.setColor(Color.red);
            g.drawLine(0, h/2, w, h/2);
            g.drawLine(w/2, 0, w/2, h);
            // Center of contentPane (drawing area).
            Insets insets = getInsets();
            int x = insets.left + (w - insets.left - insets.right)/2;
            int y = insets.top + (h - insets.top - insets.bottom)/2;
            g.setColor(Color.blue);
            g.drawLine(0, y, w, y);
            g.drawLine(x, 0, x, h);
        }
    
        public static void main(String[] args) {
            JFrame f = new Test();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  4. #4
    bdizzle's Avatar
    bdizzle is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    thanks man,
    ill try it out
    big help!

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default

    ill try it out
    Which suggestion? The better suggestion is the first one suggesting you extend JPanel to do the drawing. The custom painting is done in the paintComponent() method.

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

    Default

    Quote Originally Posted by hardwired View Post
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Test extends JFrame {
        //....
    I politely disagree with this approach.

    To the original poster: you would be better off painting in a JPanel using its paintComponent rather than drawing directly in a JFrame. You probably want to draw in the center of the panel (the JFrame's content pane) anyway, not in the center of the JFrame.

  7. #7
    clydedoris is offline Member
    Join Date
    Feb 2010
    Posts
    80
    Rep Power
    0

    Default

    i agree. from the main class, create a JFrame, say MyJFrame. then create another class that would extend the JPanel class, say MYJPanel. then paint MYJPanel. then add it to MYJFrame.

    Hope that helps!..
    [why are you annoyed with my sig?]

  8. #8
    bdizzle's Avatar
    bdizzle is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    my actual question was how to find the middle of the frame. I am trying to draw circles that get gradually bigger as they get farther away from the middle of the screen

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

    Default

    Quote Originally Posted by bdizzle View Post
    my actual question was how to find the middle of the frame. I am trying to draw circles that get gradually bigger as they get farther away from the middle of the screen
    Well, I think I speak for all when I wish you much luck with this.

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default

    my actual question was how to find the middle of the frame.
    And you've already been given the answer.

Similar Threads

  1. How to write a string middle of a text file?
    By loggen in forum New To Java
    Replies: 5
    Last Post: 12-19-2008, 08:48 AM
  2. adding a jpanel in the middle of the script
    By 2o2 in forum AWT / Swing
    Replies: 11
    Last Post: 10-12-2008, 05:50 PM
  3. Replies: 1
    Last Post: 07-13-2008, 03:16 PM
  4. Middle Click Closes Tab Eclipse Plugin 1.0.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 10-20-2007, 05:59 PM
  5. Return in the Middle of synchronized Block
    By ariak in forum Advanced Java
    Replies: 1
    Last Post: 07-26-2007, 10:24 AM

Tags for this Thread

Posting Permissions

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