use*_*538 3 java swing mirror shape graphics2d
大家好,
我有一个功课,涉及在Swing GUI中绘制和操作形状.
我遇到了一个问题,当我试图镜像我的形状时,我得不到我想要的结果.
drawallnodes方法在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);
            }
        }
    }
}
当我镜像Shape时,例如我在右侧绘制,但镜像图像出现在左侧...我想镜像形状并在同一个地方获得镜像形状而不是我的jpanel ....
谢谢您的帮助
当您使用简单的仿射变换转换形状时,scale(-1,1)它将在x = 0时水平镜像.也就是说,当你有一个物体在(300,100)时,它将在(-300,100)之后.
为了在适当的位置镜像对象(即,在其中心点的x坐标处),你必须
这个变换序列可以(并且应该)由单个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被抓到那里!那是一种可怕的风格.例如,替换打印"在节点处找不到文本"的异常检查
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...
}