2048_gui
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| 2048_gui [2025/07/15 10:31] – frchris | 2048_gui [2025/07/15 10:46] (current) – [Using Eclipse] frchris | ||
|---|---|---|---|
| Line 19: | Line 19: | ||
| Modify the '' | Modify the '' | ||
| - | + | <code GUI2048.java> | |
| + | import java.awt.Color; | ||
| + | import java.awt.Font; | ||
| + | import java.awt.Graphics; | ||
| + | import java.awt.event.KeyEvent; | ||
| + | import java.awt.event.KeyListener; | ||
| + | import javax.swing.JFrame; | ||
| + | import javax.swing.JPanel; | ||
| + | /** | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | */ | ||
| + | |||
| + | public class GUI2048 extends JPanel implements KeyListener | ||
| + | { | ||
| + | |||
| + | private static int WIDTH = 800, HEIGHT = 800; | ||
| + | private static int LEFT_ARROW = 37, UP_ARROW = 38, | ||
| + | | ||
| + | private Font titleFont, regularFont; | ||
| + | private int keyCode, boardSize; | ||
| + | private int[][] board; | ||
| + | private char c; | ||
| + | String message; | ||
| + | private Game2048 myGame; | ||
| + | |||
| + | |||
| + | public GUI2048() | ||
| + | { | ||
| + | |||
| + | myGame = new Game2048(); | ||
| + | board = myGame.getBoard(); | ||
| + | boardSize = board.length; | ||
| + | message = "Use arrow keys (or l, r, u, d for left, right, up, down)"; | ||
| + | titleFont = new Font(" | ||
| + | regularFont = new Font(" | ||
| + | keyCode=0; | ||
| + | c=' | ||
| + | } | ||
| + | public static void main(String[] args) { | ||
| + | GUI2048 app= new GUI2048(); | ||
| + | JFrame window = new JFrame(" | ||
| + | window.setSize(WIDTH, | ||
| + | window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | window.getContentPane().add(app); | ||
| + | window.addKeyListener(app); | ||
| + | window.setVisible(true); | ||
| + | |||
| + | } | ||
| + | public void paintComponent(Graphics g){ | ||
| + | super.paintComponent(g); | ||
| + | g.setColor(Color.WHITE); | ||
| + | g.fillRect(0, | ||
| + | g.setColor(Color.BLUE); | ||
| + | |||
| + | g.setFont(new Font(" | ||
| + | g.drawString(" | ||
| + | g.setFont(titleFont); | ||
| + | g.setColor(Color.BLACK); | ||
| + | g.drawString(" | ||
| + | g.drawString( message , 20, WIDTH - 50); | ||
| + | |||
| + | g.setFont(regularFont); | ||
| + | g.drawString(" | ||
| + | g.setFont(new Font(" | ||
| + | g.setColor(Color.red); | ||
| + | drawBoard(g); | ||
| + | |||
| + | |||
| + | } | ||
| + | |||
| + | public void drawBoard(Graphics g) { | ||
| + | int size = HEIGHT/ | ||
| + | int top = 60; | ||
| + | int left = WIDTH/2 - 2 * size; | ||
| + | g.setFont(new Font(" | ||
| + | for(int r = 0; r < boardSize; r++) | ||
| + | for(int c = 0; c < boardSize; c++) | ||
| + | { | ||
| + | if (board[r][c] != 0) | ||
| + | g.drawString ("" | ||
| + | | ||
| + | } | ||
| + | g.setColor(Color.BLACK); | ||
| + | for(int r = 0; r <= boardSize; r++) { | ||
| + | g.drawLine(left, | ||
| + | } | ||
| + | |||
| + | for(int c = 0; c <= boardSize; c++){ | ||
| + | g.drawLine(left+c*size, | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // These 3 methods need to be declared to implement the KeyListener Interface | ||
| + | @Override | ||
| + | public void keyTyped(KeyEvent e) {} | ||
| + | |||
| + | @Override | ||
| + | public void keyPressed(KeyEvent e) | ||
| + | { | ||
| + | keyCode=e.getKeyCode(); | ||
| + | c=e.getKeyChar(); | ||
| + | if (keyCode == LEFT_ARROW || c == ' | ||
| + | { | ||
| + | myGame.moveLeft(); | ||
| + | myGame.mergeLeft(); | ||
| + | myGame.moveLeft(); | ||
| + | message = " | ||
| + | } | ||
| + | else if (keyCode == RIGHT_ARROW || c == ' | ||
| + | { | ||
| + | myGame.moveRight(); | ||
| + | myGame.mergeRight(); | ||
| + | myGame.moveRight(); | ||
| + | message = " | ||
| + | } | ||
| + | else if (keyCode == UP_ARROW || c == ' | ||
| + | { | ||
| + | myGame.moveUp(); | ||
| + | myGame.mergeUp(); | ||
| + | myGame.moveUp(); | ||
| + | message = " | ||
| + | } | ||
| + | else if (keyCode == DOWN_ARROW || c == ' | ||
| + | { | ||
| + | myGame.moveDown(); | ||
| + | myGame.mergeDown(); | ||
| + | myGame.moveDown(); | ||
| + | message = " | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | message = c+ " is an invalid choice!"; | ||
| + | |||
| + | } | ||
| + | if (message.length() < 6 && !myGame.gameOver()) | ||
| + | myGame.add2ToBoard(); | ||
| + | if (myGame.gameOver()) | ||
| + | message = "Game Over - You reached "+ myGame.max(); | ||
| + | repaint(); | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void keyReleased(KeyEvent e) {} | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| ===== Making an executable jar file ===== | ===== Making an executable jar file ===== | ||
| Line 25: | Line 180: | ||
| + | ==== Using Eclipse ==== | ||
| + | - From the '' | ||
| + | - In the '' | ||
| + | - Press the '' | ||
| + | - Under the '' | ||
| + | - Under '' | ||
| + | - Select the '' | ||
| + | - Press '' | ||
2048_gui.1752589895.txt.gz · Last modified: 2025/07/15 10:31 by frchris