• Post Reply Bookmark Topic Watch Topic
  • New Topic

Resizing Graphics2D Objects with JFrame Resize  RSS feed

 
Rob Bartnicki
Greenhorn
Posts: 4
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are 4 affine transforms: scale, shear, rotate and translate.
 
Rob Bartnicki
Greenhorn
Posts: 4
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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?
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!