【问题标题】:How to draw a diamond shape in java?如何在java中绘制菱形?
【发布时间】:2015-03-11 08:48:57
【问题描述】:

所以我必须画一个菱形。不是静态钻石,而是我自己拖动和绘制的钻石。我已经使用通用路径来做到这一点,但它正在绘制一个不直的钻石;钻石向左弯曲,它没有被吸引到我的鼠标指向的地方。

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

 private GeneralPath drawDiamond(int x1, int y1, int x2, int y2){
            
            int x = Math.min(x1, x2);
            int y = Math.min(y1, y2);

            // Gets the difference between the coordinates and

            int width = Math.abs(x1 - x2);
            int height = Math.abs(y1 - y2);
            
            Rectangle2D.Double diamond = new Rectangle2D.Double(x1,y1,width,height);
            
            GeneralPath connectedDiamond = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
            
            connectedDiamond.append(diamond, true);
            
            AffineTransform at = new AffineTransform();
            at.rotate(Math.toRadians(20));
            connectedDiamond.transform(at);
            
            return connectedDiamond;
        }

这是我的绘画方法:

public void paint(Graphics g) {           

            graphSettings = (Graphics2D) g;           

            graphSettings.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);

            graphSettings.setStroke(new BasicStroke(4));
           
            Iterator<Color> strokeCounter = shapeStroke.iterator();
            for (NamedShape s : shapes) {

                graphSettings.draw(s.getShape());

            }

            if (drawStart != null && drawEnd != null) {
                
                graphSettings.setComposite(AlphaComposite.getInstance(
                        AlphaComposite.SRC_OVER, 0.40f));

                graphSettings.setPaint(Color.LIGHT_GRAY);

                Shape aShape = null;
                    
                if(currentAction == 7){
                    
                    aShape = drawDiamond(drawStart.x, drawStart.y, drawEnd.x, drawEnd.y);
                }

                graphSettings.draw(aShape);
            }
        }

有人可以帮我做这件事吗?

【问题讨论】:

  • 不应该旋转90度吗?
  • @MadProgrammer 当我将它旋转到 90 度时,它根本没有被绘制。
  • @PieterDeBie 当我将角度设置为 45 度时,会绘制形状,但不会在我的鼠标位置绘制。我该如何解决>
  • 您的轴心点是否在矩形的中心?
  • 这可以解释我的意思:i.stack.imgur.com/KZ6Sc.png

标签: java arraylist geometry shape graphics2d


【解决方案1】:

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

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

【讨论】:

    【解决方案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();
        }
    
    }
    

    现在,您需要使用 AffineTransformation 或翻译 Graphics 上下文来定位它,但这并不难

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

    【讨论】:

    • 我也可以使用 path2D 来绘制三角形吗?但我想拖动并绘制三角形。我可以使用 path2D 来执行此操作吗?
    • @Lana 你最好相信...对于exampleexampleexample
    【解决方案3】:

    不如不旋转矩形,而是在矩形内的 4 个点之间画线:

    要点:

    请原谅我糟糕的 mspaint 技能。

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

    【讨论】:

    • 非常感谢 :) 我已经使用 line2D 来做到这一点。
    • 如果我没记错的话,你可以使用 GeneralPath 的 moveTo 和 lineTo 函数来画线,你可能想看看
    • 谢谢。这也可能有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多