简体   繁体   English

如何使用drawOval()创建戒指?

[英]How to create a ring using drawOval()?

I am new to Java. 我是Java新手。 I am creating a thick ring in a applet. 我在小程序中创建了一个厚实的戒指。 I am using drawOval method in a for loop. 我在for循环中使用drawOval方法。 This creates multiple rings but that are not centered. 这将创建多个环,但它们不会居中。 Please check the image and help me as soon as possible! 请检查图片,并尽快帮助我!

非中心环

You might be able to use fillOval() in order to avoid drawing many different ovals so that you only have to draw 2, one with the edge color and another with the background color. 您可能可以使用fillOval()来避免绘制许多不同的椭圆,从而只需要绘制2,一个具有边缘颜色,另一个具有背景颜色。 To center the oval, try something like fillOval(origX + changeInThickness / 2, origY + changeInThickness / 2, origWidth - changeInThickness, origHeight - changeInThickness) 要使椭圆居中,请尝试类似fillOval(origX + changeInThickness / 2, origY + changeInThickness / 2, origWidth - changeInThickness, origHeight - changeInThickness)

Here every ring you draw is centered but due to consecutive drawn ring it doesn't appear to be. 在这里,您绘制的每个圆环都居中,但是由于连续绘制的圆环似乎没有居中。 Therefore you can use Mouse Pressed Event to draw or fill oval on each click. 因此,您可以使用“鼠标按下事件”在每次单击时绘制或填充椭圆形。

    onMousePressed(Event e)
{
    Graphics g= getGraphics();
    g.fillOval(e.getX(),e.getY(),size,size);

}

Just observer and try .... your own logic. 只是观察者,然后尝试....您自己的逻辑。

As you know the ellipse drawn is within a bounding rectangle, you can use something like this. 如您所知,绘制的椭圆在边界矩形内,您可以使用类似的东西。 This is for 7 concentric circles. 这是用于7个同心圆。 You can customize the the distance, decide whether they are growing concentric circles or shrinking concentric circles, the number of circles by changing the for loop conditions. 您可以自定义距离,通过更改for循环条件来确定它们是同心圆还是收缩同心圆,以及圆数。

import java.awt.*;;
import java.applet.*;

/*
<applet code="Ellipses" width=400 height=400>
</applet>
*/

public class Ellipses extends Applet
{
    public void paint(Graphics g)
    {
        int i,j,k,l;
        for(i=170,j=170,k=50,l=50;i>=110;i-=10,j-=10,k+=20,l+=20)
        g.drawOval(i,j,k,l);
    }
}

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

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