forked from SumedhArani/Traffic-simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapGraphics.java
More file actions
227 lines (156 loc) · 7.39 KB
/
mapGraphics.java
File metadata and controls
227 lines (156 loc) · 7.39 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import java.awt.*;
import java.util.*;
import java.*;
import java.io.*;
class mapGraphics extends Canvas
{
static final int maxX = 1000;
static final int maxY = 700;
public mapGraphics()
{
setSize(200, 200);
setBackground(Color.white);
}
public static void main(String[] argS)
{
mapGraphics GP = new mapGraphics();
Frame aFrame = new Frame();
aFrame.setSize(maxX, maxY);
aFrame.add(GP);
aFrame.setVisible(true);
}
public void paint(Graphics g)
{
/////////////////////////////////////////////////////////////////////////////
//Infinite read loop.
//Drawings and equivalent strings
// 1. CHAR_*_x_y_COLOR // will print the specified char
// 2. RECT_x1_y1_x2_y2_COLOR // will draw a rectangle
// 3. CIRL_x_y_radius_COLOR // circle
// 4. LINE_x1_y1_x2_y2_COLOR // line
// Note:
// 1. Coordinate must be 3 digit number
// 2. COLOR codes : BLU , BLK , RED , YLW , GRN .
// 3. Assume coordinate system of mathematics.
int pos=1; //records the next line number to be read
String str=null;
g.setColor(Color.black);
while(true)
{
try{
str = readFile(pos);
}
catch(Exception e){System.out.println("File Error!!");}
if(str!=null)
{
System.out.print(str);
pos++;
//___________________________________ draw module
String fig ="" + str.charAt(0) + str.charAt(1) + str.charAt(2) + str.charAt(3);
if(fig.equals("CHAR") == true) /// for character
{
int x = 0;
int y = 0;
String color ="" + str.charAt(15) + str.charAt(16) + str.charAt(17);
if(color.equals("BLU")) g.setColor(Color.BLUE);
if(color.equals("BLK")) g.setColor(Color.BLACK);
if(color.equals("RED")) g.setColor(Color.RED);
if(color.equals("YLW")) g.setColor(Color.YELLOW);
if(color.equals("GRN")) g.setColor(Color.GREEN);
x = ((int)str.charAt(7) -48)*100 + ((int)str.charAt(8) -48)*10 + ((int)str.charAt(9) -48) ;
y = ((int)str.charAt(11) -48)*100 + ((int)str.charAt(12) -48)*10 + ((int)str.charAt(13) -48) ;
System.out.println("\n"+"Character -"+" "+str.charAt(5)+" "+x+" "+y+color+"\n");
g.drawString(""+str.charAt(5), x, maxY-y);
}
else if(fig.equals("LINE") == true) /// for rectangle
{
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
String color ="" + str.charAt(21) + str.charAt(22) + str.charAt(23);
if(color.equals("BLU")) g.setColor(Color.BLUE);
if(color.equals("BLK")) g.setColor(Color.BLACK);
if(color.equals("RED")) g.setColor(Color.RED);
if(color.equals("YLW")) g.setColor(Color.YELLOW);
if(color.equals("GRN")) g.setColor(Color.GREEN);
x1 = ((int)str.charAt(5) -48)*100 + ((int)str.charAt(6) -48)*10 + ((int)str.charAt(7) -48) ;
y1 = ((int)str.charAt(9) -48)*100 + ((int)str.charAt(10) -48)*10 + ((int)str.charAt(11) -48) ;
x2 = ((int)str.charAt(13) -48)*100 + ((int)str.charAt(14) -48)*10 + ((int)str.charAt(15) -48) ;
y2 = ((int)str.charAt(17) -48)*100 + ((int)str.charAt(18) -48)*10 + ((int)str.charAt(19) -48) ;
System.out.println("\n"+"Line - "+x1+" "+y1+" "+x2+" "+y2+" "+color+"\n");
g.drawLine(x1, maxY-y1, x2, maxY-y2);
}
else if(fig.equals("RECT") == true) /// for rectangle
{
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
String color ="" + str.charAt(21) + str.charAt(22) + str.charAt(23);
if(color.equals("BLU")) g.setColor(Color.BLUE);
if(color.equals("BLK")) g.setColor(Color.BLACK);
if(color.equals("RED")) g.setColor(Color.RED);
if(color.equals("YLW")) g.setColor(Color.YELLOW);
if(color.equals("GRN")) g.setColor(Color.GREEN);
x1 = ((int)str.charAt(5) -48)*100 + ((int)str.charAt(6) -48)*10 + ((int)str.charAt(7) -48) ;
y1 = ((int)str.charAt(9) -48)*100 + ((int)str.charAt(10) -48)*10 + ((int)str.charAt(11) -48) ;
x2 = ((int)str.charAt(13) -48)*100 + ((int)str.charAt(14) -48)*10 + ((int)str.charAt(15) -48) ;
y2 = ((int)str.charAt(17) -48)*100 + ((int)str.charAt(18) -48)*10 + ((int)str.charAt(19) -48) ;
System.out.println("\n"+"Rectangle - "+x1+" "+y1+" "+x2+" "+y2+" "+color+"\n");
g.drawRect(x1, maxY-y1, x2, maxY-y2);
g.fillRect(x1, maxY-y1, x2, maxY-y2);
}
else if(fig.equals("CIRL") == true) /// for rectangle
{
int x = 0;
int y = 0;
int r = 0;
String color ="" + str.charAt(17) + str.charAt(18) + str.charAt(19);
if(color.equals("BLU")) g.setColor(Color.BLUE);
if(color.equals("BLK")) g.setColor(Color.BLACK);
if(color.equals("RED")) g.setColor(Color.RED);
if(color.equals("YLW")) g.setColor(Color.YELLOW);
if(color.equals("GRN")) g.setColor(Color.GREEN);
x = ((int)str.charAt(5) -48)*100 + ((int)str.charAt(6) -48)*10 + ((int)str.charAt(7) -48) ;
y = ((int)str.charAt(9) -48)*100 + ((int)str.charAt(10) -48)*10 + ((int)str.charAt(11) -48) ;
r = ((int)str.charAt(13) -48)*100 + ((int)str.charAt(14) -48)*10 + ((int)str.charAt(15) -48) ;
/*x = x - (int)(1.414*r);
y = y - (int)(1.414*r);*/
System.out.println("\n"+"Circle - "+x+" "+y+" "+r+" "+color+"\n");
g.drawOval(x, maxY-y, r/2, r/2);
g.fillOval(x, maxY-y, r/2, r/2);
}
// _________________
}
}
}
public static String readFile(int index) throws IOException
{
BufferedReader fin = null;
int lc=0; // line counter
try
{
fin = new BufferedReader(new FileReader("test.txt"));
String ch = null;
while(true)
{
ch = fin.readLine();
if(ch == null)
break;
lc++;
if(lc == index)
return ch;
}
}
catch(IOException e)
{
System.out.println(e);
}
finally
{
if(fin != null) fin.close();
}
return null;
}
}