-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorPanel2.java
More file actions
102 lines (77 loc) · 2.83 KB
/
Copy pathColorPanel2.java
File metadata and controls
102 lines (77 loc) · 2.83 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
//ColorPanel example
import javax.swing.*;
import java.awt.*;
public class ColorPanel2 extends JPanel{
private int lifeL = 0;
private String drawString = "";
public ColorPanel2(Color whatever){
setBackground(whatever);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.black);
if(lifeL >= 1)
g.drawRect(10,10,10,10);
if(lifeL >= 2)
g.fillRect(20,20,10,10);
if(! drawString.equals("")){
//There is at least one draw command.
String drawCMD;
int indexBegin = 0;
int indexEnd = drawString.indexOf(">");
int indexBC; //index of the beginning comma;
int indexEC; //index of the ending comma;
int x, y, w, h; //draw specs
String c; //color codes
while(indexBegin != -1){
//get the drawCMD
drawCMD = drawString.substring(indexBegin + 1, indexBegin + 2 + 1); // -1 for the second parameter
indexBC = indexBegin + 3;
indexEC = drawString.indexOf(",", indexBC + 1); // + 1 because you ignore the current ,
x = Integer.parseInt(drawString.substring(indexBC + 1, indexEC - 1 + 1));
indexBC = indexEC;
indexEC = drawString.indexOf(",", indexBC + 1);
y = Integer.parseInt(drawString.substring(indexBC + 1, indexEC - 1 + 1));
indexBC = indexEC;
indexEC = drawString.indexOf(",", indexBC + 1);
w = Integer.parseInt(drawString.substring(indexBC + 1, indexEC - 1 + 1));
indexBC = indexEC;
//indexEC = indexEnd;
indexEC = drawString.indexOf(",", indexBC + 1);
h = Integer.parseInt(drawString.substring(indexBC + 1, indexEC - 1 + 1));
indexBC = indexEC;
indexEC = indexEnd;
c = drawString.substring(indexBC + 1, indexEC - 1 + 1);
if(c.equals("bl")) g.setColor(Color.black);
if(c.equals("w")) g.setColor(Color.white);
if(c.equals("r")) g.setColor(Color.red);
if(c.equals("y")) g.setColor(Color.yellow);
if(c.equals("b")) g.setColor(Color.blue);
if(drawCMD.equals("DR")) //drawRect
g.drawRect(x, y, w, h);
if(drawCMD.equals("FR")) //fillRect
g.fillRect(x, y, w, h);
if(drawCMD.equals("DO")) //drawOval
g.drawOval(x, y, w, h);
if(drawCMD.equals("FO")) //fillOval
g.fillOval(x, y, w, h);
//Starting at the current indexEnd position, check to see if there are any other <
indexBegin = drawString.indexOf("<", indexEnd);
if(indexBegin != -1){
//find the next indexEnd
indexEnd = drawString.indexOf(">", indexBegin);
}
}
}
}
public void getData(int life){
lifeL = life;
repaint();
}
public void addDraw(String cmd, int x, int y, int w, int h, String clr){
String drawS = "<" + cmd + "," + x + "," + y + "," + w + "," + h + "," + clr + ">";
System.out.println(drawS);
drawString = drawString + drawS;
repaint();
}
}