Mercurial > hg > pub > prymula > com
view numpuzzle/src/numpuzzle/NumPuzzle.java @ 1:84a8cf4ae913
immposiblerush-0.230919-0.1
author | prymula <prymula76@outlook.com> |
---|---|
date | Tue, 26 Sep 2023 21:47:56 +0200 |
parents | 2787f5e749ae |
children |
line wrap: on
line source
/* NumPuzzle - proste puzzle napisane w javie 17-12-2020 by: PRP */ package numpuzzle; import java.awt.*; // Toolkit import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JButton; //import javax.swing.JComponent; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.Box; import javax.swing.JTextArea; import javax.swing.JDialog; import javax.swing.JTextField; import javax.swing.JCheckBox; import javax.swing.BorderFactory; import javax.swing.WindowConstants; import java.awt.event.*; import java.awt.event.ActionListener; //import java.awt.event.KeyListener; import java.awt.event.ActionEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowAdapter; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.event.ItemEvent; //import java.awt.event.KeyEvent; import java.util.*; //Timer //import java.awt.Robot; import java.awt.Toolkit; import java.beans.*; //property change stuff import java.io.BufferedReader; import java.io.InputStream; import java.io.OutputStream; import java.io.InputStreamReader; import java.io.IOException; public class NumPuzzle extends JPanel implements ActionListener { static JMenuBar menuBar; static JMenu menu, info, menu2, nowplay; static JMenuItem menuItem, menuExit, menuInfo, menuPref; static JFrame frame; static boolean go=false; //static JPanel screen; static NumPuzzle nPuzzle; static Actions actions; static Mata mata; private Graphics bg; private Image bufor; private int x; private int y; //private Mata mata; //private int currentFace = Font.FACE_SYSTEM; private Puste puste; static boolean zaczynamy; static int nmove; public NumPuzzle (){ menuBar = new JMenuBar(); menu = new JMenu("Plik"); menuBar.add(menu); //menu2 = new JMenu("Opcje"); //menuBar.add(menu2); // przenosi INFO na prawa strone menuBar.add(Box.createHorizontalGlue()); info = new JMenu("Info"); // kierunek wyswietlania menu info.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); menuBar.add(info); nowplay = new JMenu("Nowa Gra"); nowplay.setToolTipText("Rozpoczyna nową gre."); menu.add(nowplay); menuItem = new JMenuItem("Ku przodowi", null); menuItem.setActionCommand("NewGame1"); menuItem.addActionListener(this); menuItem.setToolTipText("Miesza do przodu"); nowplay.add(menuItem); menuItem = new JMenuItem("Na lewo", null); menuItem.setActionCommand("NewGame2"); menuItem.addActionListener(this); menuItem.setToolTipText("Miesza na lewo"); nowplay.add(menuItem); menuItem = new JMenuItem("Pojedynczy", null); menuItem.setActionCommand("NewGame0"); menuItem.addActionListener(this); menuItem.setToolTipText("Własciwie nic nie miesza"); nowplay.add(menuItem); //menu.add(menuItem); menu.addSeparator(); menuExit = new JMenuItem("Zakoncz", null); menuExit.setActionCommand("EndGame"); menuExit.addActionListener(this); menuExit.setToolTipText("Wyjscie z programu"); menu.add(menuExit); menuInfo = new JMenuItem("Pomoc", null); menuInfo.setActionCommand("Help"); menuInfo.addActionListener(this); menuInfo.setToolTipText("Samouczek"); info.add(menuInfo); menuInfo = new JMenuItem("O Programie", null); menuInfo.setActionCommand("About"); menuInfo.addActionListener(this); menuInfo.setToolTipText("Informacje o programie"); info.add(menuInfo); } public void actionPerformed(final ActionEvent e) { // Nowa gra if(e.getActionCommand().equals("NewGame0")) { nmove=0; zaczynamy=true; mata.setMixedFields(0); System.out.println("ZACZNAMY"); //screen.setVisible(true); nPuzzle.repaint(); } else if(e.getActionCommand().equals("NewGame1")) { nmove=0; zaczynamy=true; mata.setMixedFields(1); System.out.println("ZACZNAMY"); //screen.setVisible(true); nPuzzle.repaint(); } else if(e.getActionCommand().equals("NewGame2")) { nmove=0; zaczynamy=true; mata.setMixedFields(2); System.out.println("ZACZNAMY"); //screen.setVisible(true); nPuzzle.repaint(); } // Zakonczenie else if(e.getActionCommand().equals("EndGame")) { System.exit(0); } else if(e.getActionCommand().equals("About")) { String s1=" Port mojej gry pierwotnie napisanej w J2ME w 2011 r.\n\n", s2=" Program napisany na podstawie fragmentow kodow,\n", s3=" wyszukanych w wyszukiwarce Google,\n", s4=" plus wlasna inwencja\n\n", s5=" Przemyslaw R. Pietraszczyk - grudzien 2020\n", s6=" Licencja: GPL\n\n", s7=" Stworzono w oparciu o OpenJDK 11 oraz ANT\n\n", s8=" o/s: "+System.getProperty("os.name")+" ver: "+System.getProperty("os.version")+"\n", s9=" java ver: "+System.getProperty("java.vm.version")+"\n"; JOptionPane.showMessageDialog(frame, s1+s2+s3+s4+s5+s6+s7+s8+s9, "Num Puzzle - 20201221", JOptionPane.PLAIN_MESSAGE); } else if(e.getActionCommand().equals("Help")) { String s1="Celem gry jest posortowanie ukladanki.\n", s2="Nalezy ulozyc ja w kolejnosci od najmniejszej,\n", s3="do najwiekszej liczby.\n", s4="Dokladnie w taki sposob jak na poczatkowym\n", s5="ekranie. Gra sie za pomoca klawiatury.\n", s6="\n"; JOptionPane.showMessageDialog(frame, s1+s2+s3+s4+s5+s6, "Pomoc", JOptionPane.PLAIN_MESSAGE); } } private static void createAndShowGUI() { frame=new JFrame("Num Puzzle"); mata = new Mata(); mata.setUnmixedFields(); // menuBar z konstruktora final int frameWidth = 190; final int frameHeight = 240; final int GNOMEPANEL=80; // nieco niżej niż panel final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //frame.setBounds((int) screenSize.getWidth() - frameWidth, // (int) screenSize.getHeight() - frameHeight, frameWidth, frameHeight); frame.setBounds((int) screenSize.getWidth() - frameWidth, GNOMEPANEL, frameWidth, frameHeight); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); nPuzzle= new NumPuzzle(); //screen = new PuzzleCanvas(true, mata); frame.add(nPuzzle); nPuzzle.setBounds(5,5,180,230); //frame.add(screen); //nPuzzle.setVisible(true); frame.add(menuBar); frame.setJMenuBar(menuBar); frame.setLayout(null); //frame.setSize(250,400); // 400 x 400 frame.setVisible(true); actions = new Actions(); frame.addKeyListener(actions); frame.setFocusable(true); // zapobiega utracie KeyEvent po wyjściu z SoundDialog i ResetDialog /* frame.addFocusListener(new FocusListener(){ public void focusGained(FocusEvent e){ //System.out.println("Focus GAINED:"+e); } public void focusLost(FocusEvent e){ //System.out.println("Focus LOST:"+e); // FIX FOR GNOME/XWIN FOCUS BUG e.getComponent().requestFocus(); } }); */ //timer.schedule(new KeyTicks(), 0, 1000); // lepiej 1000 //zaczynamy=true; } protected void paintComponent(Graphics g){ super.paintComponent(g); int i, j; int fontSize = 60; y=60; System.out.println("MATA: "+mata); if (mata != null) { g.setFont(new Font("Serif", Font.PLAIN, fontSize)); //g.setForeground(Color.BLACK); g.setColor(Color.BLACK); for (i=0; i<3;i++, y+=fontSize){ for (j=0, x= 10;j<3;j++, x+=fontSize) { if (zaczynamy) { if (mata.naMacie[i][j].numerporzadkowy != 9) g.drawString(Integer.toString(mata.naMacie[i][j].numerporzadkowy), x, y); else g.drawString(" ", x, y); } else { int ty; if (mata.naMacie[i][j].numerporzadkowy != 9) g.drawString(Integer.toString(mata.pole[i][j].numerporzadkowy), x, y); else g.drawString(" ", x, y); } System.out.print("wartosc: "+mata.naMacie[i][j].wartosc); } } } } public static void main(final String[] args) { javax.swing.SwingUtilities.invokeLater(new RunnableImplementation()); } private static final class RunnableImplementation implements Runnable { public void run(){ createAndShowGUI(); } } static class Actions implements KeyListener { Puste puste; public String pressKey= ""; public void Actions(){ } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent event) { } @Override public void keyPressed(KeyEvent event) { puste=null; System.out.println("event:"+event); if (zaczynamy) { if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('1')) { puste=czyObokWolne(Integer.parseInt("1")); } else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('2')) { puste=czyObokWolne(Integer.parseInt("2")); } else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('3')) { puste=czyObokWolne(Integer.parseInt("3")); } else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('4')) { puste=czyObokWolne(Integer.parseInt("4")); } else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('5')) { puste=czyObokWolne(Integer.parseInt("5")); } else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('6')) { puste=czyObokWolne(Integer.parseInt("6")); } else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('7')) { puste=czyObokWolne(Integer.parseInt("7")); } else if (event.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('8')) { puste=czyObokWolne(Integer.parseInt("8")); } if (puste != null) zamienMiejscami(); } } public Puste czyObokWolne(int k) { int i, j=0; boolean n=false; Puste puste=null; System.out.println("MATA z czyObokWolne: "+mata); for (i=0; i<3;i++) { // y for (j=0; j<3; j++) { // x //if (k == mata.naMacie[j][i].znak) { if (k == mata.naMacie[i][j].numerporzadkowy) { n=true; break; } } if (n) { System.out.println(" i: " + i + " j: " +j + " K: "+k); break; } } if (n) { if (j>0 && mata.naMacie[i][j-1].wartosc==9) puste = new Puste(i, j, i, j-1); else if (j<2 && mata.naMacie[i][j+1].wartosc==9) puste = new Puste(i, j, i, j+1); else if (i<2 && mata.naMacie[i+1][j].wartosc==9) puste = new Puste(i, j, i+1, j); else if (i>0 && mata.naMacie[i-1][j].wartosc==9) puste = new Puste(i, j, i-1, j); } puste.show(); return puste; } public void zamienMiejscami() { Mata[][] mtmp; Pole p; Puste ptmp; System.out.println("cel na y:" +puste.dy + " x:" + puste.dx); System.out.println("zrodlo na i:" +puste.sy + " j:" + puste.sx); p = mata.naMacie[puste.dy][puste.dx]; mata.naMacie[puste.dy][puste.dx] = mata.naMacie[puste.sy][puste.sx]; mata.naMacie[puste.sy][puste.sx] = p; nmove++; nPuzzle.repaint(); if (whetherArranged()) { System.out.println("ULOZONO !"); zaczynamy=false; String s1="Wykonales(as) "+nmove+" posuniec"; JOptionPane.showMessageDialog(frame, s1, "Podsumowanie", JOptionPane.PLAIN_MESSAGE); } } /* metoda zwraca prawde gdy macierz jest poukładana */ public boolean whetherArranged() { final int[] num = {1,2,3,4,5,6,7,8,9}; int n=0, count=0; boolean tidy=false; for(int y=0; y<3; y++){ for (int x=0; x<3; x++,n++){ if (mata.naMacie[y][x].numerporzadkowy==num[n]){ System.out.println("Ulozone: "+num[n]+" count:"+count); count++; } } } System.out.println("ulozono(count): "+count); if (count==9) tidy=true; return tidy; } } } /* static class KeyTicks extends TimerTask { public void run() {} } */ class Puste { // najpierw stare wspolrzedne pozniej nowe int sx, sy, dx, dy; // source - destination public Puste(int i,int j, int k, int m) { this.sy = i; this.sx = j; this.dy = k; this.dx = m; } public void show (){ System.out.println("stare y: "+sy+"stare x: "+sx+"nowy y: "+dy+"nowy x:"+dx); } }