-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGUI.java
More file actions
826 lines (716 loc) · 22.2 KB
/
Copy pathGUI.java
File metadata and controls
826 lines (716 loc) · 22.2 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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
import static javax.swing.GroupLayout.Alignment.*;
import java.util.concurrent.locks.*;
import java.awt.Dimension;
import java.util.Random;
public class GUI extends GUI_abstract {
GUI_worker g = new GUI_worker();
String m;
int round;
int currentPlayer;
int[][] points;
int[] free;
int spin_counter;
int category;
String[] categories;
int[] questions;
int timer;
boolean timer_returned = false;
public int get_n(String message){
m = message;
int int_return=0;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { g.get_n(m); }
});
g.lock.lock(); try{
g.returned = false;
while(!g.returned){g.interface_wait.await();}
int_return = g.int_share;
} catch(InterruptedException e){
} finally{g.lock.unlock();}
return int_return;
}
public String get_s(String message){
m = message;
String s_return="";
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { g.get_s(m); }
});
g.lock.lock(); try{
g.returned = false;
while(!g.returned){g.interface_wait.await();}
s_return = g.string_share;
} catch(InterruptedException e){
} finally{g.lock.unlock();}
return s_return;
}
public int yes_no_prompt(String message){
m = message;
int int_return=0;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { g.yes_no_prompt(m); }
});
g.lock.lock(); try{
g.returned = false;
while(!g.returned){g.interface_wait.await();}
int_return = g.int_share;
} catch(InterruptedException e){
} finally{g.lock.unlock();}
return int_return;
}
public int check_answer(String message){
m = message;
int int_return=0;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { g.check_answer(m); }
});
g.lock.lock(); try{
g.returned = false;
while(!g.returned){g.interface_wait.await();}
int_return = g.int_share;
} catch(InterruptedException e){
} finally{g.lock.unlock();}
return int_return;
}
public int choose_category(String message, String [] c){
categories = c;
m = message;
int int_return=0;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { g.choose_category(m,categories); }
});
g.lock.lock(); try{
g.returned = false;
while(!g.returned){g.interface_wait.await();}
int_return = g.int_share;
} catch(InterruptedException e){
} finally{g.lock.unlock();}
return int_return;
}
public String get_settings_file(){
String string_return="";
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { g.get_settings_file(); }
});
g.lock.lock(); try{
g.returned = false;
while(!g.returned){g.interface_wait.await();}
string_return = g.string_share;
} catch(InterruptedException e){
} finally{g.lock.unlock();}
return string_return;
}
public void message(String message){
// if(message==null){message="";}
m = message;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { g.message(m); }
});
g.lock.lock(); try{
g.returned = false;
while(!g.returned){g.interface_wait.await();}
} catch(InterruptedException e){
} finally{g.lock.unlock();}
}
String[] player_name;
public void show_info(String[] name, int r, int c, String message, int[][] p, int[] f, int s){
player_name=name; round = r; currentPlayer = c; m = message; points = p; free = f; spin_counter = s;
m = message;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { g.show_info(player_name, round,currentPlayer,m,points,free,spin_counter); }
});
g.lock.lock(); try{
g.returned = false;
while(!g.returned){g.interface_wait.await();}
} catch(InterruptedException e){
} finally{g.lock.unlock();}
}
private String[] choices; private int outcome;
public void spin(String[] c, int o){
choices=c; outcome=o;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { g.spin(choices,outcome); }
});
g.lock.lock(); try{
g.returned = false;
while(!g.returned){g.interface_wait.await();}
g.s_panel.stop();
} catch(InterruptedException e){
} finally{g.lock.unlock();}
return;
}
public void show_board(int r, int c, String[] ca, int[] q){
round = r; category = c; categories = ca; questions = q;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { g.show_board(round, category, categories, questions); }
});
g.lock.lock(); try{
g.returned = false;
while(!g.returned){g.interface_wait.await();}
} catch(InterruptedException e){
} finally{g.lock.unlock();}
g.s_panel.stop();
return;
}
public void ask_question(int t, String question){
m = question; timer = t;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { g.ask_question(timer,m); }
});
g.lock.lock(); try{
g.returned = false;
while(!g.returned){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { g.update_ask_question(timer,m); }
});
timer_returned = false;
Thread sec_timer = new Thread(new GUI_Timer(this));
while(!(timer_returned || g.returned)){
sec_timer.start();
g.interface_wait.await();
}
timer = timer - 1; if(timer < 0){timer = 0;}
}
} catch(InterruptedException e){
} finally{g.lock.unlock();}
}
}
class GUI_Timer implements Runnable {
GUI gui;
public GUI_Timer(GUI g){gui = g;}
public void run(){try {
Thread.sleep( 1000 );
gui.g.lock.lock();
gui.timer_returned = true;
gui.g.interface_wait.signal();
gui.g.lock.unlock();
} catch (InterruptedException e) { };}
}
class GUI_worker extends JFrame {
public ReentrantLock lock = new ReentrantLock();
public Condition interface_wait = lock.newCondition();
public boolean returned = false;
public int int_share;
public String string_share;
private JFrame frame;
private Container pane;
private int width=16*60;
private int height=9*60;
private JLabel message_label = new JLabel("<html><h1>Number of Players: </h1></html>");
private JLabel message_label2 = new JLabel("<html><h1> </h1></html>");
private JTextField get_n_text_field = new JTextField("50");
private JTextField get_s_text_field = new JTextField();
private JTextField textfield = new JTextField("");
private JButton continue_button = new JButton("Continue");
private JButton continue_s_button = new JButton("Continue");
private JButton y_button = new JButton("Yes");
private JButton n_button = new JButton("No");
private JButton timeout_button = new JButton("Timeout");
private JButton cat1_button = new JButton("Category 1");
private JButton cat2_button = new JButton("Category 2");
private JButton cat3_button = new JButton("Category 3");
private JButton cat4_button = new JButton("Category 4");
private JButton cat5_button = new JButton("Category 5");
private JButton cat6_button = new JButton("Category 6");
private JButton file_chooser = new JButton("Open file");
private JTable table;
private JScrollPane scrollPane;
public SpinPanel s_panel;
private JButton answer_button = new JButton("Answer!");
public GUI_worker(){
javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { init(); } });
}
private void init(){
frame = this;
pane = this.getContentPane();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Triviopoly");
//Display the window.
frame.setSize(width, height);
frame.setVisible(true);
continue_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lock.lock(); try{
int_share = Integer.parseInt(get_n_text_field.getText());
returned = true;
interface_wait.signal();
} catch(NumberFormatException n){
} finally{lock.unlock();}
}
});
continue_s_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lock.lock();
string_share = get_s_text_field.getText();
returned = true;
interface_wait.signal();
lock.unlock();
}
});
answer_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lock.lock(); try{
// int_share = Integer.parseInt(get_n_text_field.getText());
returned = true;
interface_wait.signal();
} catch(NumberFormatException n){
} finally{lock.unlock();}
}
});
y_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lock.lock();
int_share = 1;
returned = true;
interface_wait.signal();
lock.unlock();
}
});
n_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lock.lock();
int_share = 0;
returned = true;
interface_wait.signal();
lock.unlock();
}
});
timeout_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lock.lock();
int_share = 2;
returned = true;
interface_wait.signal();
lock.unlock();
}
});
cat1_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lock.lock();
int_share = 1;
returned = true;
interface_wait.signal();
lock.unlock();
}
});
cat2_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lock.lock();
int_share = 2;
returned = true;
interface_wait.signal();
lock.unlock();
}
});
cat3_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lock.lock();
int_share = 3;
returned = true;
interface_wait.signal();
lock.unlock();
}
});
cat4_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lock.lock();
int_share = 4;
returned = true;
interface_wait.signal();
lock.unlock();
}
});
cat5_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lock.lock();
int_share = 5;
returned = true;
interface_wait.signal();
lock.unlock();
}
});
cat6_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lock.lock();
int_share = 6;
returned = true;
interface_wait.signal();
lock.unlock();
}
});
file_chooser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
lock.lock();
File f = fileChooser.getSelectedFile();
string_share = f.getAbsolutePath();
returned = true;
interface_wait.signal();
lock.unlock();
}
}
});
return;
}
public void get_n(String message){
message = message.replace("\n", "</h1><p><h1>");
message_label.setText("<html><h1>"+message+"</h1></html>");
frame.setContentPane(pane);
pane.revalidate();
pane.removeAll();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(message_label, c);
c.gridx = 0;
c.gridy = 1;
pane.add(get_n_text_field, c);
c.gridx = 0;
c.gridy = 2;
pane.add(continue_button, c);
pane.repaint();
return;
}
public void get_s(String message){
message = message.replace("\n", "</h1><p><h1>");
message_label.setText("<html><h1>"+message+"</h1></html>");
frame.setContentPane(pane);
pane.revalidate();
pane.removeAll();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.8;
c.anchor = GridBagConstraints.CENTER;
c.gridx = 0;
c.gridy = 0;
pane.add(message_label, c);
c.gridx = 0;
c.gridy = 1;
pane.add(get_s_text_field, c);
c.gridx = 0;
c.gridy = 2;
pane.add(continue_s_button, c);
pane.repaint();
return;
}
public void yes_no_prompt(String message){
message = message.replace("\n", "</h1><p><h1>");
message_label.setText("<html><h1>"+message+"</h1></html>");
frame.setContentPane(pane);
pane.revalidate();
pane.removeAll();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
pane.add(message_label, c);
c.gridx = 0;
c.gridy = 1;
pane.add(y_button, c);
c.gridx = 2;
c.gridy = 1;
pane.add(n_button, c);
pane.repaint();
return;
}
public void check_answer(String message){
message = message.replace("\n", "</h1><p><h1>");
message_label.setText("<html><h1>Answer: "+message+"</h1></html>");
message_label2.setText("<html><h1>Did the Player answer correctly?</h1></html>");
frame.setContentPane(pane);
pane.revalidate();
pane.removeAll();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(message_label, c);
c.gridx = 0;
c.gridy = 1;
pane.add(message_label2, c);
c.gridx = 0;
c.gridy = 2;
pane.add(y_button, c);
c.gridx = 0;
c.gridy = 3;
pane.add(n_button, c);
c.gridx = 0;
c.gridy = 4;
pane.add(timeout_button, c);
pane.repaint();
return;
}
public void choose_category(String message, String[] categories){
message = message.replace("\n", "</h1><p><h1>");
message_label.setText("<html><h1>"+message+"</h1></html>");
cat1_button.setText(categories[0]);
cat2_button.setText(categories[1]);
cat3_button.setText(categories[2]);
cat4_button.setText(categories[3]);
cat5_button.setText(categories[4]);
cat6_button.setText(categories[5]);
frame.setContentPane(pane);
pane.revalidate();
pane.removeAll();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(message_label, c);
c.gridx = 0;
c.gridy = 1;
pane.add(cat1_button, c);
c.gridx = 0;
c.gridy = 2;
pane.add(cat2_button, c);
c.gridx = 0;
c.gridy = 3;
pane.add(cat3_button, c);
c.gridx = 0;
c.gridy = 4;
pane.add(cat4_button, c);
c.gridx = 0;
c.gridy = 5;
pane.add(cat5_button, c);
c.gridx = 0;
c.gridy = 6;
pane.add(cat6_button, c);
pane.repaint();
return;
}
public void get_settings_file(){
message_label.setText("<html><h1>Choose file</h1></html>");
frame.setContentPane(pane);
pane.revalidate();
pane.removeAll();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(message_label, c);
c.gridx = 0;
c.gridy = 1;
pane.add(file_chooser, c);
pane.repaint();
return;
}
public void message(String message){
message = message.replace("\n", "</h1><p><h1>");
message_label.setText("<html><h1>"+message+"</h1></html>");
frame.setContentPane(pane);
pane.revalidate();
pane.removeAll();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(message_label, c);
c.gridx = 0;
c.gridy = 1;
pane.add(continue_button, c);
pane.repaint();
return;
}
public boolean timer_returned = false;
public void update_ask_question(int timer, String message){
message_label.setText("<html><h1>"+message+"</h1></html>");
message_label2.setText("<html><h1> Time left: "+timer+" </h1></html>");
pane.repaint();
}
public void ask_question(int timer, String message){
Font font = new Font("TimesRoman", Font.BOLD, 20);
textfield.setText("");
textfield.setFont(font);
message = message.replace("\n", "</h1><p><h1>");
message_label.setText("<html><h1>"+message+"</h1></html>");
message_label2.setText("<html><h1> Time left: "+timer+" </h1></html>");
frame.setContentPane(pane);
pane.revalidate();
pane.removeAll();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(message_label, c);
c.gridx = 0;
c.gridy = 1;
pane.add(message_label2, c);
c.gridx = 0;
c.gridy = 2;
pane.add(textfield, c);
c.gridx = 0;
c.gridy = 3;
pane.add(answer_button, c);
pane.repaint();
return;
}
public void show_info(String[] player_name, int round, int currentPlayer, String message, int[][] points, int[] free, int spin_counter){
message = message.replace("\n", "</h1><p><h1>");
message_label.setText("<html><h1>"+message+"</h1></html>");
String m2 = "Round: "+(round+1)+" Spins left: "+spin_counter+" "+player_name[currentPlayer]+" "+(currentPlayer+1)+"'s turn";
message_label2.setText("<html>"+m2+"</html>");
String[] columnNames = {"Player ","#", "Round 1 Points", "Round 2 Points", "Free tokens"};
Object[][] data = new Object[(points[0].length)][columnNames.length];
for(int i=0; i<points[0].length; i++){
data[i][0] = player_name[i];
data[i][1] = i+1;
data[i][2] = points[0][i];
data[i][3] = points[1][i];
data[i][4] = free[i];
}
table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
frame.setContentPane(pane);
pane.revalidate();
pane.removeAll();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
// c.weightx = c.weighty = 1;
c.weightx = 1;
c.gridx = 0;
c.gridy = 0;
pane.add(message_label, c);
c.gridx = 0;
c.gridy = 1;
pane.add(message_label2, c);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 10;
pane.add(scrollPane, c);
c.gridx = 0;
c.gridy = 3;
pane.add(continue_button, c);
pane.repaint();
return;
}
public void show_board(int round, int category, String[] categories, int[] questions){
message_label.setText("<html><h1>Board:</h1><p>Your Category: "+categories[category]+"</html>");
String[] p = new String[6];
for(int i=0; i<p.length; i++){p[i]="_";}
p[category] = "<html><b>Category Picked</b></html>";
Object[][] data = new Object[6][p.length];
for(int i=0; i<p.length; i++){data[0][i] = categories[i];}
for(int i=0; i<5; i++){
for(int j=0; j<6; j++){
if(questions[j]<=i){data[i+1][j]=((i+1)*(round+1)*100)+"";}
else{data[i+1][j]="";}
}}
table = new JTable(data, p);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
frame.setContentPane(pane);
pane.revalidate();
pane.removeAll();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
// c.weightx = c.weighty = 1;
c.weightx = 1;
c.gridx = 0;
c.gridy = 0;
pane.add(message_label, c);
c.gridx = 0;
c.gridy = 1;
pane.add(scrollPane, c);
c.gridx = 0;
c.gridy = 2;
pane.add(continue_button, c);
pane.repaint();
return;
}
public void spin(String[] choices, int outcome){
pane.removeAll();
s_panel = new SpinPanel();
frame.setContentPane(s_panel);
s_panel.revalidate();
Color[] colors = new Color[choices.length];
Random gen = new Random(30);
for(int i=0; i<colors.length; i++){ colors[i] = new Color(gen.nextInt(255), gen.nextInt(255), gen.nextInt(255)); }
s_panel.init(choices, colors, outcome, frame.getWidth(), frame.getHeight());
Thread animation = new Thread(s_panel);
animation.start();
return;
}
class SpinPanel extends JPanel implements Runnable {
public ReentrantLock lock2 = new ReentrantLock();
private Image imageOffScreen;
private Graphics graphicsOffScreen;
Font font = new Font("TimesRoman", Font.BOLD, 40);
int height; int width;
String[] choices;
Color[] colors;
int current = 0;
int outcome;
int loc=0; int pos=0; int div=20;
boolean end = false;
public void init(String[] ch, Color[] co, int o, int w, int h){
height = h; width = w;
this.setLayout(new BorderLayout(5,5));
this.add(continue_button, BorderLayout.PAGE_END);
imageOffScreen = createImage(width, height);
graphicsOffScreen = imageOffScreen.getGraphics();
choices = ch; colors = co; outcome = o;
loc = outcome-1; if(loc < 0){loc = loc + ch.length;}
}
public void run(){
boolean atleast1 = false;
for(loc = current; !(loc == outcome && atleast1); loc = (loc+1) % choices.length){
spin1sector();
atleast1=true;
lock2.lock(); if(end){lock2.unlock(); break;} lock2.unlock();
}
current = outcome;
}
public void spin1sector(){
for(int i=0; i<(div+1); i++){
pos = i;
repaint();
try { Thread.sleep( 20 ); } catch (InterruptedException e) { }
lock2.lock(); if(end){lock2.unlock(); break;} lock2.unlock();
}
}
public void stop(){lock2.lock(); end = true; lock2.unlock();}
public void drawCenteredString(Graphics g, String text, Rectangle rect, Font font) {
FontMetrics metrics = g.getFontMetrics(font);
int x = rect.x + rect.width/2 - metrics.stringWidth(text)/2;
int y = rect.y + rect.height/2 + metrics.getHeight()/4;
g.setFont(font);
g.drawString(text, x, y);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
graphicsOffScreen.setColor(colors[(loc+1)%choices.length]);
graphicsOffScreen.fillRect(0, height*pos/div-height, width,height);
graphicsOffScreen.setColor(colors[loc]);
graphicsOffScreen.fillRect(0, height*pos/div, width, height);
graphicsOffScreen.setColor(new Color(0,0,0));
drawCenteredString(graphicsOffScreen,choices[(loc+1)%choices.length],new Rectangle(0, height*pos/div-height, width, height), font);
drawCenteredString(graphicsOffScreen,choices[loc],new Rectangle(0, height*pos/div, width, height), font);
graphicsOffScreen.setColor(new Color(255,255,255));
graphicsOffScreen.fillRect(width/4, 0, width/2, height/10);
graphicsOffScreen.setColor(new Color(0,0,0));
drawCenteredString(graphicsOffScreen,"Spin:",new Rectangle(width/4, 0, width/2, height/10), font);
g.drawImage( imageOffScreen, 0, 0, this);
}
}
}