-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentExampleActionListener.java
More file actions
329 lines (310 loc) · 8.93 KB
/
StudentExampleActionListener.java
File metadata and controls
329 lines (310 loc) · 8.93 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JTextField;
public class StudentExampleActionListener extends JApplet implements ActionListener
{
private static final long serialVersionUID = 1L;
private static int[][] Array = new int [4][5000];
private static int temp;
private static int count;
private static int counter =0;
private static int countTotal = 0;
int forColor = 1;
int m, n;
boolean paint = false;
Font titleFont;
Font regularFont;
//*** two buttons are declared
JButton DrawLinesButton;
JButton clearLinesButton;
//*** two input values declared
JTextField inputLineNumber1;
JTextField inputLineNumber2;
Container contentPane;
//*** all JApplet have a method paint to draw the graphics
//*** Graphics object is brought in
//*** paint() is automatically called
public void paint (Graphics page)
{
if(!paint)
graphInitialize(page);
super.paint(page);
setBackground(Color.WHITE);
graph(page);
}
//*** The Applet class defines a method that is essential for programming applets,
//*** the init() method. This method is called just after the applet object has
//*** been created and before it appears on the screen. Its purpose is to give
//*** the applet a chance to do any necessary initialization. This method is
//*** called by the system, not by your program. Your job as a programmer
//*** is just to provide a definition of the init() method.
public void init()
{
System.out.println("init has been called");
contentPane = getContentPane();
contentPane.setLayout(null);
//*** 32 and 18 are pixel size
titleFont = new Font("Arial", Font.BOLD, 32);
regularFont = new Font("Arial", Font.PLAIN, 18);
//*** memory is allocated for Button
DrawLinesButton = new JButton("Draw Lines");
//*** this button is active and will be read
//*** by method actionPerformed(ActionEvent e)
DrawLinesButton.addActionListener(this);
//*** Location of button is created
DrawLinesButton.setBounds(350,550,150,40);
//*** memory is allocated for Button
clearLinesButton = new JButton("Clear Lines");
//*** this button is active and will be read
//*** by method actionPerformed(ActionEvent e)
clearLinesButton.addActionListener(this);
//*** Location of button is created
clearLinesButton.setBounds(350,600,150,40);
//*** setBounds() creates the input box
//*** memory is allocated for TextField
inputLineNumber1 = new JTextField(15);
//*** Location of TextField is created
inputLineNumber1.setBounds(210,560,50,30);
//*** memory is allocated for TextField
inputLineNumber2 = new JTextField(15);
//*** Location of TextField is created
inputLineNumber2.setBounds(210,590,50,30);
//*** add them to contentPane
contentPane.add(DrawLinesButton);
contentPane.add(clearLinesButton);
contentPane.add(inputLineNumber1);
contentPane.add(inputLineNumber2);
}
public void graphInitialize (Graphics page)
{
Graphics g = getGraphics();
Color c = getBackground();
g.setColor(c);
for(int x=0;x<1250;x++)
g.drawLine (x,900, x,0);
System.out.println("graphInitialize has been called");
page.setColor(Color.BLACK);
page.setFont(titleFont);
page.setFont(regularFont);
//*** Strings line up with setBounds for each JTextField
// page.drawString("Enter line number 1: ",40, 580);
// page.drawString("Enter line number 2: ",40, 610);
//*** initialize input to 0
inputLineNumber1.setText("0");
inputLineNumber2.setText("0");
}
public void graph(Graphics page)
{
System.out.println("graph has been called");
if(!inputLineNumber1.getText().equals("")&&!inputLineNumber2.getText().equals(""))
{
//*** text must be changed to int using the wrapper class Integer
int m = Integer.parseInt(inputLineNumber1.getText());
int n = Integer.parseInt(inputLineNumber2.getText());
//*** method called here
int result = Ackermann(m, n, page);
//*** method called here
//*** 3 strings are placed in the window
page.setColor(Color.BLACK);
page.setFont(titleFont);
page.drawString("Ackermann", 540, 50);
page.setFont(regularFont);
page.drawString("Count: " + getCount(), 600, 580);
page.drawString("Value: " + result, 600, 610);
page.drawString("Enter Value of M: ",40, 580);
page.drawString("Enter Value of N: ",40, 610);
}
}
private int Ackermann(int m, int n, Graphics page)
{ //*********************************************************
//*** this method could be the recursive Ackermann function
//*********************************************************
count = 200;
// countTotal = 0;
// setNewColor(page,(int) (Math.random() * 12)+1);
// for(int i=0;i<m;i++)
// {
// page.drawLine (count,500, count,400);
// count++;
// countTotal++;
// }
// setNewColor(page,(int) (Math.random() * 12)+1);
// for(int i=0;i<n;i++)
// {
// page.drawLine (count,500, count,400);
// count++;
// countTotal++;
// }
// return countTotal;
int tableValue = tableLookUp(m, n);
if (tableValue == 0)
{
if (m == 0)
{
setNewColor(page,(int) (Math.random() * 12)+1);
for(int i=0;i<m;i++)
{
page.drawLine (count,500, count,100);
count++;
}
setNewColor(page,(int) (Math.random() * 12)+1);
for(int i=0;i<n;i++)
{
page.drawLine (count,500, count,100);
count++;
}
counter ++;
//System.out.println("Count = " + count + " M = " + m + " N = " + n);
setValue (m, n, n+1);
return (n+1);
}
else
{
if (n == 0)
{
setNewColor(page,(int) (Math.random() * 12)+1);
for(int i=0;i<m;i++)
{
page.drawLine (count,500, count,400);
count++;
}
setNewColor(page,(int) (Math.random() * 12)+1);
for(int i=0;i<n;i++)
{
page.drawLine (count,500, count,400);
count++;
}
counter ++;
//System.out.println("Count = " + count + " M = " + m + " N = " + n);
setValue (m, n, Ackermann(m-1, 1, page));
return Ackermann (m-1, 1, page);
}
else
{
setNewColor(page,(int) (Math.random() * 12)+1);
for(int i=0;i<m;i++)
{
page.drawLine (count,500, count,400);
count++;
}
setNewColor(page,(int) (Math.random() * 12)+1);
for(int i=0;i<n;i++)
{
page.drawLine (count,500, count,400);
count++;
}
counter ++;
//System.out.println("Count = " + count + " M = " + m + " N = " + n);
setValue (m, n, Ackermann(m-1, Ackermann(m,n-1, page), page));
return Ackermann(m - 1, Ackermann(m,n - 1, page), page);
}
}
}
else
return tableValue;
}
public int tableLookUp(int m, int n)
{
return Array [m][n];
}
public void setTable ()
{
for (int i = 0; i < Array.length-1; i++)
{
for (int j = 0; j < Array.length-1; j++)
{
Array [j][i] = 0;
}
}
}
public void setValue(int m, int n, int value)
{
Array[m][n] = value;
}
public int getCount ()
{
return counter-1;
}
private void setNewColor(Graphics page, int forColor)
{
switch (forColor)
{
case 1:
page.setColor(Color.BLACK);
break;
case 2:
page.setColor(Color.BLUE);
break;
case 3:
page.setColor(Color.CYAN);
break;
case 4:
page.setColor(Color.DARK_GRAY);
break;
case 5:
page.setColor(Color.GRAY);
break;
case 6:
page.setColor(Color.GREEN);
break;
case 7:
page.setColor(Color.LIGHT_GRAY);
break;
case 8:
page.setColor(Color.DARK_GRAY);
break;
case 9:
page.setColor(Color.MAGENTA);
break;
case 10:
page.setColor(Color.ORANGE);
break;
case 11:
page.setColor(Color.PINK);
break;
case 12:
page.setColor(Color.YELLOW);
break;
}
}
public void actionPerformed(ActionEvent e)
{
//*** buttons are active
String actionCommand = e.getActionCommand();
//*** one of two possible values of actionCommand
if (actionCommand.equalsIgnoreCase("Draw Lines"))
{
System.out.println("Draw Lines has been called");
paint = true;
countTotal = 0;
count = 0;
repaint();//*** method paint() is called again
}
else if (actionCommand.equalsIgnoreCase("Clear Lines"))
{
System.out.println("Clear Lines has been called");
this.clear();
inputLineNumber1.setText("");
inputLineNumber2.setText("");
}
}
public void clear()
{
//*** Graphics object is gotten
Graphics g = getGraphics();
Color c = getBackground();
g.setColor(c);
//*** lines of the background color cover up the image
for(int i=100;i<1250;i++)
g.drawLine (i,500, i,150);
for(int i=600;i<1000;i++)
g.drawLine (i,700, i,75);
}
}