summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPrzemyslaw <prymula76@outlook.com>2024-04-01 09:01:24 +0200
committerPrzemyslaw <prymula76@outlook.com>2024-04-01 09:01:24 +0200
commit18e499686d1c9991b65dc79272038b291bc14c5d (patch)
tree00b9d71f9c609e5e6fe990ea937822c3768032c9 /src
Init
Diffstat (limited to 'src')
-rw-r--r--src/ColorsMemory.java310
-rw-r--r--src/Field.java41
2 files changed, 351 insertions, 0 deletions
diff --git a/src/ColorsMemory.java b/src/ColorsMemory.java
new file mode 100644
index 0000000..0e11fef
--- /dev/null
+++ b/src/ColorsMemory.java
@@ -0,0 +1,310 @@
+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 java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+import java.util.Timer;
+import java.util.*; //Timer
+import java.lang.*;
+
+public class ColorsMemory extends JPanel
+ implements ActionListener {
+ static JMenuBar menuBar;
+ JMenu menu, info;
+ JMenuItem menuItem, menuExit, menuInfo;
+ static final int NW=4, NH=4; // weight - height
+ static JFrame frame;
+ static Timer timer;
+ static Field [][]field = new Field[NH][NW];
+ static int []tmpi = new int[2];
+ static int []tmpj = new int[2];
+ static boolean showColor=false;
+ static boolean press=false;
+ static boolean lockClick=false;
+ static int nmove=0;
+ static int nFields; // ilosc pozostalych pol do odkrycia
+ static JLabel label = new JLabel("Witaj w Colors Memory!");
+
+ public ColorsMemory() {
+ super(new GridBagLayout());
+ GridBagLayout gridbag = (GridBagLayout)getLayout();
+ GridBagConstraints c = new GridBagConstraints();
+ int i;
+ int j;
+ // int x, y;
+ int k=0;
+
+ menuBar = new JMenuBar();
+ menu = new JMenu("Plik");
+ menuBar.add(menu);
+ // przenosi INFO na prawa strone
+ menuBar.add(Box.createHorizontalGlue());
+ info = new JMenu("Info");
+ info.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
+ menuBar.add(info);
+ menuItem = new JMenuItem("Nowa Gra", null);
+ menuItem.setActionCommand("NewGame");
+ menuItem.addActionListener(this);
+ menuItem.setToolTipText("Rozpoczyna nowa gre");
+ menu.add(menuItem);
+ menu.addSeparator();
+ menuItem = new JMenuItem("Zakoncz", null);
+ menuItem.setActionCommand("Exit");
+ menuItem.addActionListener(this);
+ menuItem.setToolTipText("Wyjscie z programu");
+ menu.add(menuItem);
+ menuItem = new JMenuItem("O Programie", null);
+ menuItem.setActionCommand("About");
+ menuItem.addActionListener(this);
+ menuItem.setToolTipText("Informacje o programie");
+ info.add(menuItem);
+
+ for (i=0;i<NH;i++) {
+ for (j = 0; j < NW; j++, k++) {
+ field[i][j] = new Field(i, j);
+ field[i][j].button.setActionCommand("colorButton"+k);
+ field[i][j].button.addActionListener(this);
+ }
+ }
+ init();
+
+ for (i=0;i<NH;i++) {
+ for (j=0;j<NW;j++,k++) {
+ field[i][j].button.setPreferredSize(new Dimension(60, 60));
+ c.fill = GridBagConstraints.HORIZONTAL;
+ c.gridx = j;
+ c.gridy = i;
+ gridbag.setConstraints(field[i][j].button, c);
+ field[i][j].button.setBackground(field[i][j].getColor());
+ //field[i][j].button.setForeground(field[i][j].getColorHide());
+ add(field[i][j].button);
+
+ //field[i][j].button.setActionCommand("colorButton"+k);
+ //field[i][j].button.addActionListener(this);
+ field[i][j].button.setFont(new Font("Arial", Font.PLAIN, 40));
+
+
+ }
+ }
+ label.setPreferredSize(new Dimension(100, 20));
+ c.fill = GridBagConstraints.HORIZONTAL;
+ c.gridwidth = 4;
+ c.gridx = 0;
+ c.gridy = 4;
+ gridbag.setConstraints(label, c);
+ add(label);
+ }
+ private void init(){
+ int i,j,x,y,k=0;
+ Random rand = new Random();
+ int cc=-1;
+ final int []r={64,96,128,0,178,196,255,0};
+ final int []g={64,0,0,32,16,128,255,128};
+ final int []b={0,45,128,128,64,32,0,255};
+
+ for (i=0;i<NH;i++) {
+ for (j = 0; j < NW; j++, k++) {
+ do {
+ y = rand.nextInt(NH);
+ x = rand.nextInt(NW);
+ //System.out.println("Rand y:"+y+" x:"+x);
+ } while (field[y][x].postReserve);
+ if (!field[y][x].postReserve) {
+ //field[y][x].reserve = true;
+
+ if (k % 2 == 0) cc++;
+ field[y][x].setColorHide(r[cc], g[cc], b[cc]);
+ field[y][x].postReserve = true;
+ //field[y][x].button.setForeground(field[y][x].getColorHide());
+
+ }
+ }
+ }
+ nFields=k;
+
+ }
+ public void actionPerformed(ActionEvent e){
+ int k=0;
+ if (!lockClick) {
+ for (int i = 0; i < NH; i++) {
+ for (int j = 0; j < NW; j++, k++) {
+ if (e.getActionCommand().equals("colorButton"+k)) {
+
+ // kliknieto pierwszy kafel
+ if (!press) {
+
+ if (!field[i][j].getFound()) {
+ //Toolkit.getDefaultToolkit().beep();
+ field[i][j].button.setBackground(field[i][j].getColorHide());
+ System.out.println("i: " + i + " j: " + j);
+ tmpi[0] = i;
+ tmpj[0] = j;
+ press = true;
+ nmove++;
+ }
+ // kliknieto drugi kafel
+ } else {
+ if ((!field[i][j].getFound()) && (tmpi[0]!=i || tmpj[0]!=j)) {
+ field[i][j].button.setBackground(field[i][j].getColorHide());
+ //System.out.println("i: " + i + " j: " + j + " found: " + field[i][j].getFound());
+ tmpi[1] = i;
+ tmpj[1] = j;
+
+ showColor = true;
+ press = false;
+ lockClick = true;
+
+ if (field[tmpi[0]][tmpj[0]].getColorHide().toString().equals(field[tmpi[1]][tmpj[1]].getColorHide().toString())) {
+
+ Toolkit.getDefaultToolkit().beep();
+ //System.out.println("K O L O R: "+field[tmpi[0]][tmpj[0]].getColorHide().toString());
+ label.setText(" Odnaleziono !");
+
+ field[tmpi[0]][tmpj[0]].setFound(true);
+ field[tmpi[1]][tmpj[1]].setFound(true);
+ }
+ else label.setText(" Pudlo !");
+ }
+ else {
+ System.out.println("Ten sam klocek");
+ // tutaj nic nie dopisujemy ! ponizsza linia jest zla!
+ //field[tmpi[0]][tmpj[0]].button.setBackground(field[tmpi[0]][tmpj[0]].getColor());
+ }
+ }
+ }
+ }
+ }
+ }
+ if(e.getActionCommand().equals("NewGame")) {
+ int i,j;
+
+ for (i=0;i<NH;i++) {
+ for (j = 0; j < NW; j++, k++) {
+
+ field[i][j].setFound(false);
+ //field[i][j].reserve = false;
+ field[i][j].postReserve = false;
+ field[i][j].button.setText("?");
+ }
+ }
+ init();
+ nmove=0;
+ }
+ else if(e.getActionCommand().equals("Exit")) {
+ System.exit(0);
+ }
+ else if(e.getActionCommand().equals("About")) {
+
+ JOptionPane.showMessageDialog(frame,
+ "Moja implementacja gry Memory\nPrzemyslaw R. Pietraszczyk\nStworzono w oparciu o OpenJDK\nSierpien 2019",
+ "Colors Memory 0.210503-0",
+ JOptionPane.PLAIN_MESSAGE);
+
+ }
+ /*
+ try {
+ mySleep();
+ } catch (InterruptedException ex) {
+ ex.printStackTrace();
+ }
+ */
+ }
+ /*
+ private static void mySleep() throws InterruptedException{
+ Thread.sleep(1000);
+ }
+ */
+
+ private static void createAndShowGUI() {
+ JFrame frame = new JFrame("Colors Memory");
+ timer = new Timer();
+ int frameWidth = 300;
+ int frameHeight = 400;
+ // ustawia okno po prawe na dole
+ Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
+ frame.setBounds((int) screenSize.getWidth()/2 - frameWidth/2,
+ (int) screenSize.getHeight() - frameHeight, frameWidth, frameHeight);
+ frame.setResizable(false);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ //Create and set up the content pane.
+ JComponent newContentPane = new ColorsMemory();
+ newContentPane.setOpaque(true); //content panes must be opaque
+ frame.setJMenuBar(menuBar);
+ frame.setContentPane(newContentPane);
+
+
+ //Display the window.
+ frame.pack();
+ frame.setVisible(true);
+
+ timer.schedule(new ClearField(), 2000, 3000);
+
+ }
+ public static void main(String[] args) {
+ //Schedule a job for the event-dispatching thread:
+ //creating and showing this application's GUI.
+ javax.swing.SwingUtilities.invokeLater(new Runnable() {
+ public void run() {
+ createAndShowGUI();
+ }
+ });
+ }
+
+ static class ClearField extends TimerTask {
+ public void run() {
+ if (nFields!=0) {
+ if (showColor) {
+ System.out.println("show color");
+ press = false;
+ field[tmpi[0]][tmpj[0]].button.setBackground(field[tmpi[0]][tmpj[0]].getColor());
+ field[tmpi[1]][tmpj[1]].button.setBackground(field[tmpi[1]][tmpj[1]].getColor());
+ showColor = false;
+ lockClick = false;
+ //nmove++;
+ //System.out.println("Color 0: "+field[tmpi[0]][tmpj[0]].getColorHide().toString()+" Color 1:"+field[tmpi[1]][tmpj[1]].getColorHide().toString());
+ // zapobiega zanikaniu pojedynczego kafla
+ if (tmpi[0] != tmpi[1] || tmpj[0] != tmpj[1]) {
+ //label.setText(" Odnaleziono !");
+ // jesli te same kolory
+ if (field[tmpi[0]][tmpj[0]].getColorHide().toString().equals(field[tmpi[1]][tmpj[1]].getColorHide().toString())) {
+
+ //field[tmpi[0]][tmpj[0]].setFound(true);
+ //field[tmpi[1]][tmpj[1]].setFound(true);
+ field[tmpi[0]][tmpj[0]].button.setText(" ");
+ field[tmpi[1]][tmpj[1]].button.setText(" ");
+ //Toolkit.getDefaultToolkit().beep();
+
+ label.setText("");
+ nFields -= 2;
+ System.out.println("nFields: " + nFields);
+ } else label.setText("");
+ }
+
+
+ }
+ }
+ else {
+ JOptionPane.showMessageDialog(frame,
+ "Wykonales "+nmove+" posuniec",
+ "Podsumowanie",
+ JOptionPane.PLAIN_MESSAGE);
+ nFields=1; // aby nie wyswietlalo drugi raz
+
+ }
+
+
+ }
+
+ }
+}
+//}
diff --git a/src/Field.java b/src/Field.java
new file mode 100644
index 0000000..578fe34
--- /dev/null
+++ b/src/Field.java
@@ -0,0 +1,41 @@
+import javax.swing.*;
+import java.awt.*;
+
+
+public class Field {
+ JButton button;
+ private Color colorHide, color;
+ private int x,y;
+ boolean found;
+ //boolean reserve;
+ boolean postReserve;
+ //Color []cArray = {BLUE,CYAN,GRAY,GREEN,MAGENTA,ORANGE,PINK,RED,WHITE,YELLOW};
+ public Field () {
+ postReserve=false;
+ }
+ public Field(int i, int j){
+ button = new JButton("?");
+ color=new Color(255,255,255);
+ y = i;
+ x = j;
+ found=false;
+ // reserve=false;
+ postReserve=false;
+
+ }
+ public Color getColorHide(){
+ return colorHide;
+ }
+ public Color getColor() {
+ return color;
+ }
+ public void setColorHide(int r, int g, int b){
+ colorHide=new Color(r,g,b);
+ }
+ public void setFound(boolean f){
+ found=f;
+ }
+ public boolean getFound(){
+ return found;
+ }
+}