File: MixedNumbers.java

package info (click to toggle)
jfractionlab 0.91-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 5,188 kB
  • ctags: 1,928
  • sloc: java: 9,588; makefile: 75; sh: 68
file content (470 lines) | stat: -rw-r--r-- 15,273 bytes parent folder | download | duplicates (2)
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/**
 *	JFractionLab
 *	Copyright (C) 2005 jochen georges, gnugeo _ at _ gnugeo _ dot _ de
 *
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **/

package jfractionlab.exerciseDialogs;

import org.debian.tablelayout.TableLayout;

import java.awt.Color;
import java.awt.Container;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JLabel;

import jfractionlab.FractionMaker;
import jfractionlab.HelpStarter;
import jfractionlab.JFractionLab;
import jfractionlab.MyJTextField;
import jfractionlab.displays.FractionAsCircle;
import jfractionlab.displays.FractionLine;
import jfractionlab.exerciseGenerator.ExerciseGenerator;
import jfractionlab.jflDialogs.UsabilityDialog;
import jfractionlab.jflDialogs.WorkSheetDialog;





public class MixedNumbers extends ExerciseDialog implements ActionListener, KeyListener{
	static final long serialVersionUID = JFractionLab.serialVersionUID;
		//GUI
		
		private MyJTextField tf_mixedNBNumber = new MyJTextField(2);
		private MyJTextField tf_mixedNBNumerator = new MyJTextField(2);
		private MyJTextField tf_mixedNBDenominator = new MyJTextField(2);
		
		private MyJTextField tf_improperFractionNumerator = new MyJTextField(2);
		private MyJTextField tf_improperFractionDenominator = new MyJTextField(2);

		private FractionAsCircle[] pizzaPlace = {
				new FractionAsCircle(),
				new FractionAsCircle(),
				new FractionAsCircle(),
				new FractionAsCircle(),
				new FractionAsCircle(),
				new FractionAsCircle(),
				new FractionAsCircle(),
				new FractionAsCircle(),
				new FractionAsCircle()
		};

		private JButton btn_tipp;

		private boolean isVisible = true;
		private boolean was_neverVisible = true;
		//Zahlen
		private FractionMaker fm;
		private int mixedNBNumber;
		private int mixedNBNumerator;
		private int improperFractionNumerator;
		private int improperFractionDenominator;
		/**
		 * 
		 * @param owner
		 * @param lx
		 * @param ly
		 * @param sx
		 * @param sy
		 * @throws HeadlessException
		 */
		public MixedNumbers(JFractionLab owner, int lx, int ly, int sx, int sy) throws HeadlessException {
			super(lx, ly, sx, sy);
			setTitle(lang.Messages.getString("mixed_numbers_2_improper_fracs"));
			this.owner = owner;
			
			//Menu
			rb_random.addActionListener(this);
			jmOptions.add(rb_random);
			rb_custom.addActionListener(this);
			jmOptions.add(rb_custom);
			jmOptions.addSeparator();
			
			rb_visible.addActionListener(this);
			jmOptions.add(rb_visible);
			rb_invisible.addActionListener(this);
			jmOptions.add(rb_invisible);
			jmb.add(jmOptions);
			jmiCreateWorkSheet.addActionListener(this);
			jmWorkSheet.add(jmiCreateWorkSheet);
			jmb.add(jmWorkSheet);
			jmiHelp.addActionListener(this);
			jmb.add(jmHelp);
			setJMenuBar(jmb);

			double width_of_pizzaplace = 0.45;
			double sizes[][] = {{
				// Spalten
				5,//0
				width_of_pizzaplace/4,//1
				width_of_pizzaplace/4,//2
				width_of_pizzaplace/4,//3
				width_of_pizzaplace/4,//4	
				12,//5
				width_of_pizzaplace/6,//6
				width_of_pizzaplace/6,//7
				width_of_pizzaplace/6,//8
				width_of_pizzaplace/6,//9g.setColor(getForeground());
				width_of_pizzaplace/6,//10
				width_of_pizzaplace/6,//11
				TableLayout.FILL,//12
				5//13
			},{
				//Zeilen
				5,//0jflTextResource.properties
				TableLayout.FILL,//1
				20,//2
				TableLayout.FILL,//3
				0.4,//4
				0.4,//5
				TableLayout.FILL,//6
				5//7
			}};
			Container content = getContentPane();
			content.setLayout(new TableLayout(sizes));
			content.setBackground(Color.white);
			//--links
				addTextField(tf_mixedNBNumber);
				tf_mixedNBNumber.addKeyListener(this);
				tf_mixedNBNumber.setEditable(false);
			content.add(tf_mixedNBNumber, "2,1,2,3,c,c");
				addTextField(tf_mixedNBNumerator);
				tf_mixedNBNumerator.addKeyListener(this);
				tf_mixedNBNumerator.setEditable(false);
			content.add(tf_mixedNBNumerator, "3,1,c,b");
				addTextField(tf_mixedNBDenominator);
				tf_mixedNBDenominator.addKeyListener(this);
				tf_mixedNBDenominator.setEditable(false);
			content.add(tf_mixedNBDenominator, "3,3,c,t");
			//--rechts
				addTextField(tf_improperFractionNumerator);
				tf_improperFractionNumerator.addKeyListener(this);
				tf_improperFractionNumerator.setEditable(false);
			content.add(tf_improperFractionNumerator, "8,1,9,1,c,b");
				addTextField(tf_improperFractionDenominator);
				tf_improperFractionDenominator.setEditable(false);
			content.add(tf_improperFractionDenominator, "8,3,9,3,c,t");

			//--Zeichen
			JLabel lb_gleich1 = new JLabel("=", JLabel.CENTER);
			content.add(lb_gleich1, "5,2");
			FractionLine bs_fraction1 = new FractionLine();
			content.add(bs_fraction1, "3,2");
			FractionLine bs_fraction2 = new FractionLine();
			content.add(bs_fraction2, "8,2,9,2");
			//--Pizzen
			content.add(pizzaPlace[0], "1,4,2,4");
			content.add(pizzaPlace[1], "1,5,2,5");
			content.add(pizzaPlace[2], "3,4,4,4");
			content.add(pizzaPlace[3], "3,5,5,5");
			content.add(pizzaPlace[4], "6,4,8,4");
			content.add(pizzaPlace[5], "6,5,8,5");
			content.add(pizzaPlace[6], "9,4,11,4");
			content.add(pizzaPlace[7], "9,5,11,5");
			//--Controls
				btn_continue = new JButton(lang.Messages.getString("continue"));
				btn_continue.addActionListener(this);
				btn_continue.addKeyListener(this);
				btn_continue.setFocusTraversalKeysEnabled(false);
			content.add(btn_continue, "12,1,f,c");
				btn_tipp = new JButton(lang.Messages.getString("Tipp"));
				btn_tipp.addActionListener(this);
				btn_tipp.setEnabled(false);
			content.add(btn_tipp, "12,4,f,c");
				btn_end =  new JButton(lang.Messages.getString("end"));
				btn_end.addActionListener(this);
			content.add(btn_end, "12,3,f,c");
			content.add(pdsp, "12,5");
				lb_info.setFont(JFractionLab.infofont);
				lb_info.setText(lang.Messages.getString("you_just_need_nb_and_enter"));
			content.add(lb_info, "1,6,11,6");

			points = owner.points_gemischteZahlen;
			pdsp.setText(String.valueOf(points));
			makeProblem();
			String[] ar_howto = {
				"howto_nb_and_enter","howto_option_type_of_exercise","howto_option_invisible"
			};
			new UsabilityDialog(ar_howto);//new UsabilityDialog
		}//Konstruktor
		
		/**
		 * 
		 *
		 */	
		protected void makeProblem(){
			//System.out.println("=== makeProblem ===");
			fm = new FractionMaker(); //der Parameter setzt die exclusive Obergrenze der Zahlen des Bruches
			fm.mkImproperFraction();
			improperFractionNumerator = fm.getNumerator_1();
			improperFractionDenominator = fm.getDenominator_1();
				//TESTZAHLEN
				//improperFractionNumerator = 16; improperFractionDenominator = 8;
			mixedNBNumber = improperFractionNumerator/improperFractionDenominator;
			mixedNBNumerator = improperFractionNumerator - improperFractionDenominator * mixedNBNumber;
			
			tf_improperFractionDenominator.setText(String.valueOf(improperFractionDenominator));
			was_neverVisible = true;
			if(isVisible){
				drawPizzaBlech();
				tf_mixedNBNumber.setEditable(true);
				tf_mixedNBNumber.requestFocusInWindow();
				lb_info.setText(lang.Messages.getString("which_mixed_number_do_you_see"));
			}else{
				tf_mixedNBNumber.setText(String.valueOf(mixedNBNumber));
				if(mixedNBNumerator != 0){tf_mixedNBNumerator.setText(String.valueOf(mixedNBNumerator));}
				if(mixedNBNumerator != 0){tf_mixedNBDenominator.setText(String.valueOf(improperFractionDenominator));}
				tf_improperFractionNumerator.setEditable(true);
				tf_improperFractionNumerator.requestFocusInWindow();
			}
		}//makeProblem()
		
		/**
		 * 
		 *
		 */
		private void drawPizzaBlech(){
			int nb = 0;
			for(int i = 0; i < mixedNBNumber; i++){
				pizzaPlace[i].drawPizzaAsCircle(improperFractionDenominator, improperFractionDenominator, Color.yellow);
				nb = i;
			}//for
			if(mixedNBNumerator != 0){
				pizzaPlace[nb+1].drawPizzaAsCircle(
						mixedNBNumerator,
						improperFractionDenominator,
						Color.yellow);
			}else{
				pizzaPlace[nb+1].noPizzaAsCircle();
			}//if	
			was_neverVisible = false;
		}//drawPizzaBlech
		
		/**
		 * 
		 *
		 */
		private void clearPizzaBlech(){
			for(int i = 0; i < 8; i++){
				pizzaPlace[i].noPizzaAsCircle();
			}//for
		}//clearPizzaBlech
		
		/**
		 * 
		 *
		 */	
		protected void nextProblem(){
			clearTextFields();
			clearPizzaBlech();
			if (bl_randomProblem == true){
				makeProblem();
			}else{
				tf_mixedNBNumber.setEditable(true);
				tf_mixedNBNumber.requestFocusInWindow();
			}//if
		}

		/**
		 * 
		 *
		 *
		private void close_it(){
			setVisible(false);
			dispose();
		}//close_it()
	*/
		/**
		 * 
		 */
		public void actionPerformed (ActionEvent e) {
			Object obj = e.getSource();
			if (obj == btn_continue){
				if (isVisible){
					btn_tipp.setEnabled(false);
				}else{
					btn_tipp.setEnabled(true);
				}
				nextProblem();
			}else if (obj == btn_end){
				close_it();
			}else if (obj == btn_tipp){
				//System.out.println("btn_tipp");
				drawPizzaBlech();
				btn_tipp.setEnabled(false);
				tf_improperFractionNumerator.requestFocusInWindow();
			}else if (obj == rb_visible){
				//System.out.println("rb_visble");
				//the program is in "Invisible-mode" actually
				//but it should switch to "Visible-mode"
				if(!bl_randomProblem){drawPizzaBlech();}
				btn_tipp.setEnabled(false);
				isVisible = true;
			}else if (obj == rb_invisible){
				//System.out.println("rb_invisble");
				//the program is in "Visible-mode" actually
				//it should switch to "Invisible-mode"
				clearPizzaBlech();
				btn_tipp.setEnabled(true);
				isVisible = false;
				makeProblem();
			}else if (obj == rb_random){
				//System.out.println("rb_random");
				//the program is in "custom-mode" actually
				//it should switch to "random-mode"
				tf_mixedNBNumber.setEditable(false);
				bl_randomProblem = true;
				nextProblem();
			}else if (obj == rb_custom){
				//System.out.println("rb_custom");
				//the program is in "random-mode" actually
				//it should switch to "custom-mode"
				bl_randomProblem = false;
				tf_improperFractionDenominator.setText("");
				nextProblem();
			}else if(obj == jmiCreateWorkSheet){
				new WorkSheetDialog(
						ExerciseGenerator.MIXED_NUMBERS,
						lang.Messages.getString("mixed_numbers"),
						lang.Messages.getString("full_numbers"),
						lang.Messages.getString("fractions")
				);
			}else if(obj == jmiHelp){
				new HelpStarter(
						lang.Messages.getLocale().toString(),
						"mixed-numbers"
				);
			}
		}//actionPerformed
		
		/**
		 * 
		 */
		public void keyPressed(KeyEvent event){
			Object obj = event.getSource();
			int key = event.getKeyCode();
			lb_info.setText("");
			if(obj == btn_continue & key == KeyEvent.VK_ENTER){
				nextProblem();
			}else if(obj == tf_mixedNBNumber & key == KeyEvent.VK_ENTER){
				if(readInputInt(tf_mixedNBNumber) != null){
					int num = readInputInt(tf_mixedNBNumber);
					if(bl_randomProblem){
						if(num == mixedNBNumber){
							tf_mixedNBNumber.setEditable(false);
							if(mixedNBNumerator != 0){
								tf_mixedNBNumerator.setEditable(true);
								tf_mixedNBNumerator.requestFocusInWindow();
							}else{
								tf_improperFractionNumerator.setEditable(true);
								tf_improperFractionNumerator.requestFocusInWindow();
							}
						}else{
							tf_mixedNBNumber.setText("");
							tf_mixedNBNumber.requestFocusInWindow();
						}
					}else{
						int nb = Integer.parseInt(tf_mixedNBNumber.getText());
						if(nb >= 8){
							lb_info.setText(lang.Messages.getString("nb_smaller_than_eight"));
							tf_mixedNBNumber.setText("");
						}else{
							mixedNBNumber = nb;
							tf_mixedNBNumber.setEditable(false);
							tf_mixedNBNumerator.setEditable(true);
							tf_mixedNBNumerator.requestFocusInWindow();
						}
					}
				}
			}else if(obj == tf_mixedNBNumerator & key == KeyEvent.VK_ENTER){
				if(readInputInt(tf_mixedNBNumerator) != null){
					int num = readInputInt(tf_mixedNBNumerator);
					if(bl_randomProblem){
						if(num == mixedNBNumerator){
							tf_mixedNBNumerator.setEditable(false);
							tf_mixedNBDenominator.setEditable(true);
							tf_mixedNBDenominator.requestFocusInWindow();
						}else{
							tf_mixedNBNumerator.setText("");
							tf_mixedNBNumerator.requestFocusInWindow();
						}
					}else{
						mixedNBNumerator = readInputInt(tf_mixedNBNumerator);
						tf_mixedNBNumerator.setEditable(false);
						tf_mixedNBDenominator.setEditable(true);
						tf_mixedNBDenominator.requestFocusInWindow();
					}
				}
			}else if(obj == tf_mixedNBDenominator & key == KeyEvent.VK_ENTER){
				if(readInputInt(tf_mixedNBDenominator) != null){
					int pn = Integer.parseInt(tf_mixedNBDenominator.getText());
					if(bl_randomProblem){
						if(pn == improperFractionDenominator){
							tf_mixedNBDenominator.setEditable(false);
							tf_improperFractionNumerator.setEditable(true);
							tf_improperFractionNumerator.requestFocusInWindow();
						}else{
							tf_mixedNBDenominator.setText("");
							tf_mixedNBDenominator.requestFocusInWindow();
						}
					}else{	
						if(pn <= mixedNBNumerator){
							lb_info.setText(lang.Messages.getString("dn_must_be_bigger"));
							tf_mixedNBDenominator.setText("");
						}else{
							improperFractionDenominator = pn;
							improperFractionNumerator = mixedNBNumber * improperFractionDenominator + mixedNBNumerator;
							if(isVisible){drawPizzaBlech();}
							tf_improperFractionDenominator.setText(String.valueOf(improperFractionDenominator));
							tf_mixedNBDenominator.setEditable(false);
							tf_improperFractionNumerator.setEditable(true);
							tf_improperFractionNumerator.requestFocusInWindow();
						}
					}
				}
			}else if(obj == tf_improperFractionNumerator & key == KeyEvent.VK_ENTER){
				if(readInputInt(tf_improperFractionNumerator) != null){
					int num2 = Integer.parseInt(tf_improperFractionNumerator.getText());
					if( num2 != improperFractionNumerator ){
						lb_info.setText(lang.Messages.getString("how_many_pieces"));
						tf_improperFractionNumerator.setText("");
					}else{
						lb_info.setText(lang.Messages.getString("that_is_right"));
						if(bl_randomProblem){
							points++;
						}
						if(bl_randomProblem && !isVisible && was_neverVisible){
							//extrapunkt
							points++;
						}
						pdsp.setText(String.valueOf(points));
						owner.setPoints(points, "gemischteZahlen");
						tf_improperFractionNumerator.setEditable(false);
						drawPizzaBlech();
						btn_continue.requestFocusInWindow();
					}
				}
			}
		}//keyPressed
		public void keyTyped(KeyEvent event){}
		public void keyReleased(KeyEvent event){}
}//class