@Zeune/

JAVA GUI

Java

No description

fork
loading
Files
  • Main.java
  • 2048GUI_13_10.java
  • With Colors.java
  • WithGeneratefirst.java
  • WithTileColors.java
Main.java
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
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
// 37-left, 38-up, 39-right, 40-down;
 */
package game;
import java.util.*;
//import java.io.*;
import java.awt.*;        // Using AWT layouts
import java.awt.event.*;  // Using AWT event classes and listener interfaces
import java.text.DecimalFormat;
import javax.swing.*;     // Using Swing components and containers
import javax.swing.border.BevelBorder;

public class Game extends JFrame implements KeyListener{
    private final JFrame mainFrame;
    private final JLabel headerLabel;
    private final JLabel statusLabel;
    private final JPanel controlPanel;
    private final JLabel[] box;
    int board[][] = new int[4][4];
    int posX,posY;
    String lastMove = "  ";
    Random rand = new Random();
    public void printBoard(int board[][]){
        int temp[] = new int[16];
        int k=0;
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
              temp[k++] = board[i][j];
            }
        }
        for(int i=0;i<16;i++){
            box[i].setText(""+temp[i]);
        }
        mainFrame.setVisible(true);
    }
    public int[] changeLine(int list[]){
        int count = 0;
        int i;
        int j=0;
        //To know no of Zero's in row
        for(i=0;i<list.length;i++){
            if(list[i]==0){
              count++;
            }
        }
        if(count==4){
            return list;
        }
        int sublist[] = new int[list.length-count];
        //Collecting NonZero numbers
        for(i=0;i<list.length;i++){
            if(list[i]!=0){
                sublist[j++] = list[i];
            }
        }
        //Updating Line according
        if(count==3){
            list[0] = sublist[0];
            list[1]=0;
            list[2]=0;
            list[3]=0;
            return list;
        }
        for(i=0;i<sublist.length-1;i++){
            if(sublist[i]==sublist[i+1]){
                sublist[i] = sublist[i]+sublist[i+1];
                if(i==sublist.length-2){
                    sublist[i+1] = 0;
                }
                else{
                  if(i==sublist.length-1){
                    sublist[i+1] = 0;
                  }
                  else{
                    sublist[i+1] = sublist[i+2];
                  }
                  for(j=i+2;j<sublist.length-1;j++){
                    sublist[j] = sublist[j+1];
                  }
                  sublist[sublist.length-1] = 0;
                }
            }
        }
        for(i=0;i<sublist.length;i++){
            list[i] = sublist[i];
        }
        for(i=sublist.length;i<list.length;i++){
            list[i] = 0;
        }
        return list;
    }
    public int[][] updateBoard(int board[][],int dir){
        if(dir==0){
            for(int i=0;i<4;i++){
                int list[] = {board[0][i],board[1][i],board[2][i],board[3][i]};
                list = changeLine(list);
                for(int j=0;j<4;j++){
                    board[j][i] = list[j];
                }
            }
        }
        else if(dir==1){
            for(int i=0;i<4;i++){
                int list[] = {board[i][3],board[i][2],board[i][1],board[i][0]};
                list = changeLine(list);
                for(int j=3,k=0;j>=0;j--,k++){
                    board[i][j] = list[k];
                }
            }
        }
        else if(dir==2){
            for(int i=0;i<4;i++){
                int list[] = {board[3][i],board[2][i],board[1][i],board[0][i]};
                list = changeLine(list);
                for(int j=3,k=0;j>=0;j--,k++){
                    board[j][i] = list[k];
                }
            }
        }
        else if(dir==3){
            for(int i=0;i<4;i++){
                int list[] = {board[i][0],board[i][1],board[i][2],board[i][3]};
                list = changeLine(list);
                for(int j=0;j<4;j++){
                    board[i][j] = list[j];
                }
            }
        }
        return board;
    }
    public String generate(){
        posX = rand.nextInt(4);
        posY = rand.nextInt(4);
        Map<String, Integer> pos = new TreeMap<>();
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
              pos.put(Integer.toString(i)+Integer.toString(j),0);
            }
        }
        while(board[posX][posY]!=0){
            pos.put(Integer.toString(posX)+Integer.toString(posY),1);
            if(!(pos.containsValue(0))){
                return "Over";
            }
            posX = rand.nextInt(4);
            posY = rand.nextInt(4);
        }
        board[posX][posY] = ((rand.nextInt(2))*2)+2;
        printBoard(board);
        return "";
    }
    public void gameOver(String s){
        if(s=="Over"){
            statusLabel.setText("Last Move:"+lastMove+"        Game Over");
            mainFrame.setVisible(true);
            mainFrame.removeKeyListener(this);
        }
    }
    public void setColors(){
        Map<String, String> colors = new TreeMap<>();
        colors.put("0","#C6BCBA");
        colors.put("2","0xFFFFFF");
        colors.put("4","0xFFEEEE");
        colors.put("8","0xFFDDDD");
        colors.put("16","0xFFCCCC");
        colors.put("32","0xFFBBBB");
        colors.put("64","0xFFAAAA");
        colors.put("128","0xFF9999");
        colors.put("256","0xFF8888");
        colors.put("512","0xFF7777");
        colors.put("1024","0xFF6666");
        colors.put("2048","0xFF5555");
        colors.put("4096","0xFF4444");
        colors.put("8192","0xFF3333");
        colors.put("16384","0xFF2222");
        colors.put("32768","0xFF1111");
        for(int i=0;i<16;i++){
            box[i].setOpaque(true);
            box[i].setBackground(Color.decode(colors.get(box[i].getText())));
            //box[i].setForeground(Color.decode(colors.get(box[i].getText())));
        }
    }
    public Game(){
        box = new JLabel[16];
        mainFrame = new JFrame("2048");
        mainFrame.setSize(500,600);
        mainFrame.setLayout(new GridLayout(3, 1));
        headerLabel = new JLabel("",JLabel.CENTER );
        statusLabel = new JLabel("Thanks for playing",JLabel.CENTER);        
        statusLabel.setSize(500,40);
        mainFrame.addWindowListener(new WindowAdapter() {
           @Override
           public void windowClosing(WindowEvent windowEvent){
              System.exit(0);
           }        
        });
        mainFrame.addKeyListener(this);
        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());
        mainFrame.add(headerLabel);
        mainFrame.add(controlPanel);
        mainFrame.add(statusLabel);      
        headerLabel.setText("Welcome to 2048");        
        headerLabel.setSize(500,50);
        JPanel panel = new JPanel();
        //panel.setBackground(Color.lightGray);
        panel.setSize(500,400);
        GridLayout layout = new GridLayout(4,4);
        panel.setLayout(layout);
        for(int i=0;i<16;i++){
            box[i] = new JLabel();
            box[i].setText("0");
            box[i].setHorizontalAlignment(SwingConstants.CENTER);
            box[i].setVerticalAlignment(SwingConstants.CENTER);
            box[i].setPreferredSize(new Dimension(50, 40));
            box[i].setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            box[i].setFont(box[i].getFont().deriveFont(20f));
            box[i].setForeground(Color.BLUE);
            panel.add(box[i]);
        }
        controlPanel.add(panel);
        gameOver(generate());
        setColors();
        printBoard(board);
    }
    // The entry main() method
    public static void main(String[] args) {        
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
               new Game();  // Let the constructor do the job
            }
        });
    } 

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        String direction = "";
        switch(code){
            case 37:
                direction = "Left";
                board = updateBoard(board,3);
                break;
            case 38:
                direction = "Up";
                board = updateBoard(board,0);
                break;
            case 39:
                direction = "Right";
                board = updateBoard(board,1);
                break;
            case 40:
                direction = "Down";
                board = updateBoard(board,2);
                break;
        }
        if(direction==""){
            statusLabel.setText("Please use Arrow keys to play");
        }
        else{
            statusLabel.setText("Last Move:"+lastMove+"        Current Move:"+direction);
        }
        lastMove = direction;
        gameOver(generate());        
        setColors();
        printBoard(board);
        mainFrame.setVisible(true);
    }
    @Override public void keyTyped(KeyEvent e) {}
    @Override public void keyReleased(KeyEvent e) {}
}