如何在Java中绘制菱形? [英] How to draw a diamond shape in java?

查看:358
本文介绍了如何在Java中绘制菱形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我必须画一个菱形.不是静态钻石,而是我自己拖动并绘制的钻石.我已经用General Path做到了,但是它绘制的是不直的菱形.菱形向左弯曲,并且没有被绘制到鼠标指向的位置.

So i have to draw a diamond shape. Not a Static diamond but a diamond that i will myself drag and draw. I've used General Path to do it but it is drawing a diamond that is not straight; the diamond is bend to the left and it's not being drawn to where my mouse is pointed.

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

Here is my code to create the diamond shape. Can someone please help me solve this?

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

有人可以帮我做到这一点吗?

Can someone please help me to do this?

推荐答案

如何在矩形内部的4个点之间绘制线而不是旋转矩形:

How about instead of rotating a rectangle, you draw lines between 4 points inside a rectangle:

要点:

对不起我的mspaint技能.

excuse my poor mspaint skills.

但我希望你明白我的意思.您采用顶部中心,右侧中间,底部中心和左侧中间的点,并在它们之间画线(我认为是使用generalPath)

but i hope u get what i mean. u take the top center, middle right, bottom center and middle left points and draw lines between those (using generalPath, i think)

这篇关于如何在Java中绘制菱形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆