java中的菱形,如何在Java中绘制菱形?

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.

a6GMi.png

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;

}

Here is my paint method:

public void paint(Graphics g) {

graphSettings = (Graphics2D) g;

graphSettings.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

graphSettings.setStroke(new BasicStroke(4));

Iterator 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?

解决方案

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

the points:

ihdup.png

excuse my poor mspaint skills.

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值