a simple java applet program

A Simple Reactor GUI

In this app there are 3 classes:

  1. Reactor Class: which is the class for the reactor behaviour.
  2. ReactorGUI Class: This is the class that we create the GUI and also create an object from Reactor Class.
  3. ReactorGUITester: This is a simple class to create and execure the ReactorGUI Class.

The Reactor Class:


public class Reactor {
//Attributes
private int temperature;
private static int MAX = 10;
private static int MIN = -10;

//Methods

public Reactor () {
temperature = 0;
}

public int getTemperature() {
return temperature;
}

public boolean increaseTemperature(boolean isIncreased) {
if(isIncreased && temperature < MAX) {
temperature ++;
return true;
}
else {
return false;
}

}

public boolean decreaseTemperature(boolean isDecreased) {
if(isDecreased && temperature > MIN) {
temperature --;
return true;
}
else {
return false;
}

}

}

The ReactorGUI Class:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ReactorGUI extends JFrame implements ActionListener {
//Attributes
private JButton increaseButton = new JButton("Increase");
private JButton decreaseButton = new JButton("Decrease");
private JButton displayTemButton = new JButton("Display Temperature");
private JButton helpButton = new JButton("Help");
private JLabel reactorStatusLabel = new JLabel("Reactor Status", JLabel.CENTER);
private JLabel alarmLabel = new JLabel("", JLabel.CENTER);
private Reactor myReactor = new Reactor();

//Methods
public ReactorGUI() {
setTitle("Reactor GUI");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(450,150);
setLocation(200,200);
add(increaseButton);
add(decreaseButton);
add(displayTemButton);
add(helpButton);
add(reactorStatusLabel);
add(alarmLabel);
increaseButton.addActionListener(this);
decreaseButton.addActionListener(this);
displayTemButton.addActionListener(this);
helpButton.addActionListener(this);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if(e.getSource() == increaseButton) {
if(myReactor.increaseTemperature(true)) {
reactorStatusLabel.setText("The current temperature of the reactor has been increased by 1");
} else {
reactorStatusLabel.setText("Ooops, Something wrong! ");
}
}
if(e.getSource() == decreaseButton) {
if(myReactor.decreaseTemperature(true)) {
reactorStatusLabel.setText("The current temperature of the reactor has been decreased by 1");
} else {
reactorStatusLabel.setText("Ooops, Something wrong! ");
}
}
if(e.getSource() == displayTemButton) {
reactorStatusLabel.setText("The current temperature of the reactor is " + "" " + myReactor.getTemperature() + " "");
}
if(e.getSource() == helpButton) {
reactorStatusLabel.setText("The Max Tem is 10 and Min Tem is -10");
}
if(myReactor.getTemperature() >= 10 || myReactor.getTemperature() <= -10) {
alarmLabel.setText("***ALARM***");
} else {
alarmLabel.setText("");
}
}

}

The ReactorGUITester Class:


public class ReactorGUITester {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ReactorGUI();

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *