Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Zigster

Thread: Get current Frame Size? Autoadjusting GUI to frame size?

  1. #1
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Get current Frame Size? Autoadjusting GUI to frame size?

    What I'm trying to do, is print a circle in the middle of the JFrame, and have it stay in the middle when I resize the JFrame by with the mouse after I have run it. Similar, for instance, to how this website readjusts everything when you resize the browser window.

    What is the best way to do this? How can I find the size of the JFrame if people resize it with the mouse?

    Thanks!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Get current Frame Size? Autoadjusting GUI to frame size?

    Print a circle? do you mean paint?

    Lets see what you've tired so far, in the form of a SSCCE (Short, Self Contained, Correct (Compilable), Example).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Get current Frame Size? Autoadjusting GUI to frame size?

    Java Code:
    package Main;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class DrawingColor{
    	public static void main(String[] args) {
    		DrawingColor d = new DrawingColor();
    	}
    
    	public DrawingColor(){
    		JFrame frame = new JFrame("Drawing with Alpha");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().add(new ArenaComponent());
    		frame.setSize(800,600);
    		frame.setVisible(true);  
    	}
    
    	public class ArenaComponent extends JComponent{
    		public void paint(Graphics g){
    
    			g.setColor(Color.black);
    			g.fillOval(240,10, 420,570);
    			g.setColor(Color.white);
    			g.fillOval(250,20,400,550); 
    			g.setColor(Color.red);
    			g.fillOval(400,100,10,10);
    		}
    	}
    }
    I want it to always be in the centre, even when I resize the frame with the mouse, but I don't know how to check what the frame size is and to automatically update when it changes.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Get current Frame Size? Autoadjusting GUI to frame size?

    First off, the recommended method to override for custom painting in a Swing component is paintComponent(...), not paint(...).

    Within a painting method override in a JComponent subclass, you can use methods of the class such as getWidth() and getHeight() and compute the desired location from the returned int values.

    You don't need to check the size of the frame.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Get current Frame Size? Autoadjusting GUI to frame size?

    Thankyou DarrylBurke, it works!

    Java Code:
    package Main;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class DrawArena{
    	public static void main(String[] args) {
    		new DrawArena();
    	}
    
    	public DrawArena(){
    		JFrame frame = new JFrame("Drawing with Alpha");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().add(new ArenaComponent());
    		frame.setSize(800,600);
    		frame.setVisible(true);
    	}
    
    	public class ArenaComponent extends JComponent{
    		public void paintComponent(Graphics g){
    
    			g.setColor(Color.black);
    			g.fillOval(getWidth()/2-((getWidth()/2+20)/2),10, getWidth()/2+20,getHeight()-30);
    			g.setColor(Color.white);
    			g.fillOval(getWidth()/2-getWidth()/4,20,getWidth()/2,getHeight()-50); 
    			g.setColor(Color.red);
    			g.fillOval(400,100,10,10);
    			System.out.println(getWidth());
    		}
    	}
    }
    DarrylBurke likes this.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Get current Frame Size? Autoadjusting GUI to frame size?

    Now learn to construct and deal with Swing components on the EDT: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Failure to honor Swing's single threaded rule can lead to intermittent buggy behavior that is next to impossible to analyze.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Get current Frame Size? Autoadjusting GUI to frame size?

    Also, that code can be made vastly more readable by assigning int variables instead of resorting to repeatedly calling the same methods, observing the coding conventions in respect of horizontal whitespace, and adding a bit of vertical whitespace.
    Java Code:
            public void paintComponent(Graphics g){
    
                int w = getWidth();
                int h = getHeight();
                // System.out.println(w);
    
                g.setColor(Color.black);
                g.fillOval(w / 2 - ((w/2 + 20) / 2), 10, w / 2 + 20, h - 30);
    
                g.setColor(Color.white);
                g.fillOval(w / 2 - w / 4, 20, w / 2, h - 50); 
    
                g.setColor(Color.red);
                g.fillOval(400, 100, 10, 10);
            }
    When extending a subclass of JComponent, it's good practice to invoke the super implementation of the overridden paintComponent(...), unless you are very sure that you're painting the entire area of the component.

    db
    Last edited by DarrylBurke; 05-26-2012 at 05:53 PM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Get current Frame Size? Autoadjusting GUI to frame size?

    Thanks, I did what you suggested with the ints and spacing, and I will read some tutorials about Concurrency and JComponent.

Similar Threads

  1. AWT changing Frame-Size
    By Furtano in forum AWT / Swing
    Replies: 1
    Last Post: 12-27-2011, 05:36 AM
  2. frame changes size even tho i told it not to :D
    By wildheart25c in forum AWT / Swing
    Replies: 0
    Last Post: 06-18-2011, 12:37 PM
  3. Replies: 4
    Last Post: 01-18-2011, 08:22 AM
  4. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 04:37 PM
  5. [SOLVED] How to set the frame size?
    By impact in forum New To Java
    Replies: 7
    Last Post: 05-02-2008, 11:57 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
  •