Results 1 to 8 of 8
  1. #1
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default JFrame resizing problem

    I've made a couple games in Java using Swing/AWT, using the following method:

    • Use a JComponent (not a subclass) to draw graphics to.
    • Call the add() method of the JFrame's content pane to add my JComponent.
    • Create a loop for game math and repaint(), etc.


    The only problem is, I can't call the JFrame's pack() method, even when I call setPreferredSize(), setMaximumSize(), and setMinimumSize() on the JComponent, the window goes to smallest possible size. So instead I use the resize() method. This works for the most part, but if I decided to draw an image of width and height (WIDTH, HEIGHT), I can't call resize(WIDTH, HEIGHT) without the JFrame's size being off by 10-20 pixels.
    And not only that, but when I resize it properly using offsets, the results are different on Windows XP, Windows Vista, and Linux (GNOME).

    So, is there a way I can use Swing to set the JFrame to the proper size, so that the viewed area is the same size (in pixels) as the two numbers I specify? Normally I can use the 3 sizing methods I mentioned and call JFrame.pack(), no problems.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I generally get the JFrame's contentPane, call setPreferredSize on this (then you don't have to worry about the title strip at the top), and then call pack on the JFrame.

    edit: I'm curious about this line:
    Use a JComponent (not a subclass) to draw graphics to.
    what do you mean by this? Do you do your drawing in the JComponent's paintComponent method (the preferred method) or use getGraphics (the non-preferred method)?
    Last edited by Fubarable; 04-04-2009 at 11:52 PM.

  3. #3
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    Thank you Fubarable, that worked perfectly.

    Here's my code for whoever wants it.

    Java Code:
    // in constructor
    frame = new JFrame("Title");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Dimension size = new Dimension(WIDTH, HEIGHT);
    Container contentPane = frame.getContentPane();
    contentPane.add(this);
    contentPane.setMinimumSize(size);
    contentPane.setPreferredSize(size);
    contentPane.setMaximumSize(size);
    frame.pack();
    frame.setResizable(false);
    frame.setVisible(true);
    Fubarable:

    All I do is override the JComponent's paint(Graphics) method. I'm forgetful, but I think that calls paintComponent. I use the JComponent's inherited method createImage(int, int) to create an image that I may use as an image buffer for a double buffer. For the created image, I do use getGraphics(), because the image is drawn to in the JComponent's overridden paint method, before I draw it to the screen.

    Java Code:
    Image img = createImage(WIDTH, HEIGHT); // must be called after frame.setVisible(true);
    
    Graphics rearBufferContext = img.getGraphics();
    
    ...
    
    public void paint(Graphics g) {
      rearBufferContext.setColor(Color.BLUE); // this is the background color
      rearBufferContext.fillRect(0, 0, WIDTH, HEIGHT); // clear the image with the background color
      rearBufferContext.setColor(Color.BLACK);
      rearBufferContext.fillRect(10, 10, 20, 20);
      rearBufferContext.setColor(Color.WHITE);
      rearBufferContext.drawRect(15, 15, 20, 20);
      // other drawing here
      g.drawImage(img, 0, 0, null);
    }
    
    ...
    
    public void run() {
      while (running) { // boolean running
        // game math
        x += dx;
        y += dy;
        repaint();
        try {
          Thread.sleep(1000/20); // 20 FPS
        } catch (InterruptedException ie) {
        }
      }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I recommend that you override the JComponent's paintComponent method, not the paint method. In Swing, paint is responsible for painting the component, for painting the border, and for painting the component's child components. Unless your drawing deals with painting the border or the component's child components, you'll want to not override paint to avoid unwanted side effects.

    Also, I often call super.paintComponent(g) on the first line of my paintComponent method.

  5. #5
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    Thank you again. I've read why the superclass's paintComponent method is called first, but I've forgotten it because I've always used paint.

    I also have another question: What is the difference between the paintComponent and the paintComponents methods?

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

    Default

    I never use paintComponents, and the only time I've seen it used is when someone here accidentally overrides this in JFrame rather than paintComponent in a JPanel or JComponent derived class. So I didn't know the answer to this initially, but a simple look at the JFrame API shows that paintComponents is a Container method that paints all the child components held by the container.

  7. #7
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    Yeah, I looked at the API, but I didn't see any difference between the two.

  8. #8
    Join Date
    Feb 2011
    Location
    Florida
    Posts
    60
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I generally get the JFrame's contentPane, call setPreferredSize on this (then you don't have to worry about the title strip at the top), and then call pack on the JFrame.

    edit: I'm curious about this line:
    what do you mean by this? Do you do your drawing in the JComponent's paintComponent method (the preferred method) or use getGraphics (the non-preferred method)?
    I am also having a problem getting my initial frame the size I want. No matter what I try, the frame comes up sized the same as it was when last quit.
    Here is the code snippet:

    Java Code:
    public class MyName extends FrameView {
         public MyName (SingleFrameApplication app) {
              super(app);
              initComponents();
              app.getMainFrame().getContentPane()
                       .setPreferredSize(mainPanel.getPreferredSize());
              app.getMainFrame().pack();
    And mainPanel's getPreferredSize comes back with the proper size I want.

Similar Threads

  1. panel resizing
    By kumar_gemi in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 03:51 AM
  2. Image Resizing and DPi
    By Rob_ in forum Java Applets
    Replies: 4
    Last Post: 11-27-2008, 03:21 PM
  3. JFrame problem
    By vassil_zorev in forum AWT / Swing
    Replies: 1
    Last Post: 01-25-2008, 03:53 AM
  4. JFrame problem
    By saytri in forum New To Java
    Replies: 6
    Last Post: 01-11-2008, 06:12 PM
  5. Image resizing
    By alley in forum Java 2D
    Replies: 2
    Last Post: 11-13-2007, 11:10 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
  •