Mirror Shape in JAVA (Swing)

Hello everybody,

I have a homework that includes drawing and manipulating shapes in the Swing GUI.

I have a problem that I don’t get the results that I want when I try to reflect my shapes.

The drawallnodes method is called in the 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 mirror image appears on the left side ... I want to mirror the shape and get the mirror shape in the same place as not accros my jpanel ....

thanks for the help

+3
source share
1 answer

, scale(-1,1), x = 0. , (300 100), (-300, 100).

" " (.. x ),

  • , x = 0

( ) ​​ AffineTransform, AffineTransforms, . , ,

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

, (.. ) ,

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

: Exceptions, ! . . , " 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...
}
+7

All Articles