-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDraw.java
61 lines (48 loc) · 1.56 KB
/
Draw.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;
import java.util.Random;
public class Draw extends JComponent{
private int width;
private int height;
private Ellipse2D.Double[] circles;
private Color[] colours;
public Draw(int w, int h, int numPlanets){
width = w;
height = h;
circles = new Ellipse2D.Double[numPlanets];
Random rand = new Random();
colours = new Color[numPlanets];
for(int i = 0; i < numPlanets; i++){
int r = rand.nextInt(230);
int g = rand.nextInt(230);
int b = rand.nextInt(230);
colours[i] = new Color(r, g, b);
}
}
@Override
protected void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D)g;
for(int i = 0; i < circles.length; i++){
g2d.setColor(colours[i]);
g2d.fill(circles[i]);
}
}
public void addCircle(int id, double x, double y, double r){
double xx = x-(r/2);
double yy = y-(r/2);
//System.out.println("Adding circle " + id + ": " + xx + ", " + yy);
circles[id] = new Ellipse2D.Double(xx, yy, r, r);
}
public void moveCircle(int id, double x, double y){
double oldx = circles[id].getX();
double oldy = circles[id].getY();
double r = circles[id].getWidth();
circles[id] = new Ellipse2D.Double(oldx + x, oldy + y, r, r);
}
public void redo(){
repaint();
}
}