This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How do I scale a shape? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do I scale a shape?" Watch "How do I scale a shape?" New topic
Author

How do I scale a shape?

Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I'm drawing shapes on top of an image using a subclass of JLabel. The problem is the image can be scaled (zoomed) in or out and I need each Shape to be scaled to be larger or smaller with the image. This is pretty easy with something simple like a Rectangle, just multiply the width, height, x and y values by the same scale as the image. I need to scale a generic Shape, however, not just a Rectangle and I don't see how I could do this.
Pavan Panduga
Ranch Hand

Joined: Jun 12, 2002
Posts: 68
getScaledInstance(....) method of java.awt.Image is what you are looking for

I had something like this to scale down an image by 50%

Hope this helps,
Gideon Goodwin
Greenhorn

Joined: Dec 10, 2004
Posts: 13
This should be a simple matter of multiplying the x and y coord of each point in the shape by your scale factor. Depending on how the points are situated relative to the shape-origin, you might need to renormalize your coords after the scale so that they are still optimized to the origin.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Pavan:
That won't work, the shapes aren't part of the Image, they're drawn on top of the Image.

Gideon:
Yeah, I understand that but I'm not sure how to do that with the methods supplied by the Shape interface. Remember, I'm working with a generic Shape not directly with any of the classes that implement it. There doesn't seem to be any way to create a new scaled Shape or even change the existing one.

Am I going to have to check and see what kind of Shape it is and write methods for dealing with scaling every one of them? That's going to be a nightmare to maintain. There's got to be a better solution. The problem is basically that I want to draw Shapes on top of an image and I want them to scale with the image as it zooms in and out.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
AffineTransform has a handy method createTransformedShape. You might want to check out the reported problems of using AffineTransform to scale BufferedImages on the fly in j2se 1.5.
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
Ken,

If you are using a Graphics2D context to draw the shapes onto then take a look at its scale() method. I'm not sure how this would work alongside your image scaling though.
[ December 14, 2004: Message edited by: Ben Wood ]

SCJP 1.4, www.gsi3d.org.uk
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Craig:
Once again it's not scaling the image that I'm concerned about. I'm working with TIF images and having to use JAI and doing it through JAI. I'm drawing shapes on top of that image by overriding the paint method and using a Graphics2D and I can't figure out how to scale a generic shape. I can do it with any subclass very easily, but the methods provided with Shape alone are another matter.

Ben:
Thanks, I hadn't spent much time looking at the Graphics2D class to see if it had anything for scaling there. Like they always say, if you can't find what you're looking for look some place else. I'll check it out.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
createTransformedShape takes a Shape as an argument and returns a Shape. I only mentioned the BufferedImage scaling bug because I don't know if the same problem, described in the report, exists for using a scale instance of AffineTransform for shapes. The idea behind my mentioning the image bug was:
1 — that the issue is something to be aware concerning graphics scaling work in j2se 1.5 and,
2 — as a possible avenue of research if the Shape scaling ran into trouble.
The createTransformedShape method is not meant for use with images.
 
 
subject: How do I scale a shape?