• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I scale a shape?

 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic