Results 1 to 8 of 8
- 05-24-2012, 10:53 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
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!
- 05-24-2012, 11:03 AM #2
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).
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-24-2012, 01:45 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
Re: Get current Frame Size? Autoadjusting GUI to frame size?
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.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); } } }
- 05-24-2012, 05:08 PM #4
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-26-2012, 04:45 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
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()); } } }
- 05-26-2012, 05:43 PM #6
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-26-2012, 05:50 PM #7
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.
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.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); }
dbLast edited by DarrylBurke; 05-26-2012 at 05:53 PM.
Why do they call it rush hour when nothing moves? - Robin Williams
- 05-26-2012, 07:07 PM #8
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
Similar Threads
-
AWT changing Frame-Size
By Furtano in forum AWT / SwingReplies: 1Last Post: 12-27-2011, 05:36 AM -
frame changes size even tho i told it not to :D
By wildheart25c in forum AWT / SwingReplies: 0Last Post: 06-18-2011, 12:37 PM -
How to set Jpanel in the center of frame when Increase the size of frame
By justbeller in forum AWT / SwingReplies: 4Last Post: 01-18-2011, 08:22 AM -
Setting frame size to the size of an image
By Yoruichi in forum AWT / SwingReplies: 5Last Post: 04-22-2009, 04:37 PM -
[SOLVED] How to set the frame size?
By impact in forum New To JavaReplies: 7Last Post: 05-02-2008, 11:57 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks