User Tools

Site Tools


2048_gui

This is an old revision of the document!


2048 GUI

The AP Classroom 2048 is Text based, so here is an example of a GUI that you can use with your 2048 Lab solution.

Additional Methods For the Game2048 class

public int getScore() returns the score. public int[][] getBoard() returns the gameBoard. public int max() returns the maximum element of the gameBoard.

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.

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, 
			           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;
	/**
	 * A GUI runner for your solution to the 2025 Lab
	 *  Unit 4: 2048
	 *  
	 *  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 GUI2048()
	{
 
		myGame = new Game2048();
		board = myGame.getBoard();
		boardSize = board.length;
		message = "Use arrow keys (or l, r, u, d for left, right, up, down)";
		//initialize GUI variables here...
		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.pack();
		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.drawString("the key char "+c+" has code "+keyCode, 20, 60);
		g.setFont(new Font("Arial", Font.PLAIN, 48));
		g.setColor(Color.red);
		//g.drawString("\""+c+"\"",80, 120);
		drawBoard(g);
 
 
	}
	// update is a workaround to cure Windows screen flicker problem
	public void update(Graphics g){
		paint(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) {}
 
}
2048_gui.1752589368.txt.gz · Last modified: 2025/07/15 10:22 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki