有没有简单的方法在JPanel上绘制一个圆圈? [英] Is there any easy way to draw a circle onto a JPanel?

查看:8369
本文介绍了有没有简单的方法在JPanel上绘制一个圆圈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用drawOval(x,y,width,height)方法,该方法假定x和y值代表要绘制的椭圆的左上角的坐标( javadoc



我希望x和y值代表圆的中心点。我该怎么做呢?谢谢

解决方案

一个简单的解决方案,如果您事先声明了宽度/高度,将会使用 drawOval 方法如下:

  drawOval(x  - (width / 2),y  - (高度/ 2),宽度,高度); 

这将确保(x,y)位于椭圆的中心。



为什么?



假设(x,y)是(10,10),并且您想画一个椭圆形的(高度,宽度)=(10,10)。

  drawOval(x,y,height width); 

然后会在(10,10),,左下角为(10 + 10,10 + 10)= (20,20)



开另一方面,如果使用

  drawOval(x  - (width / 2),y  - (height / 2),height ,宽度); 

椭圆的右上角将绘制在(10 - (10/2),10 - (10/2))= (5,5),底部将绘制在(5 + 10,5 + 10)=(15,15)。该中心将是(10,10):)

I'm having trouble using the drawOval(x,y,width,height) method, which assumes that the x and y values represent the coordinates of the "upper left corner of the oval to be drawn" (javadoc)

I want the x and y values to represent the center-point of a circle. How do I do this? Thanks

解决方案

A simple solution, if you have the width/height declared in advance, would be to use the drawOval method as follows:

drawOval( x - (width/2), y - (height/2), width, height);

This will ensure that (x, y) is at the center of the oval.

Why?

Let's say (x, y) is (10, 10) and you want to draw an oval of (height, width) = (10, 10).

drawOval(x, y, height width);

would then draw the top-right of the oval at (10, 10), and the bottom-left would be at (10 + 10, 10 + 10) = (20, 20).

On the other hand, if you use

drawOval( x - (width/2), y - (height/2), height, width);

the top-right of the oval would be drawn at ( 10 - (10/2), 10 - (10/2) ) = (5, 5) and the bottom would be drawn at (5 + 10, 5 + 10) = (15, 15). The center would then be (10, 10) :)

这篇关于有没有简单的方法在JPanel上绘制一个圆圈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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