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:22] – frchris | 2048_gui [2025/07/15 10:46] (current) – [Using Eclipse] frchris | ||
|---|---|---|---|
| Line 7: | Line 7: | ||
| '' | '' | ||
| '' | '' | ||
| + | |||
| + | ===== 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 ===== | ||
| + | |||
| + | Here is an example that implements the '' | ||
| + | |||
| + | |||
| + | Modify the '' | ||
| <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; | ||
| + | /** | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | */ | ||
| 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. | + | |
| - | * you can write your own draw(Graphics g) method, and call it | + | |
| - | * from this class' | + | |
| - | */ | + | |
| 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)"; | ||
| - | // | ||
| titleFont = new Font(" | titleFont = new Font(" | ||
| regularFont = new Font(" | regularFont = new Font(" | ||
| Line 63: | Line 73: | ||
| window.getContentPane().add(app); | window.getContentPane().add(app); | ||
| window.addKeyListener(app); | window.addKeyListener(app); | ||
| - | // | ||
| window.setVisible(true); | window.setVisible(true); | ||
| Line 82: | Line 91: | ||
| g.setFont(regularFont); | g.setFont(regularFont); | ||
| g.drawString(" | g.drawString(" | ||
| - | // | ||
| g.setFont(new Font(" | g.setFont(new Font(" | ||
| g.setColor(Color.red); | g.setColor(Color.red); | ||
| - | // | ||
| 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/ | int size = HEIGHT/ | ||
| Line 171: | Line 175: | ||
| </ | </ | ||
| + | ===== Making an executable jar file ===== | ||
| + | |||
| + | Any computer with the [[https:// | ||
| + | |||
| + | |||
| + | ==== Using Eclipse ==== | ||
| + | - From the '' | ||
| + | - In the '' | ||
| + | - Press the '' | ||
| + | - Under the '' | ||
| + | - Under '' | ||
| + | - Select the '' | ||
| + | - Press '' | ||
2048_gui.1752589368.txt.gz · Last modified: 2025/07/15 10:22 by frchris