| Circle.java |
1 package fi.jyu.mit.graphics;
2
3 import java.awt.*;
4
5 /**
6 * Luokka ympyröille
7 * @author Markus Kivioja
8 *
9 */
10 public class Circle extends BasicShape {
11
12 private final RPoint center;
13 private double r;
14 private int resolution;
15 private int[] sXPoints;
16 private int[] sYPoints;
17
18 /**
19 * Luo uuden ympyrän
20 * @param x ympyrän keskipisteen x-koordinaatti
21 * @param y ympyrän keskipisteen y-koordinaatti
22 * @param r ympyrän säde
23 */
24 public Circle(double x, double y, double r) {
25 super();
26 this.center = new RPoint(x, y);
27 this.r = r;
28 initialize();
29 }
30
31 /**
32 * Luo uuden ympyrän
33 * @param x ympyrän keskipisteen x-koordinaatti
34 * @param y ympyrän keskipisteen y-koordinaatti
35 * @param z ympyrän keskipisteen z-koordinaatti
36 * @param r ympyrän säde
37 */
38 public Circle(double x, double y, double z, double r) {
39 super();
40 this.center = new RPoint(x, y, z);
41 this.r = r;
42 initialize();
43 }
44
45 /**
46 * Alustaa ympyrän asetukset
47 */
48 private void initialize() {
49 this.resolution = 360;
50 this.sXPoints = new int[resolution];
51 this.sYPoints = new int[resolution];
52 }
53
54 /**
55 * Asetetaan ympyrälle uusi säde
56 * @param r uusi säteen arvo
57 * @return tämä olio ketjuttamista varten
58 */
59 public Circle setR(double r) {
60 this.r = r;
61 redraw();
62 return this;
63 }
64
65 /**
66 * Piirretään ympyrä
67 * @see fi.jyu.mit.graphics.BasicShape#drawShape(java.awt.Graphics, fi.jyu.mit.graphics.Matrix)
68 */
69 @Override
70 protected void drawShape(Graphics g, Matrix a) {
71 Vector rp = new Vector(); // NOPMD, luulee vääräksi vektoriksi
72 SPoint sp = new SPoint(0,0);
73 for (int i = 0; i < resolution; i++) {
74 a.transform(this.r*Math.cos((2*Math.PI/resolution)*i)+this.center.getX(),
75 this.r*Math.sin((2*Math.PI/resolution)*i)+this.center.getY(),
76 this.center.getZ(),rp,sp);
77 sXPoints[i] = sp.getX();
78 sYPoints[i] = sp.getY();
79 }
80 g.drawPolygon(sXPoints, sYPoints, resolution);
81 }
82 }
83