-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawComponent.java
More file actions
148 lines (147 loc) · 3.75 KB
/
DrawComponent.java
File metadata and controls
148 lines (147 loc) · 3.75 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.geom.Ellipse2D;
import java.awt.Rectangle;
import java.util.Random;
import java.util.ArrayList;
public class DrawComponent extends JPanel{
private int shape,red,blue,green,ballcolor,white,speed,count;
private ArrayList<Shape> SHAPE;
private Random generator;
public DrawComponent(){
shape=1;
red=0;
blue=0;
green=0;
ballcolor=0;
white=0;
speed=1;
count=0;
generator=new Random();
int i=generator.nextInt(100)+100;
SHAPE=new ArrayList<Shape>();
for(int j=0;j<i;j++){
double x=generator.nextDouble()*getWidth();
double y=generator.nextDouble()*getHeight();
SHAPE.add(new Shape(x,y));
}
}
public void paintComponent(Graphics g){
Graphics2D g2=(Graphics2D) g;
count++;
g2.clearRect(0, 0, getWidth(), getHeight());
g2.setColor(CreateColor());
g2.fillRect(0, 0, getWidth(), getHeight());
if(speed==1){
if(ballcolor==0) g2.setColor(RandomColor());
else if(ballcolor==1){
if(white==0){
g2.setColor(Color.white);
white=1;
}
else {
g2.setColor(Color.BLACK);
white=0;
}
}
for(int i=0;i<SHAPE.size();i++){
if(shape==1){
g2.fill(new Ellipse2D.Double(SHAPE.get(i).getX(),SHAPE.get(i).getY(),SHAPE.get(i).getR(),SHAPE.get(i).getR()));
}
else if(shape==2){
g2.fill(new Rectangle((int)SHAPE.get(i).getX(),(int)SHAPE.get(i).getY(),(int)SHAPE.get(i).getR(),(int)SHAPE.get(i).getR()));
}
}
}
else if(speed!=1){
if(count%speed==0){
generator = new Random();
int x=generator.nextInt(getWidth());
int y=generator.nextInt(getHeight());
for(int i=0;i<SHAPE.size();i++){
SHAPE.get(i).setCordinate(x, y);
}
if(ballcolor==0) g2.setColor(RandomColor());
else if(ballcolor==1){
if(white==0){
g2.setColor(Color.white);
white=1;
}
else {
g2.setColor(Color.BLACK);
white=0;
}
}
for(int i=0;i<SHAPE.size();i++){
if(shape==1){
g2.fill(new Ellipse2D.Double(SHAPE.get(i).getX(),SHAPE.get(i).getY(),SHAPE.get(i).getR(),SHAPE.get(i).getR()));
}
else if(shape==2){
g2.fill(new Rectangle((int)SHAPE.get(i).getX(),(int)SHAPE.get(i).getY(),(int)SHAPE.get(i).getR(),(int)SHAPE.get(i).getR()));
}
}
}
else{
if(ballcolor==0) g2.setColor(RandomColor());
else if(ballcolor==1){
if(white==0){
g2.setColor(Color.white);
white=1;
}
else {
g2.setColor(Color.BLACK);
white=0;
}
}
for(int i=0;i<SHAPE.size();i++){
if(shape==1){
g2.fill(new Ellipse2D.Double(SHAPE.get(i).getX(),SHAPE.get(i).getY(),SHAPE.get(i).getR(),SHAPE.get(i).getR()));
}
else if(shape==2){
g2.fill(new Rectangle((int)SHAPE.get(i).getX(),(int)SHAPE.get(i).getY(),(int)SHAPE.get(i).getR(),(int)SHAPE.get(i).getR()));
}
}
}
}
}
public void setSpeed(int speed){
this.speed=speed;
}
public void gather(int shape,int red,int blue,int green,int ballcolor){
this.shape=shape;
this.red=red;
this.blue=blue;
this.green=green;
this.ballcolor=ballcolor;
for(int i=0;i<SHAPE.size();i++){
if(SHAPE.get(i).checkBoundaries(getWidth(),getHeight())==false){
SHAPE.get(i).regenerate(getWidth(), getHeight());
}
SHAPE.get(i).move();
}
repaint();
}
public Color CreateColor(){
int r,b,g;
if(red==1) r=255;else r=0;
if(blue==1) b=255;else b=0;
if(green==1) g=255;else g=0;
return new Color(r,g,b);
}
public Color RandomColor(){
int r,b,g;
generator=new Random();
r=generator.nextInt(255);
b=generator.nextInt(255);
g=generator.nextInt(255);
return new Color(r,g,b);
}
public void mouse(int x, int y){
for(int i=0;i<SHAPE.size();i++){
SHAPE.get(i).setCordinate(x, y);
}
repaint();
}
}