Forums Register Login

Resizing Graphics2D Objects with JFrame Resize

+Pie Number of slices to send: Send
I am creating a GUI that contains several Graphics2D ellipses and drawStrings. I have been experimenting with different methods of dynamic resizing and had no luck. I really want the objects to scale with the JFrame as it is resized. If it gets smaller I want to make the objects smaller and closer together and vice versa is the JFrame is made larger.

I have tried all of the different layout managers and even overloading the paint and paintComponent methods in different ways. I can't figure out how to get the variables that dictate size to update with the window size. Here is a self-contained copy of my code that has been set back to a "working" state:

+Pie Number of slices to send: Send
Welcome to the Ranch

You can't resize objects on display by resizing the frame. You would have to scale the Graphics object.
The Graphics object is a subclass of Graphics2D so you can do several things. First you want to call the super version of the method. Then I think you want to duplicate the Graphics object, so any changes you make with the affine transforms will be removed if you need the original Graphics object again. Then consider scaling. You can scale in x and y directions. If you scale y by -1.0, you can get the y coordinates to run upwards like when we did maths at school.You often can reverse some affine transforms; it is usually shear that causes problems.
+Pie Number of slices to send: Send
There are 4 affine transforms: scale, shear, rotate and translate.
+Pie Number of slices to send: Send
@Campbell Ritchie Thanks for the response. I somewhat understand what you are saying and I have read about affine transforms. I think that my problem here is how to implement it.

I keep thinking I need to have a transform that changes the size of the ellipses in DrawEllipses and changes the coordinates in POINT_LIST but I can't get either to work.

Where/how do I go about implementing that into the code I posted?
+Pie Number of slices to send: Send
I think you will have to look up the four affine transforms somewhere. The matrix multiplications which implement them are interesting but difficult to understand, and you won't come to any harm if you simply forget them. You can implement affine transforms by applying an AffineTransform object to your Graphics2D object. Or you can get the same effect with the translate rotate scale and shear methods.
Start with the simplest which is (probably) translate. Try two versions of the app, each with one ellipse, and in one case use translate(-200, 0), which causes all the pixels in the display to jump 200 px to the left, rather like something out of the Hitch‑hiker's Guide to the Galaxy. Observe the difference between the two.
Now get yourself some component which you can use to alter the translation. Maybe a JSlider. Add a listener which allows you to change the translation and see what happens when you slide the slider up and down.
The repeat the procedure with scale instead of translation. Then try rotation, though maybe a rectangle would be better than an Ellipse; it is easier to see the turning. Read the documentation about rotate carefully.
+Pie Number of slices to send: Send
I would draw everything to a BufferedImage at 100%, then drawImage(0, 0, getWidth(), getHeight(), this) in a paintComponent(...) override. But then I'm lazy.

A little extra code might be required for scaling the image while retaining the aspect ratio, if that's a requirement.
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 5142 times.
Similar Threads
How to draw points in Java?
Help w/ displaying several .PNG files
how to keep drawn free hand painted image when resizing the window
Movement is wacky, just increases.
JPanel inverted
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:12:33.