Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas isn't resizing with window in Java?

Tags:

java

swing

awt

I have a jFrame with a Canvas on it. When I run my program in Windows XP/Vista and resize the window the canvas resizes proportionately along with the window.

However, in Ubuntu linux, when I compile the same java application and resize my window, the Canvas stays the same size.

What do I need to do to make my Canvas resize with my window in both Windows and Linux? What is the deal with the discrepancy?

Main.java

public class Main {
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("BallBounce");
        frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));

        BallCanvas ballCanvas = new BallCanvas();

        frame.getContentPane().add(ballCanvas);
        frame.getContentPane().add(controlPanel);
        frame.pack();
        frame.setVisible(true);
    }
}

BallCanvas.java

public class BallCavnas extends Canvas {
    public BallCanvas()
    {
        setPreferredSize(new Dimension(640, 400));
        setIgnoreRepaint(true);

        ... various gui controls are wired up here
    }
    ... rest of canvas code
}

Edit: My source code is zipped up here incase someone wants to take a look:
http://www.filedropper.com/ballbounce

I've done the suggestions made by Dave Ray, and it still isn't resizing the Canvas? Remember, it resizes for me fine when I compile this java program and run it in windows. Only in linux does it does this to me. I'm also running Java 6 Sun 1.6.0.10 JVM, if it matters.

alt text http://img158.imageshack.us/img158/7642/screenshotww0.png

Perhaps my canvas is resizing by my BufferStrategy/Graphics aren't resizing ?

Edit 2: From the screenshot, it is definitely set to CENTER:

frame.getContentPane().add(ballCanvas, BorderLayout.CENTER);
frame.getContentPane().add(controlPanel, BorderLayout.SOUTH);

Resolved

Apparently the "Canvas" was getting resized but I was doing something weird with it's buffer strategy that wasn't allowing IT to be resized. I fixed it. Thanks everyone!


2 Answers

Perhaps, the layout manager is just attempting to honor your preferred size.

I would:

A) remove the preferred just to see what happens ( not a very good idea anyway )

or

B) not use canvas in first place but JComponent. After all Canvas is AWT component, and I not pretty sure how they work as today anyway. JComponent is a light weight component and since you're using a JComponent as container they would... mmhhh work better together?

Gee.. I'm giving voodoo programming suggestions now. Better get to work.

C) What have always worked for me. Make an small proof of concept, by adding step by step the stuff in my code. Start with empty canvas, then add the preffered size, then etc. etc. Chances are, the bug is on the paint method :P

Good luck.

:)

like image 68
OscarRyz Avatar answered Nov 19 '25 01:11

OscarRyz


Use a border layout instead:

frame.getContentPane().setLayout(new BorderLayout());
BallCanvas ballCanvas = new BallCanvas();
frame.getContentPane().add(ballCanvas, BorderLayout.CENTER);            
frame.getContentPane().add(controlPanel, BorderLayout.SOUTH);

Also get rid of setPreferredSize() in favor of frame.setSize() to set the initial size of the display.

like image 35
Dave Ray Avatar answered Nov 19 '25 02:11

Dave Ray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!