0% found this document useful (0 votes)
5 views2 pages

Java Circle Drawing Class

The document defines a Cercle class that extends the Element class. The Cercle class represents circle objects and contains methods for setting circle properties, drawing circles to a Graphics2D object, getting a string representation of a circle, and determining if a point is selected within a circle. Key methods include a constructor that takes coordinates and color, a draw method that calculates circle coordinates based on the center and radius, and a selection method that uses a formula to check if a y-value is within the circle.

Uploaded by

isaacrobert374
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Java Circle Drawing Class

The document defines a Cercle class that extends the Element class. The Cercle class represents circle objects and contains methods for setting circle properties, drawing circles to a Graphics2D object, getting a string representation of a circle, and determining if a point is selected within a circle. Key methods include a constructor that takes coordinates and color, a draw method that calculates circle coordinates based on the center and radius, and a selection method that uses a formula to check if a y-value is within the circle.

Uploaded by

isaacrobert374
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import static [Link].

pow;
import static [Link];
import [Link];
import [Link].Graphics2D;

class Cercle extends Element {

Cercle(final Modele model, final int x1, final int y1, final Color couleur,
final String chainecouleur) {
super(model, x1, y1, couleur, chainecouleur);
}

Cercle(final Modele model, final String chaine) throws Exception {


super(model, chaine);
x2 = [Link]([Link](0));
y2 = [Link]([Link](1));
}

@Override
void set(final int x2, final int y2, final int nb) {
newx = x2;
newy = y2;
}

@Override
void dessine(final Graphics2D g2, final boolean parfait) {
if (parfait) {
final double rayon = sqrt(pow((oldx - newx), 2)
+ pow((oldy - newy), 2));
x1 = (int) (oldx - rayon);
y1 = (int) (oldy - rayon);
x2 = (int) (2 * rayon);
y2 = (int) (2 * rayon);
} else {
if (newx > oldx) {
x1 = 2 * oldx - newx;
x2 = 2 * (newx - oldx);
} else {
x1 = newx;
x2 = 2 * (oldx - newx);
}
if (newy > oldy) {
y1 = 2 * oldy - newy;
y2 = 2 * (newy - oldy);
} else {
y1 = newy;
y2 = 2 * (oldy - newy);
}
}
[Link](x1, y1, x2, y2);
}

@Override
void dessine(final Graphics2D g2) {
[Link](x1, y1, x2, y2);
}

@Override
String getTextFigure() {
final String chaine = "cercle" + "," + chaineCouleur + "," + x1 + ","
+ y1 + "," + x2 + "," + y2 + "\n";
return chaine;
}

@Override
boolean selection(final int x, final int y) {
float a, b, ycalcul;
boolean bool = false;
a = (x2 / 2);
b = (y2 / 2);
ycalcul = (float) sqrt(pow(b, 2)
- ((float) (pow((b / a), 2) * pow((x - x1 - a), 2))))
+ y1 + b;
if (y >= (y1 + b)) {
if ((y > (ycalcul - 10)) && (y < (ycalcul + 10))) {
bool = true;
}
yp = (int) (ycalcul - 10);
}
if (y <= (y1 + b)) {
if ((y > ((2 * (y1 + b)) - ycalcul - 10))
&& (y < ((2 * (y1 + b)) - ycalcul + 10))) {
bool = true;
}
yp = (int) (2 * (y1 + b) - ycalcul) - 10;
}
xp = x - 10;
return bool;
}

@Override
String getTexte() {
return "cercle," + chaineCouleur + "," + x1 + "," + y1 + "," + x2 + ","
+ y2;
}
}

You might also like