public void paint (Graphics g) {
int radius = 10;
int pos_X = 100;
int pos_Y = 100;
g.setColor(Color.green);
g.fillOval(pos_X, pos_Y, radius, radius);
}
--
JUAN CARLOS JAIMES
S.I.T
SINVA LTDA
Hiya,
Learn how to use the API. Download it and keep it open when writing code.
You can find it at http://java.sun.com/docs/index.html
Have a look at the Graphics class. Examine the methods. You can quickly see
what parameters they need and what they return. Yes, fillOval() is the
method to use, but the example already given to you is somewhat misleading.
The radius is not used, the last two parameters are for the width and height
of the bounding rectangle.
The method takes 4 integer parameters:
fillOval(int x, int y, int width, int height)
x and y are not the centre. They are the upper right corner of the bounding
rectangle. As in the example given to you you can call the variable that you
pass to fillOval 'radius', and it still works, but that doesn't make it a
radius. The radius (if height = width) is actually equal to width divided by
2. The first time I saw the drawOval method I didn't much like it (you get
used to it). It seemed more intuitive to me to design a cirle method that
requires a centre point and radius. I don't believe there is such a method
in the API, but you could easily write one. Some like the following. Note
that in this case, x and y are the centre point and radius is the radius.
myFillOval(int x, int y, int radius)
{
fillOval(x - radius, y - radius, 2 * radius, 2 * radius)
}
If you often require a circle method for when you know the centre point and
radius then write such a method and keep it in your personal library of
classes. Use it when you need it.
Good Luck
Paul
> myFillOval(int x, int y, int radius)
> {
> fillOval(x - radius, y - radius, 2 * radius, 2 * radius)
> }
>
Hi,
Sorry, the code snippet that I gave you was a little careless. It won't run.
I was just trying to give an example of hiding the displacement inside a
method that you create yourself. The method myFillOval() calls an instance
method from the class java.awt.Graphics, therefore it needs a Graphics
object. I have taken the time to put the corrected version to work in an
applet that runs (I've tested it) so you can see what I meant. I am sure
there are more elegant ways to do this, but I am still learning, and this is
one way.
import java.applet.Applet;
import java.awt.*;
public class Test extends Applet
{
public void paint(Graphics g)
{
g.drawString("hello", 50, 50);
g.setColor(Color.green);
g.fillOval(100, 100, 100, 100);
g.setColor(Color.red);
MyGraphics.myFillOval(150, 150, 50, g);
}
}
class MyGraphics
{
public static void myFillOval(int x, int y, int radius, Graphics page)
{
page.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
}
}
You won't see the green circle, because the red circle is drawn right on top
of it. Two ways of drawing the circle.
Cheers,
Paul
drawOval(x, y, width, height)
or
fillOval(x, y, width, height)
The Clock sample from sun demos shows an example how to generate a circle from
line segments, calculating points of the circumference with given angle step