Results 1 to 2 of 2

Thread: Resize frame

  1. #1
    lenny is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default 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

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

    Default

    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

  1. close a frame..
    By tajinvillage in forum New To Java
    Replies: 5
    Last Post: 04-27-2008, 11:22 PM
  2. Replies: 1
    Last Post: 02-17-2008, 12:43 AM
  3. resize tabs in jtabbedpane
    By osval in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 04:02 AM
  4. Help with JTextArea, resize windows
    By paul in forum AWT / Swing
    Replies: 1
    Last Post: 07-16-2007, 05:11 PM
  5. Frame Query
    By Daniel in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 07:27 PM

Posting Permissions

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