Mirrored shapes in Java (swing)

hello everyone,

I have a lesson about drawing and manipulating shapes in swing GUI

I have a problem. When I try to mirror my shape, I can't get the result I want

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, but the mirror image appears on the left... I want to mirror the shape and get the mirror shape in the same place instead of my JPanel

Thank you for your help

Solution

When you transform a shape using a simple affine transformation, such as scale (- 1,1), it will mirror horizontally at x = 0 That is, when you have an object in (300100), it will be after (- 300100)

In order to mirror an object "in place" (that is, at the X coordinate of its center point), you must

>Move the object so that its center is at x = 0 > mirror Object > move object back to its original position

This transformation sequence can (and should) be represented by a single affinetransform, which can be obtained by connecting the affinetransforms representing each step For example, you can create a similar method

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);
}

To draw a shape that flips horizontally (that is, mirrors) at its center, you can call

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

By the way: get rid of those caught exceptions! That's a terrible style For example Replace the exception check for printing "text not found on node"

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...
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>