user3050538
user3050538

Reputation: 89

Mirror Shape in JAVA(Swing)

Helo everyone,

I have a homework which involves drawing and manipulating shapes in a Swing GUI.

I got into a problem that I dont get the results I want when I try to mirror my shapes.

The drawallnodes method is called in Jpanels paintComponent.

public void drawallnodes(ArrayList<DevicesEditor> nodes, Graphics2D g2)
{
    int arraysize = nodes.size();
    ArrayList<DevicesEditor> temparray;

    AffineTransform at = new AffineTransform();

    if (nodes.size() != 0)
    {
        System.out.println("nodes.size " + nodes.size());
        if (currentarrayindex >= 0)
        {

            AffineTransform afx = new AffineTransform();// for rotate

            for (int i = 0; i <= currentarrayindex; i++)
            {
                if (nodes.get(i).getWasAngleChanged())
                {
                    afx.rotate(
                        Math.toRadians(nodes.get(i).getAngleInDegrees()), 
                        nodes.get(i).getCenter().x,
                        nodes.get(i).getCenter().y);
                    nodes.get(i).setShape(
                            afx.createTransformedShape(nodes.get(i).getShape()));
                    nodes.get(i).setWasAngleChanged(false);
                    nodes.get(i).setokrajRectangle();
                }

                try
                {
                    Rectangle r = nodes.get(i).getShape().getBounds();
                }
                catch (Exception e)
                {
                    System.out.println(
                        "Exception found at getbounds, no shape with getbounds found");
                }

                AffineTransform saveXform = g2.getTransform();

                g2.setColor(nodes.get(i).getColor());

                int w = getWidth();

                // it gets the JPanels width, which is set to 758px
                at = AffineTransform.getTranslateInstance(w, 0); 

                System.out.println("*********Get width of shape: " + w);
                at.scale(-1, 1); // mirror -x, y
                g2.setPaint(Color.red);
                g2.draw(at.createTransformedShape(nodes.get(i).getShape()));

                try
                {
                    g2.drawString(nodes.get(i).getText(), 
                        (int) nodes.get(i).getCenter().getX(), 
                        (int) nodes.get(i).getCenter().getY());
                }
                catch (Exception e)
                {
                    System.err.println("No text found at node");
                }

                try
                {
                    g2.draw((Shape) nodes.get(i).getShape());
                }
                catch (Exception e)
                {
                    System.err.println("No shape found at node");
                }

                // g2.transform(AffineTransform.getRotateInstance(0, 1));

                g2.setTransform(saveXform);

            }
        }
    }
}

When I mirror the Shape,for example I draw on the right side,but the mirrorred image appears on the left side...I want to mirror the shape and get the mirrorred shape at the same place not accros my jpanel....

Thank you for your help

Upvotes: 3

Views: 3440

Answers (1)

Marco13
Marco13

Reputation: 54669

When you transform the shape with a simple affine transform like scale(-1,1) then it will be mirrored horizontally at x=0. That is, when you have an object at (300,100), it will afterwards be at (-300,100).

In order to mirror the object "in place" (that is, at the x-coordinate of its center point) you have to

  • Move the object so that its center is at x=0
  • Mirror the object
  • Move the object back to its original position

This sequence of transforms can (and should) be represented by a single AffineTransform that can be obtained by concatenating AffineTransforms that represent the individual steps. For example, you could create a method like

private static Shape mirrorAlongX(double x, Shape shape)
{
    AffineTransform at = new AffineTransform();
    at.translate(x, 0);
    at.scale(-1, 1);
    at.translate(-x, 0);
    return at.createTransformedShape(shape);
}

In order to paint a shape that is flipped horizontally (i.e. mirrored) at its center, you can then call

g.fill(mirrorAlongX(shape.getBounds2D().getCenterX(), shape));

BTW: Get rid of all these Exceptions being caught there! That's a horrible style. E.g. replace the exception check that prints "No text found at node" with something like

String text = nodes.get(i).getText();
if (text != null)
{
    g2.drawString(text, ...);
}
else
{
    System.err.println("No text found at node"); // May not even be necessary...
}

Upvotes: 7

Related Questions