Hi Jim,

I'm trying to implement your suggested technique of using a stroke and areas
to expand a shape. I'm experiencing some difficulty at the 'initial corner'
even though I've specified a JOIN_MITER.

Included is a small test program. Also attaching a jpeg that shows the orig
shape in the center surrounded by the expanded shape.

Am I doing something wrong?

Thanks,

Ted Hill


==== begin code sample ====
import javax.swing.JPanel;
import javax.swing.JFrame;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Shape;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Area;



/**
 * Test use of Area and Stroke to expand a shape by a constant margin.
 */
public class AreaTest extends JPanel
{
    // the shape to expand
    private Shape       origShape;

    // constuctor
    public AreaTest()
    {
        setPreferredSize(new Dimension(600, 600));
        setBackground(Color.white);

        origShape = createTestShape();
    }

    /**
     * Paint the original shape, then paint the expanded shape surrounding
it
     */
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;

        g2.draw(origShape);

        g2.draw(getExpandedShape(origShape, 40));
    }

    private Shape getExpandedShape(Shape inShape, int pixelExpansion)
    {
        /**  times 2 because half goes inside shape and half outside */
        BasicStroke stroke = new BasicStroke(2 * pixelExpansion,
BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER);

        Shape outlineShape = stroke.createStrokedShape(inShape);

        Area outlineArea = new Area(outlineShape);
        Area origArea    = new Area(inShape);

        // expand the orig by adding the outline
        origArea.add(outlineArea);

        return origArea;
   }

    private GeneralPath createTestShape()
    {
        GeneralPath genPath = new GeneralPath();

        Point2D startEndPoint = new Point2D.Float(200, 200);

        genPath.moveTo((float) startEndPoint.getX(), (float)
startEndPoint.getY());
        genPath.lineTo(250, 250);
        genPath.lineTo(300, 300);
        genPath.lineTo(250, 400);
        genPath.lineTo((float) startEndPoint.getX(), (float)
startEndPoint.getY());

        return genPath;
    }


    public static void main(String[ ] args)
    {
        JFrame frame = new JFrame("Area Test");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane( ).add(new AreaTest( ), BorderLayout.CENTER);
        frame.pack( );
        frame.setVisible(true);
    }
}
==== end code sample ====

----- Original Message -----
From: "Jim Graham" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, November 26, 2002 4:37 PM
Subject: [JAVA2D] Path fun (in Re: to both STROKE_CONTROL and GeneralPath
expansion)


> Ted and Ram�n,
>
> Using the Area and BasicStroke classes, you can achieve any of the effects
> that you two have been asking for.  Assuming a source Shape object "S",
and
> wanting to outsideline, insideline, expand or contract it by N pixels...
>
> First get all your variables set up:
>
>         BasicStroke bs = new BasicStroke(N*2);
>         Shape outline = bs.createStrokedShape(S);
>         Area a1 = new Area(outline);
>         Area a2 = new Area(S);
>
> Then plug in the appropriate Area operation depending on what you want to
do:
>
> - Outline N pixels outside of a shape (S):
>
>         Area result = a1.subtract(a2);
>
> - Outline N pixels inside of a shape (S):
>
>         Area result = a1.intersect(a2);
>
> - Expand a shape (S) by N pixels:
>
>         Area result = a1.union(a2);
>
> - Contract a shape (S) by N pixels:
>
>         Area result = a2.subtract(a1);
>
> I've left out the various attributes on constructing the BasicStroke above
> since it depends on the "look" that you are going for.  I would imagine
that
> the expand/contract operations would want to specify a MITER join so that
> it doesn't cause the corners to get rounded or chopped off, but the inside
> and outside outlines could probably use all sorts of variations depending
> on personal taste...
>
>                                 ...jim
>
> 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".

<<attachment: triangleExpansion.jpeg>>

Reply via email to