User Tools

Site Tools


2048_gui

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
2048_gui [2025/07/15 10:31] – old revision restored (2025/07/15 10:21) frchris2048_gui [2025/07/15 10:46] (current) – [Using Eclipse] frchris
Line 8: Line 8:
 ''public int max()'' returns the maximum element of the gameBoard.     ''public int max()'' returns the maximum element of the gameBoard.    
  
-==== Implementing Scoring ====+===== Implementing Scoring =====
  
 For the score to function the usual way, you need to modify each of the four ''merge'' methods in the ''Game2048'' class. Whenever two cells combine, add this number to the score.  For example, if two 8's are combined, 16 should be added to the score. For the score to function the usual way, you need to modify each of the four ''merge'' methods in the ''Game2048'' class. Whenever two cells combine, add this number to the score.  For example, if two 8's are combined, 16 should be added to the score.
  
-==== A GUI Example ====+===== A GUI Example =====
  
 Here is an example that implements the ''KeyListener'' class so the user can simply use the arrow keys to make a move.   Here is an example that implements the ''KeyListener'' class so the user can simply use the arrow keys to make a move.  
Line 19: Line 19:
 Modify the ''paintComponent'' method to change the fonts, colors, size, and appearance of the game. Modify the ''paintComponent'' method to change the fonts, colors, size, and appearance of the game.
  
-  +<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; 
 +/** 
 +  A GUI runner for your solution to the 2025 Lab 
 + *  Unit 4: 2048 
 +   
 +  @author Chris Thiel, OFMCap 
 +  @version July 15, 2025 
 +   
 +  Make sure to write a getScore() method in your Game2048 class 
 +  as well as a getGameBoard() method.  Alternatively, 
 +  you can write your own draw(Graphics g) method, and call it 
 +  from this class's paintComponent method. 
 + */ 
 + 
 +public class GUI2048 extends JPanel implements KeyListener 
 +
 + 
 + private static int WIDTH = 800, HEIGHT = 800; 
 + private static int LEFT_ARROW = 37, UP_ARROW = 38,  
 +            RIGHT_ARROW = 39, DOWN_ARROW = 40; 
 + 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("Roman", Font.BOLD, 18); 
 + regularFont = new Font("Helvetica", Font.PLAIN, 12); 
 + keyCode=0; 
 + c='-'; 
 +
 + public static void main(String[] args) { 
 + GUI2048 app= new GUI2048(); 
 + JFrame window = new JFrame("2048 Game"); 
 + window.setSize(WIDTH, HEIGHT); 
 + 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, 0, getWidth(),getHeight()); 
 + g.setColor(Color.BLUE); 
 +  
 + g.setFont(new Font("Arial", Font.PLAIN, 48)); 
 + g.drawString("2048 Game", WIDTH/3, 40); 
 + g.setFont(titleFont); 
 + g.setColor(Color.BLACK); 
 + g.drawString("Score: " + myGame.getScore(), 4*WIDTH/5, 40); 
 + g.drawString( message , 20, WIDTH - 50); 
 + 
 + g.setFont(regularFont); 
 + g.drawString("Version 1.0", 20, 50); 
 + g.setFont(new Font("Arial", Font.PLAIN, 48)); 
 + g.setColor(Color.red); 
 + drawBoard(g); 
 + 
 + 
 +
 +  
 + public void drawBoard(Graphics g) { 
 + int size = HEIGHT/(boardSize +1); 
 + int top = 60; 
 + int left = WIDTH/2 - 2 * size; 
 + g.setFont(new Font("Arial", Font.BOLD, 48)); 
 + for(int r = 0; r < boardSize; r++) 
 + for(int c = 0; c < boardSize; c++) 
 +
 + if (board[r][c] != 0) 
 + g.drawString (""+board[r][c], left+c*size + size/3,  
 +                       top + r*size + 3*size/4); 
 +
 + g.setColor(Color.BLACK); 
 + for(int r = 0; r <= boardSize; r++) { 
 + g.drawLine(left, top + size*r, left + boardSize*size, top +size*r); 
 +
 + 
 + for(int c = 0; c <= boardSize; c++){ 
 + g.drawLine(left+c*size, top ,left+c*size, top + boardSize* 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 == 'l'
 +
 + myGame.moveLeft(); 
 + myGame.mergeLeft(); 
 + myGame.moveLeft(); 
 + message = "left"; 
 + }  
 + else if (keyCode == RIGHT_ARROW || c == 'r'
 +
 + myGame.moveRight(); 
 + myGame.mergeRight(); 
 + myGame.moveRight(); 
 + message = "right"; 
 + }  
 + else if (keyCode == UP_ARROW || c == 'u'
 +
 + myGame.moveUp(); 
 + myGame.mergeUp(); 
 + myGame.moveUp(); 
 + message = "up"; 
 + }  
 + else if (keyCode == DOWN_ARROW || c == 'd' )  
 +
 + myGame.moveDown(); 
 + myGame.mergeDown(); 
 + myGame.moveDown(); 
 + message = "down"; 
 + }  
 + 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) {} 
 + 
 +
 + 
 +</code> 
 +===== Making an executable jar file =====
  
 Any computer with the [[https://www.java.com/en/download/manual.jsp|Java runtime ]] installed can play your version of the 2048 Game.  Any computer with the [[https://www.java.com/en/download/manual.jsp|Java runtime ]] installed can play your version of the 2048 Game. 
  
  
 +==== Using Eclipse ====
  
 +  - From the ''File'' menu, select Export
 +  - In the ''Java'' folder, select Runnable JAR file
 +  - Press the ''Next'' Button
 +  - Under the ''Launch Configuration'' your project that runs the GUI2048's ''main'' method
 +  - Under ''Library handling'' select ''Extract required libraries into JAR''
 +  - Select the ''Export destination'' (so you file the java executable later!)
 +  - Press ''Finish'' button (even if you're American)
2048_gui.1752589866.txt.gz · Last modified: 2025/07/15 10:31 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki