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:22] frchris2048_gui [2025/07/15 10:46] (current) – [Using Eclipse] frchris
Line 7: Line 7:
 ''public int[][] getBoard()'' returns the gameBoard. ''public int[][] getBoard()'' returns the gameBoard.
 ''public int max()'' returns the maximum element of the gameBoard.     ''public int max()'' returns the maximum element of the gameBoard.    
 +
 +===== 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 =====
 +
 +Here is an example that implements the ''KeyListener'' class so the user can simply use the arrow keys to make a move.  
 +
 +
 +Modify the ''paintComponent'' method to change the fonts, colors, size, and appearance of the game.
  
 <code GUI2048.java> <code GUI2048.java>
Line 16: Line 25:
 import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener; import java.awt.event.KeyListener;
- 
- 
 import javax.swing.JFrame; import javax.swing.JFrame;
 import javax.swing.JPanel; 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 public class GUI2048 extends JPanel implements KeyListener
Line 33: Line 52:
  String message;  String message;
  private Game2048 myGame;  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()  public GUI2048()
Line 50: Line 61:
  boardSize = board.length;  boardSize = board.length;
  message = "Use arrow keys (or l, r, u, d for left, right, up, down)";  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);  titleFont = new Font("Roman", Font.BOLD, 18);
  regularFont = new Font("Helvetica", Font.PLAIN, 12);  regularFont = new Font("Helvetica", Font.PLAIN, 12);
Line 63: Line 73:
  window.getContentPane().add(app);  window.getContentPane().add(app);
  window.addKeyListener(app);  window.addKeyListener(app);
- //window.pack(); 
  window.setVisible(true);  window.setVisible(true);
  
Line 82: Line 91:
  g.setFont(regularFont);  g.setFont(regularFont);
  g.drawString("Version 1.0", 20, 50);  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.setFont(new Font("Arial", Font.PLAIN, 48));
  g.setColor(Color.red);  g.setColor(Color.red);
- //g.drawString("\""+c+"\"",80, 120); 
  drawBoard(g);  drawBoard(g);
  
  
  }  }
- // update is a workaround to cure Windows screen flicker problem +
- public void update(Graphics g){ +
- paint(g); +
- }+
  public void drawBoard(Graphics g) {  public void drawBoard(Graphics g) {
  int size = HEIGHT/(boardSize +1);  int size = HEIGHT/(boardSize +1);
Line 171: Line 175:
  
 </code> </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. 
 +
 +
 +==== 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.1752589368.txt.gz · Last modified: 2025/07/15 10:22 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki