Results 1 to 2 of 2
Thread: Resize frame
- 07-24-2007, 03:15 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
Resize frame
Hi, For a program we have to have a frame, with a JComponent inside it, and then a few objects drawn on it. All of which is no problem but the last thing that is needed, is when the entire frame is resized, all of the pieces inside it need to stretch out as well.
This is where my problem is, I think it is because the paintComponent method needs to be called again when the frame is resized but I do not know how to do that.
All of my drawing statements use variables as opposed to set sizes, so it should all work if I could update it after the frame is resized.
Thanks
- 07-29-2007, 11:18 PM #2
The trick is to base your variable values on the current size of the component.
Java Code:import java.awt.*; import javax.swing.*; public class StretchyGraphics extends JPanel { protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); g2.drawRect(w/16, h/16, w*7/8, h*7/8); } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new StretchyGraphics()); f.setSize(400,400); f.setVisible(true); } }
Similar Threads
-
close a frame..
By tajinvillage in forum New To JavaReplies: 5Last Post: 04-27-2008, 10:22 PM -
Problems with gridBaglayout when I resize the window
By Iyengar in forum AWT / SwingReplies: 1Last Post: 02-16-2008, 11:43 PM -
resize tabs in jtabbedpane
By osval in forum New To JavaReplies: 1Last Post: 08-02-2007, 03:02 AM -
Help with JTextArea, resize windows
By paul in forum AWT / SwingReplies: 1Last Post: 07-16-2007, 04:11 PM -
Frame Query
By Daniel in forum AWT / SwingReplies: 1Last Post: 07-05-2007, 06:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks