简体   繁体   English

DrawOval()位于一个点之上

[英]DrawOval() on top of a point

I use DrawOval function to draw a circle on top of a point in my Java project: 我使用DrawOval函数在我的Java项目中的一个点上绘制一个圆:

g2d.drawOval(instruction.getX(), instruction.getY(), instruction.getWidth(), instruction.getHeight());

The problem is that my circle is drawn such that the target point is outside the circle instead of at the center. 问题是我的圆圈被绘制成使得目标点在圆圈之外而不是在中心。

I tried to illustrate what happens on the left and what I want to have happen on the right: 我试图说明左边发生的事情以及我想要发生在右边的事情:

I don't have any experience with drawing in Java, how do I solve this? 我没有使用Java绘图的经验,我该如何解决这个问题?

The oval is drawing at the give point so that it's top, left corner is at the x/y position. 椭圆形在给定点处绘制,使其位于顶部,左角位于x / y位置。 You need to offset the x/y by half the width/height... 你需要将x / y偏移宽度/高度的一半......

int width  = instruction.getWidth();
int height = instruction.getHeight();
g2d.drawOval(instruction.getX() - (width / 2), instruction.getY() - (height / 2), width, height);

Check Graphics#drawOval for more details 检查Graphics#drawOval以获取更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM