View
40
Download
0
Category
Preview:
DESCRIPTION
A simple swing example. GETTING STARTED WITH WIND CHILL. Windchill. Windchill There are several formulas for calculating the windchill temperature t wc The one provided by U.S. National Weather Service and is applicable for a windspeed greater than four miles per hour Where - PowerPoint PPT Presentation
Citation preview
A simple swing exampleA simple swing example
GETTING STARTED WITH WIND GETTING STARTED WITH WIND CHILLCHILL
WindchillWindchill
WindchillWindchill• There are several formulas forThere are several formulas for
calculating the windchill temperature calculating the windchill temperature ttwcwc
• The one provided by U.S. NationalThe one provided by U.S. NationalWeather Service and is applicable for a windspeed Weather Service and is applicable for a windspeed greater than four miles per hour greater than four miles per hour
• WhereWhere Variable Variable tt is the Fahrenheit temperature is the Fahrenheit temperature Variable Variable vv is the windspeed in miles per hour is the windspeed in miles per hour
0.081( 91.4)(3.71 5.81 0.25 ) 91.4 wct t v v
Console-based programmingConsole-based programming
Console program
Method main() { statement1; statement2; ... statementm;}
Console programsbegin and end inmethod main()
Console-based interactionConsole-based interaction
% What is the temperature (in Farenheit)?What is the temperature (in Farenheit)?
3434% What is the wind speed (in mph)?What is the wind speed (in mph)?
6767% The wind chill temperature is The wind chill temperature is
11
Graphical InterfaceGraphical Interface
In useIn use
Thereneeds to
be anevent loop
that islooking for
userinterface
events
Program needs to respond whenever the run button is clicked
GUI Program
main() { GUI gui = new GUI();}
GUI Constructor() { constructor1; constructor2; ... constructorn;}
Action Performer() { action1; action2; ... actionk;}
Constructor configuresthe components of theGUI . I t also registers
the listener-performerfor user interactions
The action performer implements the task of the GUI . After itcompletes, the event-dispatching loop is restarted
GUI program begins in method main(). The method createsa new instance of the GUI by invoking the GUI constructor.On completion, the event dispatching loop is begun
Event-dispatching loop
do if an event occurs then signal its action listenersuntil program ends
The event-dispatching loop watches for userinteractions with the GUI . When an event
occurs, its listener-performers are notified
GUI Program
main() { GUI gui = new GUI();}
GUI Constructor() { constructor1; constructor2; ... constructorn;}
Action Performer() { action1; action2; ... actionk;}
GUI program begins in method main(). The method createsa new instance of the GUI by invoking the GUI constructor.On completion, the event dispatching loop is begunGUI Program
main() { GUI gui = new GUI();}
GUI Constructor() { constructor1; constructor2; ... constructorn;}
Action Performer() { action1; action2; ... actionk;}
GUI program begins in method main(). The method createsa new instance of the GUI by invoking the GUI constructor.On completion, the event dispatching loop is begun
Event-dispatching loop
do if an event occurs then signal its action listenersuntil program ends
The event-dispatching loop watches for userinteractions with the GUI . When an event
occurs, its listener-performers are notified
GUI Program
main() { GUI gui = new GUI();}
GUI Constructor() { constructor1; constructor2; ... constructorn;}
Action Performer() { action1; action2; ... actionk;}
Constructor configuresthe components of theGUI . I t also registers
the listener-performerfor user interactions
GUI program begins in method main(). The method createsa new instance of the GUI by invoking the GUI constructor.On completion, the event dispatching loop is begun
Event-dispatching loop
do if an event occurs then signal its action listenersuntil program ends
The event-dispatching loop watches for userinteractions with the GUI . When an event
occurs, its listener-performers are notified
GUI-based programmingGUI-based programming
Java supportJava support
JFrameJFrame• Represents a titled, bordered windowRepresents a titled, bordered window
JLabelJLabel• Represents a display area suitable for one or both of a single-Represents a display area suitable for one or both of a single-
line text or image.line text or image. JTextFieldJTextField
• Represents an editable single-line text entry componentRepresents an editable single-line text entry component JButtonJButton
• Represents a push buttonRepresents a push button
JTextAreaJTextArea• Represents an editable multiline text entry componentRepresents an editable multiline text entry component
Instance variablesInstance variables
private JFrame windowprivate JFrame window• References the window containing the other References the window containing the other
components of the GUIcomponents of the GUI
Instance variablesInstance variables
private JTextArea legendAreaprivate JTextArea legendArea• References the text display for the multiline program References the text display for the multiline program
legendlegend
Instance variablesInstance variables
private JLabel fahrTagprivate JLabel fahrTag• References the label for the data entry area supplying References the label for the data entry area supplying
the temperaturethe temperature
Instance variablesInstance variables
private JTextField fahrTextprivate JTextField fahrText• References the data area supplying the temperatureReferences the data area supplying the temperature
Instance variablesInstance variables
private JLabel windTagprivate JLabel windTag• References the label for the data entry area supplying References the label for the data entry area supplying
the windspeedthe windspeed
Instance variablesInstance variables
private JTextField windTextprivate JTextField windText• References the data area supplying the windspeedReferences the data area supplying the windspeed
Instance variablesInstance variables
private JLabel chillTagprivate JLabel chillTag• References the label for the data area giving the References the label for the data area giving the
windchillwindchill
Instance variablesInstance variables
private JTextField chillTextprivate JTextField chillText• References the data area giving the windchillReferences the data area giving the windchill
Class constantsClass constants private static final String LEGEND = "private static final String LEGEND = "This windchill calculatorThis windchill calculator" "
+ " + "is intended for velocities greater than 4 mphis intended for velocities greater than 4 mph.“.“• Program legend textProgram legend text
Class constantsClass constants
private static final int WINDOW_WIDTH = 250private static final int WINDOW_WIDTH = 250• Initial width of the GUIInitial width of the GUI
250250
Class constantsClass constants
private static final int WINDOW_HEIGHT = 275private static final int WINDOW_HEIGHT = 275• Initial height of the GUIInitial height of the GUI
275275
Class constantsClass constants
private static final int TEXT_WIDTH = 20private static final int TEXT_WIDTH = 20• Number of characters per data entry areaNumber of characters per data entry area
2020
Class constantsClass constants private static final FlowLayout LAYOUT_STYLE =private static final FlowLayout LAYOUT_STYLE =
new FlowLayout() new FlowLayout()• References manager that lays out GUI components in a top-to-References manager that lays out GUI components in a top-to-
bottom, left-to-right mannerbottom, left-to-right manner
Class constantsClass constants private static FlowLayout LAYOUT_STYLE =private static FlowLayout LAYOUT_STYLE =
new FlowLayout() new FlowLayout()• References manager that lays out GUI components in a top-to-References manager that lays out GUI components in a top-to-
bottom, left-to-right mannerbottom, left-to-right manner
Program Windchill.javaProgram Windchill.java
import javax.swing.*;import javax.swing.*;
import java.awt.*;import java.awt.*;
import import java.awt.eventjava.awt.event.*;.*;
public class Windchill public class Windchill implementsimplements ActionListenerActionListener { {// class constants// class constants
// instance variables with initialization// instance variables with initialization
// Windchill(): default constructor// Windchill(): default constructor
// actionPerformed(): run button action event handler// actionPerformed(): run button action event handler
// main(): application entry point// main(): application entry point
}}
Program Windchill.java – class Program Windchill.java – class constantsconstants
private static final int WINDOW_WIDTH = 250; private static final int WINDOW_WIDTH = 250; // pixels// pixels
private static final int WINDOW_HEIGHT = 275; private static final int WINDOW_HEIGHT = 275; // pixels// pixels
private static final int FIELD_WIDTH = 20; private static final int FIELD_WIDTH = 20; // characters// characters
private static final FlowLayout LAYOUT_STYLE =private static final FlowLayout LAYOUT_STYLE =
new FlowLayout();new FlowLayout();
private static final String LEGEND = "This windchill "private static final String LEGEND = "This windchill "
+ "calculator is intended for velocities greater than 4 mph.";+ "calculator is intended for velocities greater than 4 mph.";
Program Windchill.java – instance Program Windchill.java – instance variablesvariables
// window for GUI // window for GUI
private JFrame window =private JFrame window =new JFrame("Windchill Calculator");new JFrame("Windchill Calculator");
// legend // legend
private JTextArea legendArea = new JTextArea(LEGEND, 2,private JTextArea legendArea = new JTextArea(LEGEND, 2,
AREA_WIDTH);AREA_WIDTH);
// user entry area for temperature // user entry area for temperature private JLabel fahrTag = new JLabel("Fahrenheit temperature");private JLabel fahrTag = new JLabel("Fahrenheit temperature");
private JTextField fahrText = new JTextField(FIELD_WIDTH);private JTextField fahrText = new JTextField(FIELD_WIDTH);
Program Windchill.java – instance Program Windchill.java – instance variablesvariables
// user entry area for windspeed // user entry area for windspeed private JLabel windTag = new JLabel(" Windspeed (mph)");private JLabel windTag = new JLabel(" Windspeed (mph)");
private JTextField windText = new JTextField(FIELD_WIDTH);private JTextField windText = new JTextField(FIELD_WIDTH);
// entry area for windchill result // entry area for windchill result
private JLabel chillTag =private JLabel chillTag =new JLabel(" Windchill temperature");new JLabel(" Windchill temperature");
private JTextField chillText = new JTextField(FIELD_WIDTH);private JTextField chillText = new JTextField(FIELD_WIDTH);
// run button // run button
private JButton runButton = new JButton("Run");private JButton runButton = new JButton("Run");
Program Windchill.java – Program Windchill.java – constructorconstructor
public Windchill() {public Windchill() {
// configure GUI // configure GUI
// register event listener // register event listener
// add components to container // add components to container
// display GUI // display GUI
}}
Program Windchill.java – Program Windchill.java – constructorconstructor
public Windchill() {public Windchill() {// configure GUI // configure GUI window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
legendArea.setEditable(false);legendArea.setEditable(false);legendArea.setLineWrap(true);legendArea.setLineWrap(true);legendArea.setWrapStyleWord(true);legendArea.setWrapStyleWord(true);legendArea.setBackground(window.getBackground());legendArea.setBackground(window.getBackground());
chillText.setEditable(false);chillText.setEditable(false);chillText.setBackground(Color.WHITE);chillText.setBackground(Color.WHITE);
Dangers of an editable legendDangers of an editable legend
Bad line wrappingBad line wrapping
Line wrapping inthe middle of a
word
Program Windchill.java – Program Windchill.java – constructorconstructor
public Windchill() {public Windchill() {// configure GUI …// configure GUI …
// register event listener // register event listener runButton.addActionListener(this);runButton.addActionListener(this);
Run button action-event Run button action-event handlinghandling
ActionEvent
actionPerformer() Method
Get data entries from temperatureand windspeed data areas
Compute windchill according to theWeather Service formula
Display result to windchill dataarea
GUI : Action Listener
When the run button isclicked, it dispatches an
action event
Action events aresent to registered
action listeners
An ActionListener has anactionPerformer() method thathandles the class-specific activity
Program Windchill.java – Program Windchill.java – constructorconstructor
public Windchill() {public Windchill() {// configure GUI …// configure GUI …
// register event listener …// register event listener …
// add components to container // add components to container Container c = window.getContentPane();Container c = window.getContentPane();c.setLayout(LAYOUT_STYLE);c.setLayout(LAYOUT_STYLE);
c.add(legendArea);c.add(legendArea);c.add(fahrTag);c.add(fahrTag);c.add(fahrText);c.add(fahrText);c.add(windTag);c.add(windTag);c.add(windText);c.add(windText);c.add(chillTag);c.add(chillTag);c.add(chillText);c.add(chillText);c.add(runButton);c.add(runButton);
Program Windchill.java – Program Windchill.java – constructorconstructor
public Windchill() {public Windchill() {
// configure GUI …// configure GUI …
// register event listener …// register event listener …
// add components to container …// add components to container …
// make GUI visible // make GUI visible
window.setVisible(true);window.setVisible(true);
Program Windchill.java – action Program Windchill.java – action performerperformer
public void actionPerformed(ActionEvent e) {public void actionPerformed(ActionEvent e) {// get user’s responses// get user’s responses
// compute windchill// compute windchill
// display windchill // display windchill }}
Program Windchill.java – action Program Windchill.java – action performerperformer
public void actionPerformed(ActionEvent e) {public void actionPerformed(ActionEvent e) {// get user’s responses// get user’s responsesString response1 = fahrText.getText();String response1 = fahrText.getText();double t = Double.parseDouble(response1);double t = Double.parseDouble(response1);String response2 = windText.getText();String response2 = windText.getText();double v = Double.parseDouble(response2);double v = Double.parseDouble(response2);
// compute windchill// compute windchill
// display windchill // display windchill }}
Program Windchill.java – action Program Windchill.java – action performerperformer
Program Windchill.java – action Program Windchill.java – action performerperformer
public void actionPerformed(ActionEvent e) {public void actionPerformed(ActionEvent e) {// get user’s responses// get user’s responsesString response1 = fahrText.getText();String response1 = fahrText.getText();double t = Double.parseDouble(response1);double t = Double.parseDouble(response1);String response2 = windText.getText();String response2 = windText.getText();double v = Double.parseDouble(response2);double v = Double.parseDouble(response2);
// compute windchill// compute windchilldouble windchillTemperature = 0.081 * (t - 91.4)double windchillTemperature = 0.081 * (t - 91.4)
* (3.71*Math.sqrt(v) + 5.81 - 0.25*v) + 91.4;* (3.71*Math.sqrt(v) + 5.81 - 0.25*v) + 91.4;
int perceivedTemperature = int perceivedTemperature = (int) Math.round(windchillTemperature);(int) Math.round(windchillTemperature);
// display windchill // display windchill }}
Program Windchill.java – action Program Windchill.java – action performerperformer
public void actionPerformed(ActionEvent e) {public void actionPerformed(ActionEvent e) {// get user’s responses// get user’s responsesString response1 = fahrText.getText();String response1 = fahrText.getText();double t = Double.parseDouble(response1);double t = Double.parseDouble(response1);String response2 = windText.getText();String response2 = windText.getText();double v = Double.parseDouble(response2);double v = Double.parseDouble(response2);
// compute windchill// compute windchilldouble windchillTemperature = 0.081 * (t - 91.4)double windchillTemperature = 0.081 * (t - 91.4)
* (3.71*Math.sqrt(v) + 5.81 - 0.25*v) + 91.4;* (3.71*Math.sqrt(v) + 5.81 - 0.25*v) + 91.4;
int perceivedTemperature = int perceivedTemperature = (int) Math.round(windchillTemperature);(int) Math.round(windchillTemperature);
// display windchill // display windchill String output = String.valueOf(perceivedTemperature);String output = String.valueOf(perceivedTemperature);chillText.setText(output);chillText.setText(output);
}}
Program Windchill.java – action Program Windchill.java – action performerperformer
Method main()Method main()
public static void main(String[] args) {public static void main(String[] args) {
Windchill gui = new Windchill();Windchill gui = new Windchill();
}}
Another method main()Another method main()
public static void main(String[] args) {public static void main(String[] args) {
Windchill gui1 = new Windchill();Windchill gui1 = new Windchill();
Windchill gui2 = new Windchill();Windchill gui2 = new Windchill();
}}
What Next…What Next… Practice Work:Download and run the existing Practice Work:Download and run the existing
windchill programmewindchill programme• Amend the code to remove the need for the run buttonAmend the code to remove the need for the run button• HINT: respond to enter on the text field HINT: respond to enter on the text field
Coursework Work:Coursework Work:• Develop a simple desktop calculatorDevelop a simple desktop calculator
Minimum is simple calculatorMinimum is simple calculator AddAdd
• % conversions% conversions• trig functionstrig functions
• HINT: Build on the windchill exampleHINT: Build on the windchill example
1234.098
1 2 3
4 5 6
7 8 9
0 .
+
-
/
*
=
+/-
C
Recommended