0
|
1 /*
|
|
2 Trefle - ukladanka z puzzli
|
|
3 Przemyslaw R. Pieraszczyk
|
|
4 20-01-2021
|
|
5 licencja - GPL
|
|
6 powered by ATOM
|
|
7 */
|
|
8
|
|
9
|
|
10 package trefle;
|
|
11
|
|
12 import java.awt.*; // Toolkit
|
|
13 import javax.swing.JFrame;
|
|
14 import javax.swing.JLabel;
|
|
15 import javax.swing.JPanel;
|
|
16 import javax.swing.JButton;
|
|
17 //import javax.swing.JComponent;
|
|
18 import javax.swing.JMenuBar;
|
|
19 import javax.swing.JMenu;
|
|
20 import javax.swing.JMenuItem;
|
|
21 import javax.swing.JOptionPane;
|
|
22 import javax.swing.Box;
|
|
23 import javax.swing.JTextArea;
|
|
24 import javax.swing.JDialog;
|
|
25 import javax.swing.JTextField;
|
|
26 import javax.swing.JCheckBox;
|
|
27 import javax.swing.BorderFactory;
|
|
28 import javax.swing.WindowConstants;
|
|
29 import java.awt.event.*;
|
|
30 import java.awt.event.ActionListener;
|
|
31 //import java.awt.event.KeyListener;
|
|
32 import java.awt.event.ActionEvent;
|
|
33 import java.awt.event.WindowEvent;
|
|
34 import java.awt.event.WindowAdapter;
|
|
35 import java.awt.event.ComponentAdapter;
|
|
36 import java.awt.event.ComponentEvent;
|
|
37 import java.awt.event.ItemEvent;
|
|
38
|
|
39 import java.awt.event.MouseListener;
|
|
40 import java.awt.event.MouseEvent;
|
|
41
|
|
42 //import java.awt.event.KeyEvent;
|
|
43 import java.util.*; //Timer
|
|
44 //import java.awt.Robot;
|
|
45 import java.awt.Toolkit;
|
|
46
|
|
47 import java.beans.*; //property change stuff
|
|
48 import java.io.BufferedReader;
|
|
49 import java.io.InputStream;
|
|
50 import java.io.OutputStream;
|
|
51 import java.io.InputStreamReader;
|
|
52 import java.io.IOException;
|
|
53 import java.io.*;
|
|
54
|
|
55 import java.awt.image.BufferedImage;
|
|
56 import java.awt.image.*;
|
|
57 import javax.imageio.ImageIO;
|
|
58 import javax.imageio.*;
|
|
59
|
|
60
|
|
61 public class Trefle extends JPanel implements ActionListener {
|
|
62
|
|
63 static JMenuBar menuBar;
|
|
64 static JMenu menu, info, menu2, nowplay;
|
|
65 static JMenuItem menuItem, menuExit, menuInfo, menuPref;
|
|
66 static JFrame frame;
|
|
67 //static boolean go=false;
|
|
68 //static JPanel screen;
|
|
69 static Trefle trefle;
|
|
70 static Timer timer = new Timer();
|
|
71
|
|
72 //static Actions actions;
|
|
73 //static Mata mata;
|
|
74
|
|
75 private Graphics bg;
|
|
76 private Image bufor;
|
|
77 static private int x;
|
|
78 static private int y;
|
|
79 //private Mata mata;
|
|
80
|
|
81 //private Puste puste;
|
|
82 //static boolean zaczynamy;
|
|
83 static int nmove;
|
|
84 BufferedImage imageNow;
|
|
85 static Mouse mouse;
|
|
86 static MouseMotion mouseMotion;
|
|
87 //Image img;
|
|
88 //Image cell[]=new Image[6*4];
|
|
89 static CellPicture origPicture[][] = new CellPicture[6][4];
|
|
90 static CellPicture cellPicture[][] = new CellPicture[6][4];
|
|
91 static CellPicture wallPaper[][] = new CellPicture[6][4];
|
|
92 //static CellWallPaper wallPaper[][] = new CellWallPaper[6][4];
|
|
93 static CellPicture dragAndDrop;
|
|
94 BufferedImage wallPaperImage=null;
|
|
95 BufferedImage borderImage=null;
|
|
96 BufferedImage startScreen=null;
|
|
97 boolean startscreen=true;
|
|
98 static boolean go=false;
|
|
99 static int getBorderX;
|
|
100 static int getBorderY;
|
|
101
|
|
102 static int iw,ih,tw,th;
|
|
103 static int move;
|
|
104
|
|
105
|
|
106 public Trefle (){
|
|
107
|
|
108 menuBar = new JMenuBar();
|
|
109 menu = new JMenu("Plik");
|
|
110 menuBar.add(menu);
|
|
111 //menu2 = new JMenu("Options");
|
|
112 //menuBar.add(menu2);
|
|
113 // przenosi INFO na prawa strone
|
|
114 menuBar.add(Box.createHorizontalGlue());
|
|
115 info = new JMenu("Info");
|
|
116 // kierunek wyswietlania menu
|
|
117 info.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
|
|
118 menuBar.add(info);
|
|
119
|
|
120 nowplay = new JMenu("Nowa Gra");
|
|
121 nowplay.setToolTipText("Rozpoczyna nowa gre.");
|
|
122 menu.add(nowplay);
|
|
123
|
|
124 menuItem = new JMenuItem("Anielica", null);
|
|
125 menuItem.setActionCommand("NewGame1");
|
|
126 menuItem.addActionListener(this);
|
|
127 menuItem.setToolTipText("Piekna Anielica");
|
|
128
|
|
129 nowplay.add(menuItem);
|
|
130
|
|
131 menuItem = new JMenuItem("Zielony", null);
|
|
132 menuItem.setActionCommand("NewGame2");
|
|
133 menuItem.addActionListener(this);
|
|
134 menuItem.setToolTipText("Zielony Archaniol");
|
|
135
|
|
136 nowplay.add(menuItem);
|
|
137
|
|
138 menuItem = new JMenuItem("Niebieski", null);
|
|
139 menuItem.setActionCommand("NewGame3");
|
|
140 menuItem.addActionListener(this);
|
|
141 menuItem.setToolTipText("Niebieski Archaniol");
|
|
142
|
|
143 nowplay.add(menuItem);
|
|
144
|
|
145 menuItem = new JMenuItem("Na Linie", null);
|
|
146 menuItem.setActionCommand("NewGame4");
|
|
147 menuItem.addActionListener(this);
|
|
148 menuItem.setToolTipText("Aniol na linie");
|
|
149
|
|
150 nowplay.add(menuItem);
|
|
151
|
|
152 //menuItem = new JMenuItem("Pojedynczy", null);
|
|
153 //menuItem.setActionCommand("NewGame0");
|
|
154 //menuItem.addActionListener(this);
|
|
155 //menuItem.setToolTipText("Wlasciwie nic nie miesza");
|
|
156
|
|
157 //nowplay.add(menuItem);
|
|
158
|
|
159 //to nie
|
|
160 //menu.add(menuItem);
|
|
161
|
|
162 menu.addSeparator();
|
|
163 menuExit = new JMenuItem("Wyjscie", null);
|
|
164 menuExit.setActionCommand("EndGame");
|
|
165 menuExit.addActionListener(this);
|
|
166 menuExit.setToolTipText("Wyjscie z programu");
|
|
167 menu.add(menuExit);
|
|
168
|
|
169 menuInfo = new JMenuItem("Pomoc", null);
|
|
170 menuInfo.setActionCommand("Help");
|
|
171 menuInfo.addActionListener(this);
|
|
172 menuInfo.setToolTipText("HOWTO");
|
|
173 info.add(menuInfo);
|
|
174 menuInfo = new JMenuItem("O Programie", null);
|
|
175 menuInfo.setActionCommand("About");
|
|
176 menuInfo.addActionListener(this);
|
|
177 menuInfo.setToolTipText("Informacje o programie");
|
|
178 info.add(menuInfo);
|
|
179
|
|
180 LoadWallPaper();
|
|
181 LoadBorder();
|
|
182 LoadStartScreen();
|
|
183
|
|
184 }
|
|
185 public void actionPerformed(final ActionEvent e) {
|
|
186
|
|
187 // Nowa gra
|
|
188 if(e.getActionCommand().equals("NewGame1")) {
|
|
189
|
|
190 imageNow = LoadPicture("/aniol.JPG");
|
|
191 CropImage();
|
|
192 MixImage();
|
|
193 ZerosWallPaper();
|
|
194 dragAndDrop =null; //= new CellPicture(0,0,0,0,null,0); // inicjujemy wydmuszke z image=null
|
|
195 go=true;
|
|
196 startscreen=false;
|
|
197 this.repaint();
|
|
198 //nmove=0;
|
|
199
|
|
200 move=0;
|
|
201 //mata.setMixedFields(0);
|
|
202 System.out.println("ZACZNAMY");
|
|
203 //screen.setVisible(true);
|
|
204 //nPuzzle.repaint();
|
|
205 }
|
|
206 else if(e.getActionCommand().equals("NewGame2")) {
|
|
207
|
|
208 imageNow = LoadPicture("/zielony_archaniol.jpg");
|
|
209 CropImage();
|
|
210 MixImage();
|
|
211 ZerosWallPaper();
|
|
212 dragAndDrop = null; //new CellPicture(0,0,0,0,null,0); // inicjujemy wydmuszke z image=null
|
|
213 go=true;
|
|
214 startscreen=false;
|
|
215 this.repaint();
|
|
216 move=0;
|
|
217 System.out.println("ZACZNAMY");
|
|
218 //screen.setVisible(true);
|
|
219 //nPuzzle.repaint();
|
|
220 }
|
|
221 else if(e.getActionCommand().equals("NewGame3")) {
|
|
222
|
|
223 imageNow = LoadPicture("/niebieski_archaniol.jpg");
|
|
224 CropImage();
|
|
225 MixImage();
|
|
226 ZerosWallPaper();
|
|
227 dragAndDrop = null; //new CellPicture(0,0,0,0,null,0); // inicjujemy wydmuszke z image=null
|
|
228 go=true;
|
|
229 startscreen=false;
|
|
230 this.repaint();
|
|
231 move=0;
|
|
232 System.out.println("ZACZNAMY");
|
|
233 //screen.setVisible(true);
|
|
234 //nPuzzle.repaint();
|
|
235 }
|
|
236 else if(e.getActionCommand().equals("NewGame4")) {
|
|
237
|
|
238 imageNow = LoadPicture("/nalinie.jpg");
|
|
239 CropImage();
|
|
240 MixImage();
|
|
241 ZerosWallPaper();
|
|
242 dragAndDrop = null; //new CellPicture(0,0,0,0,null,0); // inicjujemy wydmuszke z image=null
|
|
243 go=true;
|
|
244 startscreen=false;
|
|
245 this.repaint();
|
|
246 move=0;
|
|
247 System.out.println("ZACZNAMY");
|
|
248 //screen.setVisible(true);
|
|
249 //nPuzzle.repaint();
|
|
250 }
|
|
251 // Zakonczenie
|
|
252 else if(e.getActionCommand().equals("EndGame")) {
|
|
253 System.exit(0);
|
|
254 }
|
|
255 else if(e.getActionCommand().equals("About")) {
|
|
256 String
|
|
257 s2=" Program napisany na podstawie fragmentow kodow,\n",
|
|
258 s3=" wyszukanych w wyszukiwarce Google,\n",
|
|
259 s4=" plus wlasna inwencja\n\n",
|
|
260 s5=" Przemyslaw R. Pietraszczyk - styczen 2021\n",
|
|
261 s6=" Licencja: GPL\n\n",
|
|
262 s7=" Stworzono w oparciu o OpenJDK 11 oraz ANT\n\n",
|
|
263 s8=" o/s: "+System.getProperty("os.name")+" ver: "+System.getProperty("os.version")+"\n",
|
|
264 s9=" java ver: "+System.getProperty("java.vm.version")+"\n";
|
|
265
|
|
266 JOptionPane.showMessageDialog(frame,
|
|
267 s2+s3+s4+s5+s6+s7+s8+s9,
|
|
268 "Anielskie Puzzle - 0.2-1",
|
|
269 JOptionPane.PLAIN_MESSAGE);
|
|
270
|
|
271 }
|
|
272 else if(e.getActionCommand().equals("Help")) {
|
|
273 String s1="Ukladanka polegajaca na metodzie przeciagnij i upusc elementow z prawej strony na lewa.\n Dokladniej mowiac nalezy kliknac i przytrzymac lewy klawisz myszki na elemencie\n z prawej strony a nastepnie nie puszczajac przycisku,\n przesunac obrazek nad lewa kratownice, umieszczajac element w wybranym punkcie,\n poprzez puszczenie lewego przycisku myszki.\n Po prawidlowym ulozeniu wszystkich obrazkow w jeden duzy obraz,\n aplikacja wyswietli okienko z iloscia wykonanych posuniec.\n",
|
|
274 s2="Dozwolone jest rowniez przenoszenie elementow z lewej na prawa strone,\n jak i w zasiegu jednej kratownicy.",
|
|
275 s3="\n";
|
|
276 JOptionPane.showMessageDialog(frame,
|
|
277 s1+s2+s3,
|
|
278 "Pomoc",
|
|
279 JOptionPane.PLAIN_MESSAGE);
|
|
280 }
|
|
281 }
|
|
282 private static void createAndShowGUI() {
|
|
283
|
|
284 frame=new JFrame("Anielskie Puzzle");
|
|
285 //mata = new Mata();
|
|
286 //mata.setUnmixedFields();
|
|
287 // menuBar z konstruktora
|
|
288 final int frameWidth = 1200; //1300;
|
|
289 final int frameHeight = 680;
|
|
290 final int GNOMEPANEL=80; // nieco nizej niz panel
|
|
291 final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
|
292 //frame.setBounds((int) screenSize.getWidth() - frameWidth,
|
|
293 // (int) screenSize.getHeight() - frameHeight, frameWidth, frameHeight);
|
|
294 frame.setBounds((int) screenSize.getWidth() - frameWidth,
|
|
295 GNOMEPANEL, frameWidth, frameHeight);
|
|
296 frame.setResizable(false);
|
|
297 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
298
|
|
299 trefle= new Trefle();
|
|
300 //screen = new PuzzleCanvas(true, mata);
|
|
301 frame.add(trefle);
|
|
302 //trefle.setPreferredSize(new Dimension(1200, 640));
|
|
303 trefle.setBounds(5,5, 1170,640); // 1200, 640
|
|
304 //frame.add(screen);
|
|
305 //nPuzzle.setVisible(true);
|
|
306 frame.add(menuBar);
|
|
307
|
|
308 frame.setJMenuBar(menuBar);
|
|
309 frame.setLayout(null);
|
|
310 //frame.setSize(250,400); // 400 x 400
|
|
311 frame.setVisible(true);
|
|
312 mouse = new Mouse();
|
|
313 mouseMotion = new MouseMotion();
|
|
314 mouse.setCanvas(trefle);
|
|
315 frame.addMouseListener(mouse);
|
|
316 frame.addMouseMotionListener(mouseMotion);
|
|
317 //addMouseListener(mouse);
|
|
318 //frame.addKeyListener(actions);
|
|
319 frame.setFocusable(true);
|
|
320
|
|
321 // zapobiega utracie KeyEvent po wyjsciu z SoundDialog i ResetDialog
|
|
322 /*
|
|
323 frame.addFocusListener(new FocusListener(){
|
|
324 public void focusGained(FocusEvent e){
|
|
325 //System.out.println("Focus GAINED:"+e);
|
|
326 }
|
|
327 public void focusLost(FocusEvent e){
|
|
328 //System.out.println("Focus LOST:"+e);
|
|
329
|
|
330 // FIX FOR GNOME/XWIN FOCUS BUG
|
|
331 e.getComponent().requestFocus();
|
|
332 }
|
|
333 });
|
|
334 */
|
|
335 //dragAndDrop = new CellPicture(0,0,0,0,null,0); // inicjujemy wydmuszke z image=null
|
|
336 timer.schedule(new KeyTicks(), 0, 1000); // lepiej 1000
|
|
337 //zaczynamy=true;
|
|
338 }
|
|
339 protected void paintComponent(Graphics g){
|
|
340 super.paintComponent(g);
|
|
341
|
|
342 //int i, j;
|
|
343 int fontSize = 60;
|
|
344
|
|
345 //y=60;
|
|
346
|
|
347 //System.out.println("MATA: "+mata);
|
|
348 //if (mata != null) {
|
|
349
|
|
350 g.setFont(new Font("Serif", Font.PLAIN, fontSize));
|
|
351 //g.setForeground(Color.BLACK);
|
|
352 g.setColor(Color.BLACK);
|
|
353 //g.drawImage(imageNow, 0,0,this);
|
|
354 if (startscreen){
|
|
355 g.drawImage(startScreen, 130+getInsets().left, 40+getInsets().top, null); // 160 + getInsets().left
|
|
356 }
|
|
357 if (go){
|
|
358 for (int y=0;y<6;y++){
|
|
359 for (int x=0; x<4;x++){
|
|
360 //int i = y*4+x;
|
|
361 // jesli na tapecie nie ma obrazka wyswietla szachownice
|
|
362 if (wallPaper[y][x] == null)
|
|
363 g.drawImage(wallPaperImage, x*wallPaperImage.getWidth(null)+getInsets().left, y*wallPaperImage.getHeight(null)+getInsets().top, null);
|
|
364 else
|
|
365 g.drawImage(wallPaper[y][x].image, x*wallPaper[y][x].w+getInsets().left, y*wallPaper[y][x].h+getInsets().top, null);
|
|
366 // wallPaper[y][x]=new CellWallPaper(wallPaperImage.getWidth(null)*x, wallPaperImage.getHeight(null)*y,wallPaperImage.getWidth(null), wallPaperImage.getHeight(null), wallPaperImage, i);
|
|
367
|
|
368 }
|
|
369 }
|
|
370
|
|
371
|
|
372 //if (go){
|
|
373 for (int y=0;y<6;y++){
|
|
374 for (int x=0; x<4;x++){
|
|
375 int i = y*4+x;
|
|
376 // g.drawImage(cell[y*4+x], 650+x*tw+getInsets().left, y*th+getInsets().top, null);
|
|
377 if(cellPicture[y][x]==null){
|
|
378 g.drawImage(wallPaperImage, 496+124+x*wallPaperImage.getWidth(null)+getInsets().left, y*wallPaperImage.getHeight(null)+getInsets().top, null);
|
|
379 System.out.println("ADRESS: "+cellPicture[y][x]);
|
|
380 } else
|
|
381 g.drawImage(cellPicture[y][x].image, 496+124+x*cellPicture[y][x].w+getInsets().left, y*cellPicture[y][x].h+getInsets().top, null);
|
|
382 //} else
|
|
383 // g.drawImage(wallPaper[y][x].image, 496+124+x*wallPaper[y][x].w+getInsets().left, y*wallPaper[y][x].h+getInsets().top, null);
|
|
384
|
|
385 }
|
|
386 }
|
|
387 if (dragAndDrop != null){
|
|
388 if (dragAndDrop.image != null){
|
|
389 System.out.println("JUZ JESTEM!");
|
|
390 System.out.println("X: "+dragAndDrop.x);
|
|
391 System.out.println("Y: "+dragAndDrop.y);
|
|
392
|
|
393 g.drawImage(dragAndDrop.image, dragAndDrop.x, dragAndDrop.y, null);
|
|
394
|
|
395 }
|
|
396 }
|
|
397 if (getBorderX !=0 | getBorderY != 0){
|
|
398 //System.out.println("RRYSUJE BORDER");
|
|
399 g.drawImage(borderImage, getBorderX*tw+getInsets().left, getBorderY*th+getInsets().top, null);
|
|
400
|
|
401
|
|
402 }
|
|
403 }
|
|
404
|
|
405 }
|
|
406 private void LoadBorder(){
|
|
407
|
|
408 try {
|
|
409 borderImage = ImageIO.read(getClass().getResourceAsStream("/border.png")); // "/nazwa_pliku"
|
|
410 } catch (IOException ioe) {
|
|
411 ioe.printStackTrace();
|
|
412 }
|
|
413
|
|
414 }
|
|
415 private BufferedImage LoadPicture(String name){
|
|
416 BufferedImage image=null;
|
|
417
|
|
418 try {
|
|
419 image = ImageIO.read(getClass().getResourceAsStream(name)); // "/nazwa_pliku"
|
|
420 } catch (IOException ioe) {
|
|
421 ioe.printStackTrace();
|
|
422 }
|
|
423 iw=image.getWidth(null);
|
|
424 ih=image.getHeight(null);
|
|
425 tw=iw/4;
|
|
426 th=ih/6;
|
|
427
|
|
428 return image;
|
|
429 }
|
|
430 private void LoadWallPaper() {
|
|
431
|
|
432 try {
|
|
433 wallPaperImage = ImageIO.read(getClass().getResourceAsStream("/wallpaper.jpg")); // "/nazwa_pliku"
|
|
434 } catch (IOException ioe) {
|
|
435 ioe.printStackTrace();
|
|
436 }
|
|
437 /*
|
|
438 for (int y=0;y<6;y++){
|
|
439 for (int x=0; x<4;x++){
|
|
440 int i = y*4+x;
|
|
441 wallPaper[y][x]=null;
|
|
442 // wallPaper[y][x]=new CellWallPaper(wallPaperImage.getWidth(null)*x, wallPaperImage.getHeight(null)*y,wallPaperImage.getWidth(null), wallPaperImage.getHeight(null), wallPaperImage, i);
|
|
443
|
|
444 }
|
|
445 }
|
|
446 */
|
|
447 }
|
|
448 private void LoadStartScreen() {
|
|
449
|
|
450 try {
|
|
451 startScreen = ImageIO.read(getClass().getResourceAsStream("/start.jpg"));
|
|
452 } catch (IOException ioe) {
|
|
453 ioe.printStackTrace();
|
|
454 }
|
|
455 }
|
|
456
|
|
457 private void ZerosWallPaper() {
|
|
458 for (int y=0;y<6;y++){
|
|
459 for (int x=0; x<4;x++){
|
|
460 int i = y*4+x;
|
|
461 wallPaper[y][x]=null;
|
|
462 // wallPaper[y][x]=new CellWallPaper(wallPaperImage.getWidth(null)*x, wallPaperImage.getHeight(null)*y,wallPaperImage.getWidth(null), wallPaperImage.getHeight(null), wallPaperImage, i);
|
|
463
|
|
464 }
|
|
465 }
|
|
466
|
|
467 }
|
|
468
|
|
469 private void CropImage (){
|
|
470
|
|
471 CropImageFilter f;
|
|
472 FilteredImageSource fis;
|
|
473
|
|
474 for (int y=0;y<6;y++){
|
|
475 for (int x=0; x<4;x++){
|
|
476 f=new CropImageFilter(tw*x, th*y, tw,th);
|
|
477 fis=new FilteredImageSource(imageNow.getSource(), f);
|
|
478 int i = y*4+x;
|
|
479 //cell[i]=createImage(fis);
|
|
480 cellPicture[y][x]=new CellPicture(tw*x, th*y, tw, th, createImage(fis), i);
|
|
481 origPicture[y][x]=new CellPicture(tw*x, th*y, tw, th, createImage(fis), i);
|
|
482
|
|
483 mouse.set(tw,th);
|
|
484 }
|
|
485 }
|
|
486 }
|
|
487 private void MixImage(){
|
|
488
|
|
489 for (int i=0; i<48; i++){
|
|
490 //int si = (int) (Math.random()*24);
|
|
491 int sy = (int) (Math.random()*6);
|
|
492 int sx = (int) (Math.random()*4);
|
|
493
|
|
494 //int di = (int) (Math.random()*24);
|
|
495 int dy = (int) (Math.random()*6);
|
|
496 int dx = (int) (Math.random()*4);
|
|
497
|
|
498 //Image tmp = cell[si];
|
|
499 //cell[si]=cell[di];
|
|
500 //cell[di]=tmp;
|
|
501
|
|
502 CellPicture tmp_c = cellPicture[sy][sx];
|
|
503 cellPicture[sy][sx]=cellPicture[dy][dx];
|
|
504 cellPicture[dy][dx]=tmp_c;
|
|
505
|
|
506 //tmp_c = origPicture[sy][sx];
|
|
507 //origPicture[sy][sx]=origPicture[dy][dx];
|
|
508 //origPicture[dy][dx]=tmp_c;
|
|
509
|
|
510 }
|
|
511 }
|
|
512
|
|
513
|
|
514 public static void main(final String[] args) {
|
|
515
|
|
516 javax.swing.SwingUtilities.invokeLater(new RunnableImplementation());
|
|
517 }
|
|
518 private static final class RunnableImplementation implements Runnable {
|
|
519 public void run(){
|
|
520 createAndShowGUI();
|
|
521 }
|
|
522 }
|
|
523
|
|
524
|
|
525 static private class CellPicture {
|
|
526 public int x, y, w, h, n;
|
|
527 public Image image;
|
|
528 //public boolean view;
|
|
529 public CellPicture(int x, int y, int w, int h, Image img, int n){
|
|
530 this.x=x;
|
|
531 this.y=y;
|
|
532 this.w=w;
|
|
533 this.h=h;
|
|
534 this.n=n; // numer porzadkowy
|
|
535 this.image=img; // biezacy obrazek
|
|
536 // this.view=true;
|
|
537 }
|
|
538 public void set(int _x, int _y){
|
|
539 this.x=_x;
|
|
540 this.y=_y;
|
|
541 }
|
|
542
|
|
543 }
|
|
544 /*
|
|
545 static private class CellWallPaper extends CellPicture {
|
|
546 public Image store; // kopia ukladanki
|
|
547
|
|
548 public CellWallPaper(int x, int y, int w, int h, Image img, int n){
|
|
549
|
|
550 super(x,y,w,h,img,n);
|
|
551 store = null;
|
|
552 }
|
|
553
|
|
554 }
|
|
555 */
|
|
556 static class KeyTicks extends TimerTask {
|
|
557 //int cellGetIndex, cellPutIndex; // indeksy z obszaru do obszaru
|
|
558 Coordinate cellGetCoor, cellPutCoor;
|
|
559 Coordinate dragGetCoor=null;
|
|
560 //static CellPicture dragAndDrop;
|
|
561 boolean redraw = false;
|
|
562 int tmpx=0, tmpy=0;
|
|
563 boolean fromLeft=false, fromRight=false;
|
|
564 //int arr[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
|
|
565
|
|
566 public void run() {
|
|
567
|
|
568 if (go){
|
|
569 cellGetCoor=mouse.getCell();
|
|
570 cellPutCoor=mouse.putCell();
|
|
571 int gety=getBorderY=(cellGetCoor.y-(th/2))/th; // plus korekta y od belki (th/2)
|
|
572 int getx=getBorderX=cellGetCoor.x/tw;
|
|
573 int test = cellGetCoor.x/tw-5;
|
|
574 int puty=(cellPutCoor.y-(th/2))/th;
|
|
575 int putx=cellPutCoor.x/tw;
|
|
576 //int tmpx=0,tmpy=0;
|
|
577 //System.out.println("KLIKNIETO NA x:"+((cellGetCoor.x/tw))+" y:"+(cellGetCoor.y/th)-20));
|
|
578 //System.out.println("KLIKNIETO NA x:"+(cellGetCoor.x/tw)+" y:"+gety+" test:"+test);
|
|
579 //trefle.repaint();
|
|
580 System.out.println("cellPutCoor.y: "+cellPutCoor.y+" cellPutCoor.x: "+cellPutCoor.x);
|
|
581 System.out.println("PATYK");
|
|
582 if (cellGetCoor.click && !redraw && cellGetCoor.y-(th/2)>44 && cellGetCoor.y<645 && cellGetCoor.x<1122){
|
|
583 System.out.println("przerysowuje -1 x:"+cellGetCoor.x+" y:"+cellGetCoor.y);
|
|
584
|
|
585 redraw=true;
|
|
586 //System.out.println("cellGetCoor:"+cellGetCoor+" gety:"+gety+" getx:"+getx+" wallPaper:"+wallPaper[gety][getx]);
|
|
587 // drag z lewej strony
|
|
588 if (getx<4 && cellGetCoor.x/tw<4 && cellGetCoor.y-(th/2)/th>=0 && wallPaper[gety][getx]!=null && wallPaper[gety][getx].image!=null){
|
|
589 //if (wallPaper[gety][getx]!=null)
|
|
590 dragAndDrop=new CellPicture(cellGetCoor.x, cellGetCoor.y, tw, th, wallPaper[gety][getx].image, wallPaper[gety][getx].n);
|
|
591 //else
|
|
592 // dragAndDrop=null;
|
|
593 System.out.println("Jestem na lewo");
|
|
594 tmpx=cellGetCoor.x/tw;
|
|
595 tmpy=(cellGetCoor.y-(th/2))/th;
|
|
596 fromLeft=true;
|
|
597 }
|
|
598 // drag z prawej strony
|
|
599 else if (cellGetCoor.x/tw>4 && cellGetCoor.y-(th/2)/th>=0 && cellPicture[gety][getx-5]!=null && cellPicture[gety][getx-5].image!=null){
|
|
600 // bylo z origPicture
|
|
601 //dragAndDrop=new CellPicture(cellGetCoor.x, cellGetCoor.y, tw, th, cellPicture[gety][getx-5].image, cellPicture[gety][getx-5].n);
|
|
602 dragAndDrop=new CellPicture(cellGetCoor.x, cellGetCoor.y, tw, th, cellPicture[gety][getx-5].image, cellPicture[gety][getx-5].n);
|
|
603 System.out.println("Jestem naprawo");
|
|
604 tmpx=cellGetCoor.x/tw-5;
|
|
605 tmpy=(cellGetCoor.y-(th/2))/th;
|
|
606 fromRight=true;
|
|
607 }
|
|
608 else {
|
|
609 System.out.println("Jestem ELSE");
|
|
610 redraw=false;
|
|
611 //return;
|
|
612 }
|
|
613 //cellPicture[gety][getx-5].view=false;
|
|
614
|
|
615 System.out.println("przerysowuje - 2 x:"+tmpx+" y:"+tmpy);
|
|
616
|
|
617
|
|
618 }
|
|
619 else if(!cellGetCoor.click && redraw) {
|
|
620 System.out.println("puszczono");
|
|
621 redraw=false;
|
|
622 if (dragAndDrop.image!=null){
|
|
623 if(dragGetCoor.x/tw<4){
|
|
624 // drag z prawej na lewo
|
|
625 if (fromRight){
|
|
626 if (wallPaper[(dragGetCoor.y-(th/2))/th][dragGetCoor.x/tw]==null) {
|
|
627 //wallPaper[(dragGetCoor.y-(th/2))/th][dragGetCoor.x/tw]=new CellPicture(dragAndDrop.image.getWidth(null)*x, dragAndDrop.image.getHeight(null)*y,dragAndDrop.image.getWidth(null), dragAndDrop.image.getHeight(null), dragAndDrop.image, dragAndDrop.n);
|
|
628 wallPaper[(dragGetCoor.y-(th/2))/th][dragGetCoor.x/tw]=new CellPicture(tw*x, th*y,tw, th, dragAndDrop.image, dragAndDrop.n);
|
|
629 System.out.println("Dokuje na lewo");
|
|
630
|
|
631 dragAndDrop.image=null;
|
|
632 //cellPicture[(cellGetCoor.y-(th/2)/th)][cellGetCoor.x/tw]=null;
|
|
633 System.out.println(" coor.y"+puty+" coor.x"+putx);
|
|
634 System.out.println(" tmpy"+tmpy+" tmpx"+tmpx);
|
|
635
|
|
636 //cellPicture[puty][putx]=null;
|
|
637 cellPicture[tmpy][tmpx]=null;
|
|
638 fromRight=false;
|
|
639 }
|
|
640 else {
|
|
641 dragAndDrop.image=null;
|
|
642 }
|
|
643 move++;
|
|
644 }
|
|
645 // drag z lewej na lewo
|
|
646 else if (fromLeft){
|
|
647 if (wallPaper[(dragGetCoor.y-(th/2))/th][dragGetCoor.x/tw]==null){
|
|
648 //System.out.println("CELL:"+dragAndDrop.image);
|
|
649
|
|
650 wallPaper[(dragGetCoor.y-(th/2))/th][dragGetCoor.x/tw]=new CellPicture(tw*x, th*y,tw, th, dragAndDrop.image, dragAndDrop.n);
|
|
651 System.out.println("Dokuje na lewo");
|
|
652
|
|
653 dragAndDrop.image=null;
|
|
654 //cellPicture[(cellGetCoor.y-(th/2)/th)][cellGetCoor.x/tw]=null;
|
|
655 System.out.println(" coor.y"+puty+" coor.x"+putx);
|
|
656 System.out.println(" tmpy"+tmpy+" tmpx"+tmpx);
|
|
657
|
|
658 //cellPicture[puty][putx]=null;
|
|
659 wallPaper[tmpy][tmpx]=null;
|
|
660 fromLeft=false;
|
|
661 }
|
|
662 else {
|
|
663 dragAndDrop.image=null;
|
|
664 }
|
|
665 move++;
|
|
666 }
|
|
667 }
|
|
668 // drag spowrotem na prawo z lewj
|
|
669 else if (dragGetCoor.x/tw>4){
|
|
670 if(fromLeft){
|
|
671 if (cellPicture[(dragGetCoor.y-(th/2))/th][dragGetCoor.x/tw-5]==null){
|
|
672 System.out.println("Dokuje z lewa na prawo");
|
|
673 cellPicture[(dragGetCoor.y-(th/2))/th][dragGetCoor.x/tw-5]=new CellPicture(tw*x, th*y,tw, th, dragAndDrop.image, dragAndDrop.n);
|
|
674 dragAndDrop.image=null;
|
|
675 wallPaper[tmpy][tmpx]=null;
|
|
676 fromLeft=false;
|
|
677 }
|
|
678 else {
|
|
679 dragAndDrop.image=null;
|
|
680 }
|
|
681 }
|
|
682 else if(fromRight){
|
|
683 if (cellPicture[(dragGetCoor.y-(th/2))/th][dragGetCoor.x/tw-5]==null){
|
|
684 System.out.println("Dokuje z prawa na prawo");
|
|
685 cellPicture[(dragGetCoor.y-(th/2))/th][dragGetCoor.x/tw-5]=new CellPicture(tw*x, th*y,tw, th, dragAndDrop.image, dragAndDrop.n);
|
|
686 dragAndDrop.image=null;
|
|
687 cellPicture[tmpy][tmpx]=null;
|
|
688 fromRight=false;
|
|
689 }
|
|
690 else {
|
|
691 dragAndDrop.image=null;
|
|
692 }
|
|
693
|
|
694 }
|
|
695 }
|
|
696 else {
|
|
697 dragAndDrop.image=null;
|
|
698 fromLeft=false;
|
|
699 fromRight=false;
|
|
700 }
|
|
701
|
|
702
|
|
703 }
|
|
704 }
|
|
705 else {
|
|
706 System.out.println("POZA ZAKRESEM");
|
|
707 }
|
|
708
|
|
709 //System.out.println("DRAGANDDROP:"+dragAndDrop);
|
|
710
|
|
711 dragGetCoor = mouseMotion.get();
|
|
712 //if(dragGetCoor.x!=0 && dragGetCoor.y!=0){
|
|
713 System.out.println("drag x: "+dragGetCoor.x+" drag y: "+dragGetCoor.y+" xputcoor:"+putx+" yputcoor:"+puty);
|
|
714 if (dragAndDrop!=null)
|
|
715 dragAndDrop.set(dragGetCoor.x, dragGetCoor.y);
|
|
716 //}
|
|
717 trefle.repaint();
|
|
718
|
|
719 if (arranged()){
|
|
720 String s1="Ulozyles wszystkie puzzle !\n",
|
|
721 s2="Wykonales "+move+" ruchow.\n";
|
|
722 JOptionPane.showMessageDialog(frame,
|
|
723 s1+s2,
|
|
724 "Podsumowanie",
|
|
725 JOptionPane.PLAIN_MESSAGE);
|
|
726 go=false;
|
|
727 }
|
|
728 System.out.println("PO ARRANGED");
|
|
729 }
|
|
730 }
|
|
731 private boolean arranged(){
|
|
732 int i=0, a=0;
|
|
733 for (int y=0;y<6;y++){
|
|
734 for (int x=0;x<4;x++){
|
|
735 if (wallPaper[y][x] != null){
|
|
736 if (wallPaper[y][x].n==origPicture[y][x].n){
|
|
737 a++;
|
|
738 }
|
|
739 }
|
|
740 i++;
|
|
741 }
|
|
742 }
|
|
743 System.out.println("A: "+a);
|
|
744 if(a==24) return true;
|
|
745 return false;
|
|
746 }
|
|
747
|
|
748 }
|
|
749
|
|
750 }
|