CPSC 329 Software Development Fall 2026

CPSC 329 Lab 8: JavaFX II

This lab provides practice with additional elements of JavaFX relevant for project P2: drag and drop, MVC, picking, multiple screens. All of these elements are covered in the slides from class and in the posted Klondike code; all but multiple screens were discussed in class.

Objectives

Successful completion of this lab means that you:

Collaboration and AI

Labs are for learning and practice — you may help and be helped by others when it comes to figuring out what to do, but in the end you need to understand and be able to do the things for yourself.

This lab can be done individually or in pairs. If you work together, use pair programming rather than splitting things up and working in parallel.

You may not use AI to figure out what to do or to do the task for you.

Due Date, Deliverables, and Handin

due Fri 10/30 at the start of class

To hand in your work:


Preliminaries

JavaFX Reference

This lab handout itself and the slides and Klondike example from class (posted on the schedule page) should be your primary reference for these exercises.

For additional reference, consult the sources identified in the JavaFX reference provided in lab 7.

Provided Code

You have been provided with a peg solitaire game in which the model and most of the view are complete. You will fill in the parts that connect them, marked with TODO comments. The files:

This program has the same structure as the Klondike example from class — match up these classes with those as you look at the slides.

In addition to the Java code, you have been provided with background images for two board layouts (board-english.png and board-european.png). Each image is 448×448 pixels, for a board of 7×7 squares of 64x64 pixels.

Setup

This lab requires JavaFX so you will need to have done the Eclipse setup from lab 6. If you haven't done that, or if you run into compilation and/or runtime errors working on this lab, go back to lab 6 for those instructions.

Create an Eclipse project and share it to a local Git repository:


Peg Solitaire

Peg solitaire is played on a board of holes, all but one of them filled with pegs. A move jumps a peg over a neighboring peg (up, down, left, or right) into an empty hole, and the peg jumped over is removed. The game ends when no jump is possible; the goal is to leave as few pegs as you can. Two board layouts are provided: the English board (33 holes, a cross) and the European board (37 holes, the cross plus one more hole in each inside corner).

Work through the parts below in order. Places where you need to add code are marked with TODO comments in the provided code, labeled with the specific steps (e.g. 0a, 0b).

0. Setup and Switching Screens

When the program starts, it shows a start screen where the user picks a board layout. Pressing Start should replace the start screen with the game screen, in the same window. In this part you'll fill in the start screen's controls and write the method that builds and connects a game's model, view, and controller.

1. The Layered Board

The board is designed as a StackPane with two layers: the background image at the bottom and a GridPane of cells on top. Each hole gets a cell: a StackPane exactly one square of the board in size. Each cell contains a slot (a Rectangle) which allows for decoration (e.g. highlighting a cell as a peg is dragged over it) and a peg if the hole has one. The cells let the image show through, line up exactly with the holes, and are where pegs are drawn and dropped. In this part you'll build the cells.

2. MVC Control Flow

When the user does something, the view passes the user's intention to the controller, the controller asks the model to make the move, and the model notifies its listener (the view), which redraws. If the move is refused (illegal move), nothing changes and the model doesn't notify anyone; the controller tells the view the reason instead. In this part you'll write the controller's method and the view's listener method. Since dragging pegs doesn't work yet, the game screen has a temporary Debug menu with a Make a Legal Move item for testing.

3. Drag and Drop

Now the user will be able to move pegs by dragging them. A drag and drop takes handlers at both ends. On the peg, one handler starts the drag when the peg is dragged, carrying where the peg came from as a string (e.g. "3,1"), and another cleans up when the drag ends. On the cells, one handler says the cell accepts the drop, and another handles the drop. You'll write all four. The drop handler doesn't decide whether the move is legal; instead, it passes the user's intention to the controller, just like Klondike's drop handler calls playWasteTo.

4. Picking

JavaFX gives a mouse or drag event to the topmost node under the pointer. That's usually what you want, but a decoration drawn on top of the board, even a translucent one, gets the events instead of the cells underneath it. In this part you'll add such an overlay, see what goes wrong, and fix it. The drop zone hint in the Klondike example is this kind of overlay.

5. Switching Screens, Again

Finally, the user needs a way back to the start screen: the Game → New Game menu item at any time and a Play Again button when the game is over. Both go through the same NavigationController as Start, this time calling newGame(), which asks Main to showStart(). Pressing Start again then builds a fresh game, view, and controller.

If You Have Time

This part is optional. If you have time, add one or more of the following: (see the slides from class for hints/examples of these for Klondike)