CPU Monitor FXML
Description:
Create a Java 8 SE application in NetBeans using JavaFX FXML and an associated controller that implements a CPU monitor having an analog display, a digital display, and a record board. The analog display is comprised of a dial with tick marks and a sweeping hand that displays real time CPU usage. The digital display shows real time CPU usage as a percentage in decimal format from 0.00% to 100.00% depending on the system’s activity during the recent period being observed.
The CPU monitor has two buttons. When the monitor is first initialized the two buttons will be “Record” and “Start”. When the monitor is running the two buttons will be “Record” and “Stop”. When the monitor is stopped, or paused, the two buttons will be “Reset” and “Start”. The text on the two buttons should be displayed dynamically and change dynamically.
The record board will be used to record the CPU usage when the user specifies. When the “Record” button is clicked, the number on the digital display will be recorded and showed on the record board. The record board consist of three placeholders, the newest record will always replace the oldest one. If the monitor has not started yet, then the record button should not have any action.
Purpose:
This challenge develops skills in creating and manipulating JavaFX interfaces, using FXML and Scene Builder, using a controller associated with an FXML interface, learning and implementing the Model-View-Controller architecture, generating and handling events, and organizing code.
Requirements:
- Project Name: <Pawprint>CPUMonitorFXML
- For the project name, follow the same naming scheme used in the first challenge. The
project name is to be comprised of your pawprint, with the first letter capitalized, followed by CPUMonitorFXML. For example, if the pawprint is abcxyz9, the project is to be named Abcxyz9CPUMonitorFXML.
- This challenge allows you to apply your creativity while meeting a set of requirements. Not every aspect of the application is described in the requirements. The requirements establish what functionality must exist, but it is up to you to apply your creativity in implementing the functionality and the user interface to support that functionality.
- Platform: Java 8 SE, JavaFX, FXML with Controller
- Project IDE: NetBeans
1
CPU Monitor FXML
Create a CPU monitor application that contains an analog, a digital display, and a record board. The analog display contains a circular gauge with 10 tick marks where each tick mark represents 10% of CPU usage and a sweeping hand that will change between ticks based off the current CPU load. See Figure 1: Analog Interface for an example. The digital display, minimally, shows the CPU usage as a percentage to the nearest 100th decimal place in a –.–% format (“Minimally” means you can do more if you’d like, which is usually rewarded). The record board will be used to display the percentage load of three different records. See Figure 2: Record Board for an example.
Figure 1: Analog Interface Example Figure 2: Record Board Example
The monitor has two buttons. When the monitor is initialized the two buttons will be “Record” and “Start”. When the monitor is in motion and running, the two buttons will be “Record” and “Stop”. If the user presses the “Stop” button and the monitor is no longer in motion or running, then the two buttons will be “Reset” and “Start”. If the user presses the reset button, the two buttons will return to “Record” and “Start”. Therefore, the buttons will change dynamically depending on the current state of the monitor.
The start button will start the monitor. The stop button will halt the monitor. The reset button resets the displays, CPU load, record count, places the displays in a halt state, and clears the record board. The record button will take the current CPU usage displayed at the moment in time the button was pressed and record the percentage on the board in the same format as the digital display –.–%, along with the current time and record number.
2
CPU Monitor FXML
Starting the monitor causes it to get the real time CPU usage from the whole system based on a 0-100% value depending on whether the system is idle or active during the recent period being observed. If the monitor is at 0 and the start is pressed, the monitor starts from 0. If the monitor is stopped at 15.00% and then the start button is pressed, the monitor will start from 15.00%. The reset button is used to set the monitor to 0 and place it in a halted state. If the current CPU load usage is 25% and the record button is pressed for the first time, the current CPU usage of 25% will be displayed to the record board, while keep track of which record it is and the current system time. However, if the monitor has not started yet, meaning the start button has not been pressed for the first time, then the record button has no action and nothing will happen if pressed.
The record board consist of three placeholders. When the “Record” button is clicked for the first time, the percentage on the digital display will be recorded on the first placeholder with a record number of 1 and the current time displayed. If the “Record” button is clicked for the second and third time, the procedure repeats, by having the percentage on the digital display recorded in the second and third placeholder with a record number of 2 and 3, respectively, and the current time displayed. However, if the “Record” button is clicked for the fourth time, the first label will be changed, and the original process repeated. Therefore, the newest record will always replace the oldest one. After the “Reset” button is clicked, the record board will be initialized back to “–.–%” for all placeholders, the record count will be set back to zero, and the record board should look the same as it was in the beginning. Below are some example images of record board:
3
At the Beginning
After Second Click
After Fourth Click
After First Click
After Third Click
After Reset Button
CPU Monitor FXML
The code you can use to get the real time CPU usage in a double format return value:
private static double getCPUUsage() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean(); double value = 0;
for(Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
method.setAccessible(true);
if (method.getName().startsWith(“getSystemCpuLoad”) && Modifier.isPublic(method.getModifiers())) { try {
value = (double) method.invoke(operatingSystemMXBean); } catch (Exception e) {
value = 0; }
return value; }
}
return value; }
4
CPU Monitor FXML
double getSystemCpuLoad()returns the “recent cpu usage” for the whole system. This value is a double in the [0.0, 1.0] interval. A value of 0.0 means that all CPUs were idle during the recent period of time observed, while a value of 1.0 means that all CPUs were actively running 100% of the time during the recent period being observed. All values between 0.0 and 1.0 are possible depending of the activities going on in the system. If the system’s recent CPU usage is not available, the method returns a negative value.
Two image files are provided with this challenge that you can use: gauge.png and hand.png. You are not required to use the provided images. If you want to use alternatives, that is okay. The gauge.png image is the dial of the monitor. The hand.png image is the sweeping hand of the gauge.
Gauge.png Hand.png
How the user interface layout is designed is up to you. You get to decide the locations of the analog display, digital display, record board, and the two dynamically changing buttons. Whatever you choose should be a well-organized, thoughtful, aesthetically pleasing, and a useable interface or an interface that logically makes sense. The layout should look like you made intentional choices and are in control of their placement. This means the layout should not be a disorganized mess that is a result of not knowing how to implement the user interface, layout, and/or code in a meaningful way.
You may expand the functionality beyond the basic requirements provided above if you choose. Usually expanding beyond the basic requirements gets rewarded. You could do the following for example:
● Add additional functionality to the monitor or additional buttons ● Change the colors of the buttons
5
CPU Monitor FXML
○ For example, when the “Start” button is showing the button’s color could be green and when the “Stop” button is showing the button’s color could be red
- ● Add more information
○ Get the available memory
○ Get the current memory used
○ Which processes/programs are running
○ For each process, how much CPU usage is it taking ○ Get the CPU time for each process
○ Etc. - ● Add additional record board labels
- ● Make the clock more precise for each record
- ● Add additional sweeping hands to the analog display gauge
Run your application and make sure everything works as expected. Export the project directory to a ZIP using NetBeans and submit it on Canvas.
Things to submit on Canvas:
- The zip file created after you export your project.
- You may also submit screenshots of your application running for proof (optional). Put all your
screenshots in a folder, name the folder “<Pawprint>Screenshots” where you replace <pawprint> with your pawprint, and zip them, even if you only take one. Then submit the zip of screenshots on canvas.
Note: You are only allowed to submit one thing at a time on canvas. You cannot submit a zip file and your screenshots. Therefore, first submit the screenshots, then click “re-submit”, and submit your zip file. On your end, it will look like you only submitted the zip file, however, on our end, we will see both.
Extra Comments:
- Make sure you are pulling the values quick enough from the getCPUUsage() method so your Analog and Digital display look smooth and not “jumpy” or “jerky”.
- Make sure you separate your code into separate classes. There should be at least one controller and one FXML file. However, I recommend you start separating your code into a model class so you can get used to MVC and because it will help you with the next challenge, even if you don’t get MVC 100% correct. If you do MVC correctly, then bonus points will be rewarded.
6