10 Replies Latest reply: Sep 16, 2011 6:40 AM by DarrylBurke RSS

    Zoom in shape object

    888400
      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
          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
            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
              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
                Use AffineTranform.getScaleInstance() or Graphics2D.scale
                • 5. Re: Zoom in shape object
                  888400
                  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
                    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
                      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
                        Cross posted
                        http://www.coderanch.com/t/552785/GUI/java/Zoom-shape-object

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

                            It's your choice of word, not mine.

                            db