JAVA(Swing)中的镜像形状

3
大家好,
我有一个涉及在Swing GUI中绘制和操作形状的作业。
当我尝试镜像我的形状时,遇到了无法得到想要结果的问题。
drawallnodes方法在JPanel的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);

            }
        }
    }
}

当我镜像形状时,例如我在右侧绘制,但是镜像图像出现在左侧...我想要镜像形状并获得相同位置的镜像形状而不是跨越我的jpanel....

谢谢你的帮助

1个回答

7
当你使用简单的仿射变换(如scale(-1,1))来转换形状时,它将在处水平镜像。也就是说,当你有一个在(300,100)的对象时,它之后会在(-300,100)。
为了在“原地”镜像对象(也就是在其中心点的x坐标处),你需要:
  • 移动对象使其中心在x=0
  • 镜像对象
  • 把对象移回其原始位置
这些变换序列可以(也应该)通过连接表示各个步骤的AffineTransforms来表示成单一的AffineTransform。例如,你可以创建一个方法:
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));

顺便说一下:把那里捕获的所有异常都去掉!这是一种可怕的风格。例如,将打印“未找到节点文本”的异常检查替换为以下内容:

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...
}

感谢您的快速回复。您的解决方案似乎是正确的,非常感谢。您能帮我添加缩放功能吗?另外,我如何给您的快速答案加分 :) 祝好 - user3050538
@user3050538 当您在创建有关面板缩放或特定形状缩放的问题时,我或StackOverflow上的其他数百万人肯定会回答。 - Marco13

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接