Hi Jim,
Thank you for the code.
This does a beautiful job of 'expanding' and 'contracting' while preserving
the x and y aspect ratio of the shapes.
I would also like to be able to expand and contract by a 'constant margin.'
For example, in your sample using the AffineTransform, if we have a
rectangle that has
width = 2
height = 1
and we scale by 2,
new width = 4
new height = 2
so that the 'margin' in the x direction is wider than the margin in the y
direction.
What I'd like here for the 'expanded' values is
new width = 4
new height = 3
so that the margin is a constant size, in this example, one unit.
For simple shapes like rectangles, I think that I could play around with the
scaleX and scaleY values
AffineTransform trans = AffineTransform.getScaleInstance(scaleX,
scaleY);
so that the margin between the original shape and its expansion or
contraction would be equal in both x and y. However, for less regular
shapes, e.g. a 'kidney' or 'crescent' shape, I'm not sure how to do this.
Thank you for your help, it is much appreciated,
Ted Hill
----- Original Message -----
From: "Jim Bucher" <[EMAIL PROTECTED]>
To: "Ted Hill" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Tuesday, November 26, 2002 4:54 PM
Subject: Re: [JAVA2D] General Path contraction/expansion algorithm (again)
> // I think this is what you want.
> // Expansion / contraction is the same as applying a scaling transform.
>
> import javax.swing.*;
> import java.awt.*;
> import java.awt.geom.*;
>
>
> public class Scaler extends JPanel {
> double R_CENTER_X = 50;
> double R_CENTER_Y = 50;
> double E_CENTER_X = 150;
> double E_CENTER_Y = 150;
> double WIDTH = 10;
> double HEIGHT = 20;
>
> Shape rect = new Rectangle2D.Double(0,0,WIDTH,HEIGHT);
> Shape ellipse = new Ellipse2D.Double(0,0,WIDTH,HEIGHT);
> AffineTransform rectTrans =
> AffineTransform.getTranslateInstance(R_CENTER_X, R_CENTER_Y);
> AffineTransform ellipseTrans =
> AffineTransform.getTranslateInstance(E_CENTER_X, E_CENTER_Y);
>
> public void init() {
> // put the center of the rect and ellipse at the origin
> AffineTransform trans =
> AffineTransform.getTranslateInstance(-WIDTH/2, -HEIGHT/2);
> rect = trans.createTransformedShape(rect);
> ellipse = trans.createTransformedShape(ellipse);
> setBackground(Color.black);
> }
>
> public void scale(Graphics2D g, double scaleFactor) {
> AffineTransform trans =
> AffineTransform.getScaleInstance(scaleFactor, scaleFactor);
> rectTrans.concatenate(trans);
> ellipseTrans.concatenate(trans);
> Shape shape = rectTrans.createTransformedShape(rect);
> g.draw(shape);
> shape = ellipseTrans.createTransformedShape(ellipse);
> g.draw(shape);
> }
>
> protected void paintComponent(Graphics g) {
> super.paintComponent(g);
> Graphics2D g2 = (Graphics2D) g;
> g2.setColor(Color.white);
> scale(g2, 1);
> g2.setColor(Color.blue);
> scale(g2, 2);
> g2.setColor(Color.green);
> scale(g2, 3);
> g2.setColor(Color.red);
> scale(g2, .5);
> }
>
> public static void main(String[ ] args) {
> JFrame frame = new JFrame("scale test");
> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> Scaler scaler = new Scaler();
> scaler.init();
> frame.getContentPane().add(scaler);
> frame.setSize(new Dimension(300,300));
> frame.setVisible(true);
> }
>
> }
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".