comparison kopieckreta/src/KopiecKreta.java @ 0:2787f5e749ae

INIT
author prymula <prymula76@outlook.com>
date Thu, 21 Sep 2023 22:33:57 +0200
parents
children 489244127359
comparison
equal deleted inserted replaced
-1:000000000000 0:2787f5e749ae
1
2 /*
3 Kopiec Kreta - gra w odgadywanie słów
4 ver. 0.4.0-beta
5 autor: Przemysław R. Pietraszczyk
6 data: 2020-09-30
7 licencja: Public Domain
8
9 słownik pochodzi z: sjp.pl
10
11 */
12
13 package src;
14
15 import java.awt.*; // Toolkit
16 import javax.swing.JFrame;
17 import javax.swing.JLabel;
18 import javax.swing.JPanel;
19 import javax.swing.JButton;
20 //import javax.swing.JComponent;
21 import javax.swing.JMenuBar;
22 import javax.swing.JMenu;
23 import javax.swing.JMenuItem;
24 import javax.swing.JOptionPane;
25 import javax.swing.Box;
26 import javax.swing.JTextArea;
27 import javax.swing.JDialog;
28 import javax.swing.JTextField;
29 import javax.swing.JCheckBox;
30 import javax.swing.BorderFactory;
31 import javax.swing.WindowConstants;
32 import java.awt.event.*;
33 import java.awt.event.ActionListener;
34 //import java.awt.event.KeyListener;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.WindowEvent;
37 import java.awt.event.WindowAdapter;
38 import java.awt.event.ComponentAdapter;
39 import java.awt.event.ComponentEvent;
40 import java.awt.event.ItemEvent;
41 //import java.awt.event.KeyEvent;
42 import java.util.*; //Timer
43 //import java.awt.Robot;
44 import java.awt.Toolkit;
45
46 import java.beans.*; //property change stuff
47 import java.io.BufferedReader;
48 import java.io.InputStream;
49 import java.io.OutputStream;
50 import java.io.InputStreamReader;
51 import java.io.IOException;
52
53 public class KopiecKreta extends JTextArea implements ActionListener {
54
55 static JMenuBar menuBar;
56 static JMenu menu, info, menu2;
57 static JMenuItem menuItem, menuExit, menuInfo, menuPref;
58 static JFrame frame;
59 static boolean go=false;
60 static Timer timer = new Timer();
61 static String content;
62 final static int NRINGS=11;
63 final static int NCHAR=23; //32;
64 static MyResource res = new MyResource();
65 static wavResource wav;
66 static Ring []truering = new Ring[NRINGS];
67 static Ring []falsering = new Ring[NRINGS];
68 static String [] mound = new String[20]; // tekst do wyswietlenia przez paint
69 static JTextArea textArea;
70 static JPanel startScreen;
71 static int activeRing; // indeks ringu na którym jest prowadzona aktualnie gra
72 static KopiecKreta kk;
73 static Actions actions;
74 final static String CONSTCH="1234";
75 //final static Character[] charsInStock= {'a', 'ą', 'b', 'c', 'ć', 'd', 'e', 'ę', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'ł', 'm', 'n', 'ń', 'o', 'ó', 'p', 'r', 's', 'ś', 't', 'u', 'w', 'y', 'z', 'ż', 'ź'};
76 final static Character[] charsInStock= {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'w', 'y', 'z'};
77 static ArrayList<String> cheatSheet;
78 static int pointsForTheRing;
79 static int pointsFOrTheMund;
80 static int totalScore, level;
81 static boolean cheatActivate=false;
82 static SoundDialog sounddialog;
83 //static ResetDialog resetdialog;
84 //static GetNameDialog getNameDialog;
85 //Boolean runSoundDialog=Boolean.valueOf(false);
86 static ConfigFile configfile;
87 //static char pressKey=' ';
88
89 public KopiecKreta() {
90 //GridBagLayout gridbag = (GridBagLayout)getLayout();
91 //GridBagConstraints c = new GridBagConstraints();
92
93 menuBar = new JMenuBar();
94 menu = new JMenu("Plik");
95 menuBar.add(menu);
96 menu2 = new JMenu("Opcje");
97 menuBar.add(menu2);
98 // przenosi INFO na prawa strone
99 menuBar.add(Box.createHorizontalGlue());
100 info = new JMenu("Info");
101 // kierunek wyswietlania menu
102 info.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
103 menuBar.add(info);
104 menuItem = new JMenuItem("Nowa Gra", null);
105 menuItem.setActionCommand("MenuItem1");
106 menuItem.addActionListener(this);
107 menuItem.setToolTipText("Rozpoczyna nową grę.");
108 menu.add(menuItem);
109 //menuItem = new JMenuItem("Sala sławy", null);
110 //menuItem.setActionCommand("MenuItem11");
111 //menuItem.addActionListener(this);
112 //menuItem.setToolTipText("Wyświetla najlepsze wyniki");
113 //menu.add(menuItem);
114 menu.addSeparator();
115 menuExit = new JMenuItem("Zakończ", null);
116 menuExit.setActionCommand("MenuItem2");
117 menuExit.addActionListener(this);
118 menuExit.setToolTipText("Wyjście z programu");
119 menu.add(menuExit);
120 menuPref = new JMenuItem("Preferencje dzwięku",null);
121 menuPref.setActionCommand("MenuItem4");
122 menuPref.addActionListener(this);
123 menuPref.setToolTipText("Preferencje dotyczące dzwięku");
124 menu2.add(menuPref);
125 /*
126 menuPref = new JMenuItem("Resetowanie",null);
127 menuPref.setActionCommand("MenuItem5");
128 menuPref.addActionListener(this);
129 menuPref.setToolTipText("Resetowanie najlepszych wyników");
130 menu2.add(menuPref);
131 */
132 menuInfo = new JMenuItem("Pomoc", null);
133 menuInfo.setActionCommand("MenuItem33");
134 menuInfo.addActionListener(this);
135 menuInfo.setToolTipText("Samouczek");
136 info.add(menuInfo);
137 menuInfo = new JMenuItem("O Programie", null);
138 menuInfo.setActionCommand("MenuItem3");
139 menuInfo.addActionListener(this);
140 menuInfo.setToolTipText("Informacje o programie");
141 info.add(menuInfo);
142
143 //addKeyListener(this);
144 //actions = new Actions();
145 //addKeyListener(actions);
146 //setFocusable(true);
147
148
149 }
150
151
152 public void actionPerformed(final ActionEvent e) {
153
154 // Nowa gra
155 if(e.getActionCommand().equals("MenuItem1")) {
156 //getNameDialog.initUI(configfile, frame, getNameDialog.runGetNameDialog);
157 //getNameDialog.runGetNameDialog=true;
158 //if (getNameDialog.go){
159
160 activeRing=10;
161 totalScore=0;
162 pointsForTheRing=0;
163 pointsFOrTheMund=0;
164 level=1;
165 //frame.remove(startScreen);
166 //frame.add(textArea);
167 startScreen.setVisible(false);
168 textArea.setVisible(true);
169 create_secret_mound();
170 create_real_mound(); // generuje prawdziwy kopiec
171 cheatSheet = new ArrayList<String>();
172 setCharCheatSheet ();
173 go=true;
174 setDisplayMound();
175 // }
176 }
177 // Zakonczenie
178 else if(e.getActionCommand().equals("MenuItem2")) {
179 System.exit(0);
180 }
181 // O Programie
182 else if(e.getActionCommand().equals("MenuItem3")) {
183 String s1=" Port mojej gry pierwotnie napisanej dla Androida 2.3\n\n",
184 s2=" Program napisany na podstawie fragmentów kodów,\n",
185 s3=" wyszukanych w wyszukiwarce Google,\n",
186 s4=" plus własna inwencja\n\n",
187 s5=" Autor zarzeka się że nigdy nie posiadał, ani nie posiada\n",
188 s6=" żadnej ksiażki na temat Javy.\n",
189 s7="Oraz nie przeczytał ani jednej ksiązki na temat tego języka!\n\n",
190 s8=" Przemysław R. Pietraszczyk - jesień 2020\n",
191 s9=" Licencja: Public Domain\n\n",
192 s10=" Stworzono w oparciu o OpenJDK 11 oraz ANT\n\n",
193 s11=" o/s: "+configfile.osName+" ver. "+configfile.osVersion+"\n",
194 s12=" java ver: "+configfile.jvmVersion;
195
196 JOptionPane.showMessageDialog(frame,
197 s1+s2+s3+s4+s5+s6+s7+s8+s9+s10+s11+s12,
198 "Kopiec Kreta 0.4.0-beta",
199 JOptionPane.PLAIN_MESSAGE);
200
201 }
202 else if(e.getActionCommand().equals("MenuItem33")) {
203 String s1="Celem gry jest odgadnięcie wszyskich zakrytych pól.\n",
204 s2="Rozgrywkę rozpoczyna się od dolnego pierścienia.\n",
205 s3="Po odgadnięciu wszyskich liter w danym pierścieniu,\n",
206 s4="rozgrywka przenosi się na kolejny, wyższy ring.\n",
207 s5="Po odgadnięciu wszystkich liter, gracz pytany jest\n",
208 s6="o chęć gry na opcjonalnym następnym poziomie.\n\n",
209 s7="UWAGA ! Caps Lock - bez znaczenia !\n",
210 s8="\n";
211 JOptionPane.showMessageDialog(frame,
212 s1+s2+s3+s4+s5+s6+s7+s8,
213 "Pomoc",
214 JOptionPane.PLAIN_MESSAGE);
215 }
216 else if(e.getActionCommand().equals("MenuItem4")) {
217 sounddialog.initUI(configfile, frame, sounddialog.runSoundDialog);
218 sounddialog.runSoundDialog=true;
219 }
220 /*
221 else if(e.getActionCommand().equals("MenuItem5")) {
222 resetdialog.initUI(configfile);
223 }
224 */
225 // sala sławy
226 /*
227 else if(e.getActionCommand().equals("MenuItem11")) {
228
229 }
230 */
231 //System.out.println("ACTION PERFORMED " + e);
232 }
233
234 private static void createAndShowGUI() {
235
236 frame=new JFrame("Kopiec Kreta");
237 //Robot robot;
238 // menuBar z konstruktora
239 final int frameWidth = 250;
240 final int frameHeight = 460;
241 final int GNOMEPANEL=80; // nieco niżej niż panel
242 final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
243 //frame.setBounds((int) screenSize.getWidth() - frameWidth,
244 // (int) screenSize.getHeight() - frameHeight, frameWidth, frameHeight);
245 frame.setBounds((int) screenSize.getWidth() - frameWidth,
246 GNOMEPANEL, frameWidth, frameHeight);
247 frame.setResizable(false);
248 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
249 sounddialog = new SoundDialog();
250 //resetdialog = new ResetDialog();
251 //getNameDialog = new GetNameDialog();
252 textArea = new KopiecKreta();
253 startScreen = new StartScreen();
254 //textArea.setFont(new Font("Serif", Font.ITALIC, 18));
255 textArea.setFont(new Font("Serif", Font.BOLD, 18));
256 textArea.setLineWrap(true); //nowe
257 textArea.setWrapStyleWord(true); // nowe
258 textArea.setEditable(false);
259 textArea.setBounds(5,5,240,430);
260 frame.add(textArea);
261 textArea.setVisible(false);
262 frame.add(menuBar);
263 frame.add(startScreen);
264 startScreen.setFont(new Font("Serif", Font.BOLD, 18));
265 startScreen.setBounds(5,5,240,430);
266
267 frame.setJMenuBar(menuBar);
268 frame.setLayout(null);
269 frame.setSize(250,500); // 400 x 400
270 frame.setVisible(true);
271 actions = new Actions();
272 frame.addKeyListener(actions);
273 frame.setFocusable(true);
274 textArea.setForeground(Color.BLACK);
275 textArea.setBackground(Color.GREEN);
276 // zapobiega utracie KeyEvent po wyjściu z SoundDialog i ResetDialog
277 frame.addFocusListener(new FocusListener(){
278 public void focusGained(FocusEvent e){
279 //System.out.println("Focus GAINED:"+e);
280 }
281 public void focusLost(FocusEvent e){
282 //System.out.println("Focus LOST:"+e);
283
284 // FIX FOR GNOME/XWIN FOCUS BUG
285 e.getComponent().requestFocus();
286 }
287 });
288 try {
289 configfile = new ConfigFile();
290 }catch (IOException e){}
291 configfile.load();
292
293 /*
294 try {
295 // obiekt robot posiada metode do pobierania koloru pixela
296 robot = new Robot();
297 robot.mouseMove(screenSize.width-100, 200);
298 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
299 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
300 }catch (AWTException e) {}
301 frame.setAlwaysOnTop(true);
302 */
303
304 timer.schedule(new KeyTicks(), 0, 1000); // lepiej 1000
305
306 }
307 // dodaje punkty za pierścień i kazuje pozostałe litery ściągi
308 static public void addScores(){
309 int n;
310 pointsForTheRing=0;
311 if (!cheatActivate){
312 for (int i=0;i< cheatSheet.size();i++){
313 pointsForTheRing++;
314 }
315 pointsFOrTheMund+=pointsForTheRing;
316 }
317 //System.out.println("Naliczyłem "+pointsForTheRing+" punktów za ring");
318 }
319 // zapełnia tablice ze znakami do podpowiedzi
320 static private void setCharCheatSheet (){
321
322 for (int i=0; i<NCHAR; i++){
323 // przepisujemy kolejno znaki do listy stringów
324 char c= Character.toUpperCase(charsInStock[i]);
325 cheatSheet.add(String.valueOf(c));
326 }
327 }
328 static private void refreshCheatSheet(int n){
329 String t="";
330 cheatSheet.remove(n);
331 for (int i=0;i< cheatSheet.size();i++){
332 t+=cheatSheet.get(i);
333 }
334 }
335 // szuka znaku w tablicy z podpowiedzia
336 static private int searchCharChaetSheet(char c){
337 String s="";
338 String sc="";
339 //String test="";
340 for (int i=0;i< cheatSheet.size();i++){
341 s=cheatSheet.get(i);
342 sc = String.valueOf(c);
343 // test+=sc;
344 if (s.equals(sc)) {
345 return i;
346 }
347 }
348 //System.out.println("Nie odnalazłem znaku:"+sc);
349 return -1;
350 }
351 static private void create_real_mound(){
352
353 int n_char=3;
354 String r="";
355
356 for (int i=0; i<NRINGS; i++){
357 truering[i] = new Ring(res.getDirectory(n_char));
358 n_char++;
359 for (int j=0;j<truering[i].getLength();j++){
360 String _r=Character.toString(truering[i].getChar(j));
361
362 r+=_r;
363 }
364 r+="\n";
365 }
366
367 System.out.println(r);
368
369 }
370 public static void create_secret_mound(){
371 // kodujemy od 3 liter
372 String s="???";
373
374 for (int i=0; i<NRINGS; i++){
375 falsering[i] = new Ring(s);
376 s+="?";
377 }
378 }
379
380 public static void main(final String[] args) {
381
382 try {
383 res.read();
384 //wav = new wavResource();
385 //wav.loadWav();
386 //wav.initWav();
387 }catch (Exception e) {
388 e.printStackTrace();
389 }
390 javax.swing.SwingUtilities.invokeLater(new RunnableImplementation());
391 }
392
393 static private void setDisplayMound(){
394 String sr = new String();
395 String r="";
396 textArea.setText(null);
397 sr+="\n";
398 // i=1 ponieważ pomijamy 3 literowy ring
399 for (int i=1;i<NRINGS;i++){
400
401 //String r="";
402 for (int j=0;j<truering[i].getLength();j++){
403
404 String _r=Character.toString(truering[i].getChar(j));
405 r+=_r;
406 }
407 //System.out.println(r);
408 r="";
409 sr+=" ";
410 if (i==activeRing){
411 sr+=">";
412 }
413 else sr+=" ";
414 for (int j=0;j<falsering[i].getLength();j++){
415 String _s=Character.toString(falsering[i].getChar(j));
416 sr +=_s;
417 }
418 if (i==activeRing){
419 sr+="<";
420 }
421 mound[i]=sr;
422 //System.out.println("mound["+i+"] "+mound[i]);
423
424 textArea.append(mound[i]+"\n");
425 sr="";
426 }
427 textArea.append("\n\n");
428 for (int i=0; i<cheatSheet.size();i++){
429 textArea.append(cheatSheet.get(i));
430 textArea.append(" ");
431 }
432
433 }
434
435
436 private static final class RunnableImplementation implements Runnable {
437 public void run(){
438 wav = new wavResource();
439 createAndShowGUI();
440 }
441 }
442 public static void unhideWords(){
443 for (int j=0;j<truering[activeRing].getLength();j++){
444 falsering[activeRing].setChar(truering[activeRing].getChar(j),j);
445 }
446 }
447
448 static class KeyTicks extends TimerTask {
449 int cw=0;
450 static char pressKey='Q';
451 static char lastKey='Q';
452 static boolean firstRun=true;
453 int n=0;
454 /* FIXME - miga gdy odnaleziono litere ?
455 */
456 public void run() {
457 ArrayList<Integer> indexExist = new ArrayList<Integer>();
458 int returnIndex;
459
460
461 pressKey=actions.getKey();
462
463 if (go) {
464 wav.stop();
465 indexExist=checkInstance(pressKey);
466 for (int exist: indexExist){
467 falsering[activeRing].setChar(pressKey,exist);
468 wav.PlayFlag=true;
469 if (lastKey != pressKey) {
470 if (sounddialog.soundAccept()){
471 System.out.println("Dzwięk akceptuje");
472 wav.play("/smooth-ok.wav");
473 }
474 else System.out.println("Dzwięk NIE akceptuje");
475 }
476 }
477 if (lastKey != pressKey && indexExist.isEmpty() && !firstRun) {
478 //if (lastKey != pressKey && indexExist.isEmpty()) {
479
480 wav.PlayFlag=true;
481 if (sounddialog.soundAccept()){
482 System.out.println("Dzwięk akceptuje");
483 wav.play("/bass-bad.wav");
484 }
485 else System.out.println("Dzwięk NIE akceptuje");
486 }
487 lastKey=pressKey;
488
489 returnIndex = searchCharChaetSheet(pressKey);
490 if (returnIndex != -1){
491 refreshCheatSheet(returnIndex);
492 }
493 setDisplayMound();
494 pressKey=' ';
495 indexExist.clear();
496
497 if (actions.getCheat().equals(CONSTCH)){
498 //pointsForTheRing=0;
499 actions.resetCheat();
500 cheatActivate=true;
501 unhideWords();
502 }
503 if (checkFullRing()) {
504 totalScore+=pointsForTheRing;
505 setCharCheatSheet ();
506 setDisplayMound();
507
508 }
509
510 firstRun=false;
511 }
512 }
513 static private boolean checkFullRing() {
514 if (truering[activeRing].getString().equals(falsering[activeRing].getString())){
515 //System.out.println("Czy zaliczony pierścień ? activeRing:"+activeRing);
516 if (activeRing >1){
517 addScores();
518 cheatActivate=false;
519 // nowa ściąga
520 cheatSheet = new ArrayList<String>();
521 activeRing--;
522 return true;
523 }
524 else {
525 go=false;
526
527 setDisplayMound();
528 if(activeRing==1){
529
530 String s1="Ukończyłeś poziom "+level+" !\n",
531 s2="Na tym poziomie uzyskałeś "+pointsFOrTheMund+" punktów.\n",
532 s3="Póki co ugrałeś "+totalScore+" punktów !\n\n",
533 s4="Kontynujesz grę ?";
534
535 // moze boolean ?
536 int dialogButton = JOptionPane.YES_NO_OPTION;
537 int answer = JOptionPane.showConfirmDialog (frame, s1+s2+s3+s4,"Komunikat", dialogButton);
538
539 if(answer == JOptionPane.YES_OPTION) {
540 activeRing=10;
541 go=true;
542 level++;
543 create_secret_mound();
544 create_real_mound(); // generuje prawdziwy kopiec
545 setDisplayMound();
546 pointsFOrTheMund=0;
547 }
548 if(answer == JOptionPane.NO_OPTION) {
549 System.exit(0);
550
551 }
552 }
553 }
554 }
555 return false;
556 }
557 // sprawdza czy rządana litera znajduje sie w pierscieniu
558 static private ArrayList<Integer> checkInstance(char key){
559 ArrayList<Integer> indexExist = new ArrayList<Integer>();
560
561 for (int i=0; i<truering[activeRing].getLength();i++){
562 if (truering[activeRing].getChar(i) == key) {
563 indexExist.add(i);
564 }
565 }
566 /*
567 if (indexExist.size()!=0) {
568 changeView=true;
569 }
570 */
571 return indexExist;
572 }
573 }
574 static class Ring {
575 char c[];
576 int l=0; // dlugosc
577 public Ring(String s){
578 //System.out.println("Z Ring constructor: "+s+" dlugosc: "+s.length());
579 c = new char[s.length()+2];
580 c =s.toCharArray();
581 l=s.length();
582 }
583 public char getChar(int poz){
584 return (char) c[poz];
585 }
586 public void setChar(char _c,int p){
587 c[p]=_c;
588 }
589 public int getLength(){
590 return l;
591 }
592 public String getString(){
593 String sr = new String();
594 for (int j=0;j<l;j++){
595 String _s=Character.toString(c[j]);
596 sr +=_s;
597 }
598 return sr;
599 }
600 }
601 }
602 class SoundDialog implements ItemListener, ActionListener{
603 JCheckBox chkBox1;
604 JFrame frame;
605 boolean sound = true;
606 ConfigFile config;
607 public Boolean runSoundDialog=false;
608
609 protected void initUI(ConfigFile cf, JFrame f, Boolean run) {
610 if (!run) {
611 frame = new JFrame("Zapytanko");
612 frame.getContentPane().setSize(new Dimension(300,200)); // nie działa
613 frame.getContentPane().setLayout(new GridBagLayout());
614 frame.setAlwaysOnTop(true);
615 GridBagConstraints c = new GridBagConstraints();
616 // nadpisanie metody zamykania okna
617 frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
618 frame.addWindowListener(new WindowAdapter() {
619 public void windowClosing(WindowEvent ev) {
620 runSoundDialog=false;
621 frame.dispose();
622 }
623 // zapobiega minimalizacji okna dialogowego
624 public void windowIconified(WindowEvent we) {
625 frame.setState(JFrame.NORMAL);
626 //JOptionPane.showMessageDialog(frame, "Cant Minimize");
627 }
628 });
629 JLabel label = new JLabel(" ");
630 JButton button = new JButton("Zatwierdź");
631 chkBox1= new JCheckBox("Odtwarzaj dźwięk");
632 config=cf;
633 sound = cf.load();
634 System.out.println("Dzwiek: "+sound);
635 chkBox1.setSelected(sound);
636 c.fill = GridBagConstraints.HORIZONTAL;
637 c.ipady = 0;
638 c.weighty = 1.0;
639 c.gridwidth = 3;
640 c.gridx = 0;
641 c.gridy = 0;
642 frame.getContentPane().add(chkBox1,c);
643
644 c.fill = GridBagConstraints.HORIZONTAL;
645 c.ipady = 0;
646 c.weighty = 1.0;
647 c.gridwidth = 3;
648 c.gridx = 0;
649 c.gridy = 1;
650 frame.getContentPane().add(label,c);
651
652 c.fill = GridBagConstraints.HORIZONTAL;
653 c.ipady = 0;
654 c.weighty = 1.0;
655 c.anchor = GridBagConstraints.PAGE_END; //bottom of space
656 c.gridwidth = 3;
657 c.gridx = 0;
658 c.gridy = 2;
659 frame.getContentPane().add(button,c);
660
661 chkBox1.addItemListener(this);
662 button.addActionListener(this);
663 if (cf.osName.equals("Linux")) frame.setUndecorated(true);
664 chkBox1.setBorder(BorderFactory.createMatteBorder(20, 20, 20, 20, Color.RED)); // w tym wypadku kolor nie ma znaczenia
665 frame.getRootPane().setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.RED));
666 frame.pack();
667 // pojawi się nad oknem rodzica
668 frame.setLocationRelativeTo(f);
669 frame.setResizable(false);
670 frame.setVisible(true);
671 }
672 }
673 public void itemStateChanged(ItemEvent e) {
674
675 Object source = e.getItemSelectable();
676
677 if (source == chkBox1) {
678 //System.out.println("Zaznaczono dzwiek");
679 sound = true;
680 }
681 if (e.getStateChange() == ItemEvent.DESELECTED) {
682 //System.out.println("Odznaczono dzwiek");
683 sound = false;
684
685 }
686
687 }
688 public void actionPerformed(ActionEvent e) {
689 frame.setVisible(false);
690 config.save(sound);
691 runSoundDialog=false;
692 //System.out.println("Zapisuje dzwięk: "+sound);
693
694 }
695 public boolean soundAccept(){
696 return sound;
697 }
698 }
699 /*
700 class ResetDialog implements ItemListener, ActionListener{
701 JCheckBox chkBox1;
702 JFrame frame;
703 boolean reset = false;
704 protected void initUI(ConfigFile cf) {
705
706 frame = new JFrame("Zapytanko");
707
708 JPanel mainPanel;
709 JButton button;
710 frame.getContentPane().setLayout(new GridBagLayout());
711 GridBagConstraints c = new GridBagConstraints();
712 button = new JButton("Zatwierdź");
713 //chkBox1= new JCheckBox("Odtwarzaj dźwięk");
714 chkBox1= new JCheckBox("Zresetuj najlepsze wyniki");
715
716 chkBox1.setSelected(reset);
717 c.fill = GridBagConstraints.HORIZONTAL;
718 c.ipady = 0;
719 c.weighty = 1.0;
720 //c.anchor = GridBagConstraints.PAGE_END; //bottom of space
721 c.gridwidth = 3;
722 c.gridx = 0;
723 c.gridy = 0;
724 frame.getContentPane().add(chkBox1,c);
725
726
727
728 c.fill = GridBagConstraints.HORIZONTAL;
729 c.ipady = 0;
730 c.weighty = 1.0;
731 c.anchor = GridBagConstraints.PAGE_END; //bottom of space
732 c.gridwidth = 3;
733 c.gridx = 0;
734 c.gridy = 2;
735 frame.getContentPane().add(button,c);
736
737
738
739 chkBox1.addItemListener(this);
740 button.addActionListener(this);
741 frame.pack();
742 frame.setLocationRelativeTo(null);
743 frame.setResizable(false);
744 frame.setVisible(true);
745 }
746 public void itemStateChanged(ItemEvent e) {
747 //t index = 0;
748 //ar c = '-';
749 Object source = e.getItemSelectable();
750
751 if (source == chkBox1) {
752 //dex = 0;
753 System.out.println("Zaznaczono dzwiek");
754 reset = true;
755 }
756 if (e.getStateChange() == ItemEvent.DESELECTED) {
757 System.out.println("Odznaczono dzwiek");
758 reset = false;
759
760 }
761
762 }
763 public void actionPerformed(ActionEvent e) {
764 frame.setVisible(false);
765 System.out.println("jestem action");
766 }
767 public boolean resetAccept(){
768 return reset;
769 }
770 }
771 */
772 /*
773 class GetNameDialog implements ActionListener, KeyListener {
774 JTextField text;
775 JFrame frame;
776 public String namePlayer="";
777 public boolean go;
778 ConfigFile config;
779 public Boolean runGetNameDialog=false;
780
781 protected void initUI(ConfigFile cf, JFrame f, Boolean run) {
782 if (!run) {
783 frame = new JFrame("Zapytanko");
784 frame.getContentPane().setSize(new Dimension(300,200)); // nie działa
785 frame.getContentPane().setLayout(new GridBagLayout());
786 frame.setAlwaysOnTop(true);
787 GridBagConstraints c = new GridBagConstraints();
788 // nadpisanie metody zamykania okna
789 frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
790 frame.addWindowListener(new WindowAdapter() {
791 public void windowClosing(WindowEvent ev) {
792 runGetNameDialog=false;
793 frame.dispose();
794 }
795 });
796 JLabel label1 = new JLabel(" ");
797 JLabel label2 = new JLabel("Najperw się przedstaw:");
798 JButton button = new JButton("Zatwierdź");
799 button.setActionCommand("Button");
800
801 text = new JTextField();
802 //text.addActionListener(this);
803 //text.setActionCommand("Text");
804
805 text.addKeyListener(this);
806 text.setFocusable(true);
807 text.requestFocus();
808
809 c.fill = GridBagConstraints.HORIZONTAL;
810 c.ipady = 0;
811 c.weighty = 1.0;
812 c.gridwidth = 3;
813 c.gridx = 0;
814 c.gridy = 0;
815 frame.getContentPane().add(label2,c);
816
817 c.fill = GridBagConstraints.HORIZONTAL;
818 c.ipady = 0;
819 c.weighty = 1.0;
820 c.gridwidth = 3;
821 c.gridx = 0;
822 c.gridy = 1;
823 frame.getContentPane().add(text,c);
824
825 c.fill = GridBagConstraints.HORIZONTAL;
826 c.ipady = 0;
827 c.weighty = 1.0;
828 c.gridwidth = 3;
829 c.gridx = 0;
830 c.gridy = 2;
831 frame.getContentPane().add(label1,c);
832
833 c.fill = GridBagConstraints.HORIZONTAL;
834 c.ipady = 0;
835 c.weighty = 1.0;
836 c.anchor = GridBagConstraints.PAGE_END; //bottom of space
837 c.gridwidth = 3;
838 c.gridx = 0;
839 c.gridy = 3;
840 frame.getContentPane().add(button,c);
841 text.setEditable(true);
842 if (namePlayer.length()!=0){
843 text.setText(namePlayer);
844 text.selectAll();
845 }
846 button.addActionListener(this);
847 if (cf.osName.equals("Linux")) frame.setUndecorated(true);
848 frame.getRootPane().setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.RED));
849 frame.pack();
850 frame.setLocationRelativeTo(f);
851 frame.setResizable(false);
852 frame.setVisible(true);
853 }
854 }
855
856 public void actionPerformed(ActionEvent e) {
857 if(e.getActionCommand().equals("Button")) {
858
859 frame.setVisible(false);
860 namePlayer=text.getText();
861 runGetNameDialog=false;
862 if (namePlayer.length()!=0){
863 go=true;
864 }
865 }
866 else if(e.getActionCommand().equals("Text")) {
867
868 String t= text.getText();
869 text.setText(t);
870 text.selectAll();
871
872 }
873 }
874
875 @Override
876 public void keyTyped(KeyEvent e) {
877 }
878
879 @Override
880 public void keyReleased(KeyEvent event) {
881 }
882
883 @Override
884 public void keyPressed(KeyEvent event) {
885 if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('a')) {
886 System.out.println("OK");
887 }
888 System.out.println("GETNAME");
889 String t= text.getText();
890 text.setText(t);
891 text.selectAll();
892 }
893 }
894
895 /*
896 private void readConfig() {
897
898
899 try {
900 InputStream is = this.getClass().getResourceAsStream("/config");
901 BufferedReader br=new BufferedReader(new InputStreamReader(is, "utf-8"));
902
903 lineConfig = br.readLine();
904 } catch (Exception e){
905 e.printStackTrace();
906 }
907 }
908 */
909 /*
910 private void writeConfig() {
911
912
913 try {
914 OutputStream is = this.getClass().getResourceAsStream("/config");
915 BufferedReader br=new BufferedReader(new InputStreamReader(is, "utf-8"));
916
917 lineConfig = br.readLine();
918 } catch (Exception e){
919 e.printStackTrace();
920 }
921 }
922 */
923 //}