-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha11.java
More file actions
229 lines (191 loc) · 4.58 KB
/
a11.java
File metadata and controls
229 lines (191 loc) · 4.58 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.applet.Applet;
import java.awt.*;
import javax.swing.*;
import java.util.concurrent.TimeUnit;
import java.lang.Math.*;
/* <applet code="a11" width="600" height="600"> </applet> */
public class a11 extends Applet
{
int appletHeight = 0, appletWidth = 0, box_width = 0, box_height = 0, spacing = 0, gap = 0, top_padding = 0, left_padding=0, connectedLineHeight=40, step=1, t_sleep=20;
int clicked = 0;
Box[] bb;
Button b;
//Graphics g;
public void init()
{
setLayout(null);
setBackground(Color.WHITE);
Dimension appletSize = this.getSize();
appletHeight = appletSize.height;
appletWidth = appletSize.width;
box_width = appletWidth/12 + 14;
box_height = appletHeight/12;
spacing = 5;
top_padding =(int)(box_height*0.4 + 10);
left_padding = (int)(box_width*0.4 + 5);
//b = new Button("Sort");
bb = new Box[8];
bb[0] = new Box(3);
bb[1] = new Box(5);
bb[2] = new Box(8);
bb[3] = new Box(9);
bb[4] = new Box(7);
bb[5] = new Box(4);
bb[6] = new Box(2);
bb[7] = new Box(1);
gap = spacing + box_width;
//b.setBounds(10,10,50,50);
//b.addActionListener(this);
for(int i=0; i<bb.length; i++)
{
bb[i].updateBounds(gap*i+20, (appletHeight/2)-(box_height/2));
//g.drawRect(gap*i+20, (appletHeight/2)-(box_height/2), box_width, box_height);
//bb[i].setEnabled(false);
//add(bb[i]);
}
}
public void paint(Graphics g)
{
display(g);
for(int i=0;i<4;i++)
{
swappingLines(bb[i],bb[i+4],g);
swap(bb[i], bb[i+4], g);
}
//swappingLines(bb[2],bb[5],g);
}
public void swappingLines(Box b1, Box b2, Graphics g)
{
int x1 = b1.xx;
int x2 = b2.xx;
int y1 = b1.yy;
int y2 = b2.yy;
int mid_b1 = x1 + box_width/2;
int mid_b2 = x2 + box_width/2;
g.setColor(Color.BLACK);
g.drawLine(mid_b1, y1, mid_b1, y1 - connectedLineHeight);
g.drawLine(mid_b1, y1 - connectedLineHeight, mid_b2, y1 - connectedLineHeight);
g.drawLine(mid_b2, y1 - connectedLineHeight, mid_b2, y2);
try
{
TimeUnit.SECONDS.sleep(2);
}
catch(Exception e)
{
}
g.setColor(Color.WHITE);
g.drawLine(mid_b1, y1, mid_b1, y1 - connectedLineHeight);
g.drawLine(mid_b1, y1 - connectedLineHeight, mid_b2, y1 - connectedLineHeight);
g.drawLine(mid_b2, y1 - connectedLineHeight, mid_b2, y2);
//repaint();
}
public void swap(Box b1, Box b2, Graphics g)
{
moveBox(b1, b1.xx, b1.yy-connectedLineHeight-(box_height/2), g);
moveBox(b1, b2.xx, b1.yy, g);
}
public void moveBox(Box box, int dest_x, int dest_y, Graphics g)
{
int bx = box.xx;
int by = box.yy;
if(bx == dest_x)
{
if(by > dest_y)
{
while((by-dest_y)>0)
{
g.setColor(Color.WHITE);
g.drawRect(bx, by, box_width, box_height);
by-=step;
g.setColor(Color.BLACK);
g.drawRect(bx, by, box_width, box_height);
try
{Thread.sleep(t_sleep);}
catch(Exception e)
{}
}
}
else if(by < dest_y)
{
while((dest_y-by)>0)
{
g.setColor(Color.WHITE);
g.drawRect(bx, by, box_width, box_height);
by-=step;
g.setColor(Color.BLACK);
g.drawRect(bx, by, box_width, box_height);
try
{Thread.sleep(t_sleep);}
catch(Exception e)
{}
}
}
}
else if(by == dest_y)
{
if(bx > dest_x)
{
while((bx-dest_x)>0)
{
g.setColor(Color.WHITE);
g.drawRect(bx, by, box_width, box_height);
bx-=step;
g.setColor(Color.BLACK);
g.drawRect(bx, by, box_width, box_height);
try
{Thread.sleep(t_sleep);}
catch(Exception e)
{}
}
}
else if(bx < dest_x)
{
while((dest_x-bx)>0)
{
g.setColor(Color.WHITE);
g.drawRect(bx, by, box_width, box_height);
bx+=step;
g.setColor(Color.BLACK);
g.drawRect(bx, by, box_width, box_height);
try
{Thread.sleep(t_sleep);}
catch(Exception e)
{}
}
}
}
box.xx = bx;
box.yy = by;
}
public void display(Graphics g)
{
for(int i=0; i<bb.length; i++)
{
//String s = bb[i].value.toString();
//bb[i].updateBounds(gap*i+20, (appletHeight/2)-(box_height/2));
g.drawRect(bb[i].xx, bb[i].yy, box_width, box_height);
g.drawString(Integer.toString(bb[i].value), bb[i].xx + left_padding, bb[i].yy + top_padding);
//bb[i].setEnabled(false);
//add(bb[i]);
}
}
}
class Box
{
public int xx=0;
public int yy=0;
//public int width = 50;
//public int height = 50;
public int value=0;
Box(int value)
{
this .value = value;
//this.setBounds(xx, yy, width, height);
//this.setLayout(null);
}
public void updateBounds(int xx, int yy)
{
this.xx = xx;
this.yy = yy;
}
}