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] – old revision restored (2025/07/15 10:21) frchris | 2048_gui [2025/07/15 10:46] (current) – [Using Eclipse] frchris | ||
---|---|---|---|
Line 8: | Line 8: | ||
'' | '' | ||
- | ==== Implementing Scoring ==== | + | ===== Implementing Scoring |
For the score to function the usual way, you need to modify each of the four '' | For the score to function the usual way, you need to modify each of the four '' | ||
- | ==== A GUI Example ==== | + | ===== A GUI Example |
Here is an example that implements the '' | Here is an example that implements the '' | ||
Line 19: | Line 19: | ||
Modify the '' | Modify the '' | ||
- | + | <code GUI2048.java> | |
- | ==== Making an executable jar file ==== | + | 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; | ||
+ | /** | ||
+ | | ||
+ | * Unit 4: 2048 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | */ | ||
+ | |||
+ | 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 ===== | ||
Any computer with the [[https:// | Any computer with the [[https:// | ||
+ | ==== Using Eclipse ==== | ||
+ | - From the '' | ||
+ | - In the '' | ||
+ | - Press the '' | ||
+ | - Under the '' | ||
+ | - Under '' | ||
+ | - Select the '' | ||
+ | - Press '' |
2048_gui.1752589866.txt.gz · Last modified: 2025/07/15 10:31 by frchris