This discussion is archived
10 Replies Latest reply: Sep 16, 2011 4:40 AM by DarrylBurke RSS

Zoom in shape object

888400 Newbie
Currently Being Moderated
Hello, I am trying to zoom on a shape object with the AffineTransform class, and would like to know how to zoom from the center of my object graph, and this graphic object can be a circle, rectangle, polygon, etc. .. .
I'm lost in logic to perform the transformation.
Thanks.
  • 1. Re: Zoom in shape object
    801313 Explorer
    Currently Being Moderated
    If you can get away with it (I mean, you don't need the Rectangle.getWidth / getHeight etc etc methods - which can be gotten through getBounds(2D)) I highly recommend using Area, a subclass of Shape. You can directly apply an AffineTransform to an Area to create another Area.

    If you're interested, have a look at my Shape package ( http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/ui/shape/ ) and specifically at Resizeable
  • 2. Re: Zoom in shape object
    801313 Explorer
    Currently Being Moderated
    To answer your question more specifically:

    Question: Do you always know the center of the shape of your object? With a rectangle, circle or ellipse it's simple. But with an arbitrary irregular polygon there's no go way (aside from getBounds().getWidth()/2, getHeight()/ 2)

    BUT assuming you know the center, do an AffineTransform translate to the center of the object first, then do your scale

    Edited by: tjacobs01 on Sep 15, 2011 4:37 AM
  • 3. Re: Zoom in shape object
    888400 Newbie
    Currently Being Moderated
    Hello friend, thank you for your help, I have to identify the center of each graphic object of my system, but still did not understand how to zoom in, you could give me a more practical example?
    Thank you;)
  • 4. Re: Zoom in shape object
    801313 Explorer
    Currently Being Moderated
    Use AffineTranform.getScaleInstance() or Graphics2D.scale
  • 5. Re: Zoom in shape object
    888400 Newbie
    Currently Being Moderated
    Here's part of my code, entered the getScaleInstance now as I could zoom in from the center of my object, assuming for example that my object is a rectangle?
    thanks
    @Override
        public void paint(Graphics g) {
    
            super.paint(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.RED);
            AffineTransform tx = AffineTransform.getScaleInstance(scale, scale);
            g2.draw(tx.createTransformedShape(rect1));
        }
  • 6. Re: Zoom in shape object
    801313 Explorer
    Currently Being Moderated
    Is there a question? Are you asking me to run your partial code snippet for you?
    user2307826 wrote:
    Here's part of my code, entered the getScaleInstance now as I could zoom in from the center of my object, assuming for example that my object is a rectangle?
    thanks
    @Override
    public void paint(Graphics g) {
    
    super.paint(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.RED);
    AffineTransform tx = AffineTransform.getScaleInstance(scale, scale);
    g2.draw(tx.createTransformedShape(rect1));
    }
  • 7. Re: Zoom in shape object
    888400 Newbie
    Currently Being Moderated
    No, I'm wondering, just how to scale from the center of my object graph, I'm lost in this part because I think I need to accomplish the translation of the object after the zoom, and knowing I'm not doing this.
    Just posted this code for you to see how I'm doing;)
  • 8. Re: Zoom in shape object
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Cross posted
    http://www.coderanch.com/t/552785/GUI/java/Zoom-shape-object

    db
  • 9. Re: Zoom in shape object
    888400 Newbie
    Currently Being Moderated
    Hello, what's the problem I post the same question in multiple forums? I see no problem
  • 10. Re: Zoom in shape object
    DarrylBurke Guru Moderator
    Currently Being Moderated
    If you didn't see the problem, you wouldn't have used the word 'problem'

    It's your choice of word, not mine.

    db

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points