44
A simple swing A simple swing example example GETTING STARTED WITH WIND GETTING STARTED WITH WIND CHILL CHILL

A simple swing example GETTING STARTED WITH WIND CHILL

Embed Size (px)

Citation preview

Page 1: A simple swing example GETTING STARTED WITH WIND CHILL

A simple swing exampleA simple swing example

GETTING STARTED WITH WIND GETTING STARTED WITH WIND CHILLCHILL

Page 2: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 3: A simple swing example GETTING STARTED WITH WIND CHILL

Console-based programmingConsole-based programming

Console program

Method main() { statement1; statement2; ... statementm;}

Console programsbegin and end inmethod main()

Page 4: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 5: A simple swing example GETTING STARTED WITH WIND CHILL

Graphical InterfaceGraphical Interface

Page 6: A simple swing example GETTING STARTED WITH WIND CHILL

In useIn use

Thereneeds to

be anevent loop

that islooking for

userinterface

events

Program needs to respond whenever the run button is clicked

Page 7: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 8: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 9: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 10: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 11: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 12: A simple swing example GETTING STARTED WITH WIND CHILL

Instance variablesInstance variables

private JTextField fahrTextprivate JTextField fahrText• References the data area supplying the temperatureReferences the data area supplying the temperature

Page 13: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 14: A simple swing example GETTING STARTED WITH WIND CHILL

Instance variablesInstance variables

private JTextField windTextprivate JTextField windText• References the data area supplying the windspeedReferences the data area supplying the windspeed

Page 15: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 16: A simple swing example GETTING STARTED WITH WIND CHILL

Instance variablesInstance variables

private JTextField chillTextprivate JTextField chillText• References the data area giving the windchillReferences the data area giving the windchill

Page 17: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 18: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 19: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 20: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 21: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 22: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 23: A simple swing example GETTING STARTED WITH WIND CHILL

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

}}

Page 24: A simple swing example GETTING STARTED WITH WIND CHILL

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.";

Page 25: A simple swing example GETTING STARTED WITH WIND CHILL

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);

Page 26: A simple swing example GETTING STARTED WITH WIND CHILL

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");

Page 27: A simple swing example GETTING STARTED WITH WIND CHILL

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

}}

Page 28: A simple swing example GETTING STARTED WITH WIND CHILL

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);

Page 29: A simple swing example GETTING STARTED WITH WIND CHILL

Dangers of an editable legendDangers of an editable legend

Page 30: A simple swing example GETTING STARTED WITH WIND CHILL

Bad line wrappingBad line wrapping

Line wrapping inthe middle of a

word

Page 31: A simple swing example GETTING STARTED WITH WIND CHILL

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);

Page 32: A simple swing example GETTING STARTED WITH WIND CHILL

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

Page 33: A simple swing example GETTING STARTED WITH WIND CHILL

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);

Page 34: A simple swing example GETTING STARTED WITH WIND CHILL

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);

Page 35: A simple swing example GETTING STARTED WITH WIND CHILL

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 }}

Page 36: A simple swing example GETTING STARTED WITH WIND CHILL

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 }}

Page 37: A simple swing example GETTING STARTED WITH WIND CHILL

Program Windchill.java – action Program Windchill.java – action performerperformer

Page 38: A simple swing example GETTING STARTED WITH WIND CHILL

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 }}

Page 39: A simple swing example GETTING STARTED WITH WIND CHILL

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);

}}

Page 40: A simple swing example GETTING STARTED WITH WIND CHILL

Program Windchill.java – action Program Windchill.java – action performerperformer

Page 41: A simple swing example GETTING STARTED WITH WIND CHILL

Method main()Method main()

public static void main(String[] args) {public static void main(String[] args) {

Windchill gui = new Windchill();Windchill gui = new Windchill();

}}

Page 42: A simple swing example GETTING STARTED WITH WIND CHILL

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();

}}

Page 43: A simple swing example GETTING STARTED WITH WIND CHILL
Page 44: A simple swing example GETTING STARTED WITH WIND CHILL

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