如何在java中绘制菱形?

如何在java中绘制菱形?,java,arraylist,geometry,shape,graphics2d,Java,Arraylist,Geometry,Shape,Graphics2d,所以我必须画一个菱形。不是一颗静止的钻石,而是一颗我将自己拖动和绘制的钻石。我用了一般的方法,但它是画一个不直的钻石;钻石向左弯曲,它没有被吸引到我的鼠标指向的地方 这是我创建菱形的代码。有人能帮我解决这个问题吗 private GeneralPath drawDiamond(intx1、inty1、intx2、inty2){ int x=数学最小值(x1,x2); int y=数学最小值(y1,y2); //获取坐标和之间的差 int width=Math.abs(x1-x2); 内部高度

所以我必须画一个菱形。不是一颗静止的钻石,而是一颗我将自己拖动和绘制的钻石。我用了一般的方法,但它是画一个不直的钻石;钻石向左弯曲,它没有被吸引到我的鼠标指向的地方

这是我创建菱形的代码。有人能帮我解决这个问题吗

private GeneralPath drawDiamond(intx1、inty1、intx2、inty2){
int x=数学最小值(x1,x2);
int y=数学最小值(y1,y2);
//获取坐标和之间的差
int width=Math.abs(x1-x2);
内部高度=数学绝对值(y1-y2);
矩形2D.Double菱形=新矩形2D.Double(x1,y1,宽度,高度);
GeneralPath connectedDiamond=新的GeneralPath(GeneralPath.WIND\u偶数\u奇数);
connectedDiamond.append(菱形,true);
AffineTransform at=新的AffineTransform();
旋转(数学托拉迪安(20));
连接钻石变换(at);
返回连接的Diamond;

}
与其旋转矩形,不如在矩形内的4个点之间画线:

要点:

请原谅我的绘画技巧差


但我希望你明白我的意思。u取上中心点、右中点、下中心点和左中点,并在这些点之间画线(我认为使用generalPath)

将菱形创建为具有顶点的多边形会更简单

(x + Width/2, y)
(x + Width, y + Height/2)
(x + Width/2, y + Height)
(x, y + Height/2)

2D Shape API实际上非常强大,我最喜欢的类之一是
Path2D
,例如,它允许您简单地“绘制”虚拟形状

public class Diamond extends Path2D.Double {

    public Diamond(double width, double height) {
        moveTo(0, height / 2);
        lineTo(width / 2, 0);
        lineTo(width, height / 2);
        lineTo(width / 2, height);
        closePath();
    }

}
现在,您需要使用
仿射转换
或翻译
图形
上下文来定位它,但这并不难

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class JavaApplication251 {

    public static void main(String[] args) {
        new JavaApplication251();
    }

    public JavaApplication251() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Diamond diamond;

        public TestPane() {
            diamond = new Diamond(100, 100);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int x = (getWidth() - diamond.getBounds().width) / 2;
            int y = (getHeight()- diamond.getBounds().height) / 2;
            AffineTransform at = AffineTransform.getTranslateInstance(x, y);
            Shape shape = at.createTransformedShape(diamond);
            g2d.setColor(Color.YELLOW);
            g2d.fill(shape);
            g2d.setColor(Color.RED);
            g2d.draw(shape);
            g2d.dispose();
        }

    }

    public class Diamond extends Path2D.Double {

        public Diamond(double width, double height) {
            moveTo(0, height / 2);
            lineTo(width / 2, 0);
            lineTo(width, height / 2);
            lineTo(width / 2, height);
            closePath();
        }

    }

}


你不应该把它旋转90度吗?@MadProgrammer当我把它旋转到90度时,它根本没有被画出来。@PieterDeBie当我把它旋转到45度时,形状被画出来了,但它没有在我的鼠标位置被画出来。如何解决>是矩形中心的轴心点?这也许可以解释我的意思:非常感谢:)我已经用line2D来做了。如果我没有弄错的话,你可以使用GeneralPath的moveTo和lineTo函数来画线,你可能想看看谢谢。这可能也很有用。我也可以使用path2D绘制三角形吗?但是我想拖拽画三角形。我能用path2D来做这个吗?@Lana你最好相信它……为了,还有