Modern Programming Techniques
This lab has you playing a bit with JavaFX to familiarize you with the basic structure of an application, as well as
some of the various components covered in the lectures.
There are two portions to the lab: a required application, and a series of other optional applications that you may find interesting/challenging to
implement — feel free to do as many or few of the latter as you wish.
Submitting this Lab
The only exercise you should submit is the required one (next section). As this lab is not being placed into Codelab, please send me an
email of your source as well as several screen captures similar to what I present below.
The Required Application — A Simple Calculator
Create an application with three text fields and four buttons:
- Two of the text fields represent operands to an expression to be calculated; the third will display the result
and should not be editable.
- Add labels to indicate the text fields (see above illustrations).
- The four buttons should represent the four basic arithmetic operations
- Feel free to use any (or no) layout that you want
- Feel free to structure your handler(s) in any way you see fit — I used a single anonymous handler that then
getSource to determine which button was pressed
- The text fields need to be converted to integers (and the result converted back again) — I found it simpler to
do this once in a single handler rather than duplicating the conversion in separate handlers.
- You can, of course, use a single inner class for the handler, or go external — the choice is yours.
The Optional Applications
The following a various applications you may find interesting/challenging/'fun' to implement — the main purpose is to
further familiarize you with JavaFX programming.
- Create an application with two stages:
- the first one is the primary stage passed into
start by the JavaFX application framework
- create the second yourself (within
- Draw the Olympic logo of the five colored rings
- Make the lines thicker (look at stroke width)
- Make the rings interlock (hint: arcs are the way to go)
- Draw a smiley face
- Use circles/ellipses for the eyes and mouth (as well as the face)
- Look at the
Arc class (it's also a
Shape subclass) for the mouth
- Add a smile/frown button
- Display 10 smiley faces
- Have them all listen to the smile/frown button
- Have two buttons control some attribute of a shape
- Make a line longer/shorter
- Increase/decrease the radius of a circle
- Draw a clock face with a second hand, and a button
- On each click of the button have the second hand move 90 degrees
- Have the second hand circle the clock face once per 60 clicks
04-TextFields: add a 'Clear' button to the text field application that clears out the contents of the two text fields
- Look up the
TextArea class in the API. Place a text area labelled
document, and a
text field labelled
search for on the applet. Have a button labelled
Search, which when
clicked looks for the first occurrence of the
search for field in the
document area. The
resulting position/text (or an indication that it wasn't found)
should be displayed in a non-editable text field. Test your application by placing some lengthy text into the
area and then enter various searches in the
search for field.
- Code an applet with 10 buttons positioned like a phone keypad —
- wire them up so that when pressed they display the proper value (0-9 *, #) on the console.
- Add logic to simulate dialing a phone with the following behavior:
- Pressing '#' two times in a row indicates a number is about to be dialed
- Dialed numbers must be 10 digitis preceded by a '1'. Anything else causes the message
"Please dial a number in the proper format" to be displayed on the console.
- After the 10 digit number has been dialed, a message such as
"Phone number (718) 951-5000
has been dialed." should be displayed on the console.
- Once a number has been dialed pressing
* twice terminates the call, and sends the
"End of call to the console.
- During a call, entering anything but '*' results in the warning message
"Please don't touch
keypad during call" to the console.
- Put a text field above the keypad and use it instead of the console
- Create a virtual screen keyboard (like you see on tablets or smartphones. There should be a (single-line) text field
that displays the text being typed. Some ideas:
Full disclosure: I (deliberately) haven't implemented this one myself, so I'm not too sure how hard some of the
- Use an array for the
Button objects (you may have already thought of this for your phone applet.
- Mimic the actual layout of a QWERTY keyboard-- i.e., the keys of successive rows are horizontally offset.
- Shift key / Caps lock key (status light?)
- Backspace key
- Code an application that displays a calculator keyboard
- Try to get the 'wide' '0' key
- Create a rubber banding effect
- when the user drags (holds down the left button and moves) the mouse,
draw four lines (four different colors) to the current mouse position-- as the mouse moves, the lines move as well
(shrinking and stretching as their endpoints move with the new mouse position-- hence the term 'rubber-banding').
- While dragging, use dashed or dotted lines (to sho the lines are not permanent).
- While dragging (and showing the dotted lines), show the original lines as solid (maybe lighter?)
- Display a rectangle on the screen and allow the user to drag and drop the rectangle to another location