26
Events Chapter Six

Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Embed Size (px)

Citation preview

Page 1: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Events

Chapter Six

Page 2: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Old (procedural) vs. OOPS Programs

OOPS programs react to the users demandsAnytime/anyplace

EVENT = “An Action taken by a user” Program waits (LOOKS) for an event to happen when it does the program handles the event.

Page 3: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Event Program

Button eventScrollbar eventkey pressed eventany other event program “Looks” forin a LOOP …

ActionListener AdjustmentListener

Page 4: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Mouse dragHandle it

Key Press

Mouse Click

Example Loop Event

Page 5: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

FirstEvent ProgramImport java.awt.*;

import applet.Applet;

import.awt.event.*; // new one here -Events

public class FirstEvent extends Appletimplements AdjustmentListener {

// NEW “implements” NOT inherit but use //some of AdjustmentListener’s neat stuff

Page 6: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Private Scrollbar slider;

private int sliderValue = 0;

public void init( ) {

slider = new Scrollbar (Scrollbar.HORIZONTAL, 0,1,0, 100);

add(slider);

slider.addAdjustmentListener(this); }

Page 7: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Public void paint (Graphics g) {g.drawString(“Current value is “ + sliderValue,

100,100); }

public void adjustmentValueChanged (AdjustmentEvent e) {

sliderValue = slider.getValue( );

repaint( );

}

}

Page 8: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Breakdown on the “New” stuff

What the program does displays a scrollbar User can move mouse to slider and “Operate” it Program shows “where/what numeric position

slider is on”• Range 0 - 100

• initial value 0

• width of thumb is one pixel and bar is horizontal

Page 9: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

More new stuff

repaint ( ) ; // call to library “redraw stuff”sliderValue = slider.getValue ( ); //New// call to method in library that gives

numeric value of where/what number (position slider bar is at currently) User can and usually does move it with mouse.

Slider.addAdjustmentListener(this); //New//current address (this) passed to slider

Page 10: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

More New stuff

public void init ( ); // reserved used to display initial screen (The bar in original position)

Public void paint( Graphics g) { // You should have seen before

public void adjustmentValueChanged (AdjustmentEvent e) { // an object e holds the new value of the event handleing method (for the scrollbar)

Page 11: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Scope (revisited)

Where a variable can be used (can have value)

Where a variable is declared is where it has scope

local ones can only be used within the method they are created

Page 12: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Class Demo extends Applet {…private int x;private void methodOne( ) {

int a; // a is local x = 42; // etc more stuff we don’t care about } private void methodTwo ( ) {

• int b; // b is local• b =x; • }• }

Page 13: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

The AWT (only note in text windows 95 NOT totally compatible with JDK)

Very few problems in this course except with some browsers some times

Page 14: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

An event driven example (in text page 71)

Import java.awt.*;

import applet.Applet;

import.awt.event.*; // new one here -Events

public class WindowBlind extends Appletimplements AdjustmentListener {

Page 15: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Private Scrollbar slider;private int sliderValue ;

public void init( ) {slider = new Scrollbar (Scrollbar.VERTICAL, 0,1,0,

100);add(slider);

slider.addAdjustmentListener(this); }

Page 16: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

public void paint (Graphics g) {showStatus (“Scrollbar value is “ + sliderValue);

g.drawRect(40, 80, 60, 100);g.fillRect(40, 80, 60, sliderValue); }

public void adjustValueChanged(AdjustmentEvent e) {sliderValue = slider.getValue ( );repaint ( ); } }

Page 17: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

New stuff NOT really a lotthis time

Class WindowBlind is just another name we made up could have been aardvark but WindowBlind seems a better name

Other methods standard ones NOT new

Vertical not horizontal scrollbar this program

The adjustmentValueChanged is the same as the old example with HORIZONTAL scrollbar

Page 18: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Adding Labels (TEXT) on the screen

Import java.awt.*;import applet.Applet;import.awt.event.*; // new one here -Eventspublic class LabelDemo extends Applet

implements AdjustmentListener {private Scrollbar bar1, bar2;private in bar1Value = 0;private in bar2Value = 0;

Page 19: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Public void init ( ) {

label title1, title2; // local scope

title1 = new Label (“up:”);

add(title1);

bar1 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,100);

add(bar1);

bar1.addAdjustmentListener(this);

Page 20: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

title2 = new Label (“ down:”);

add(title2);

bar2 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,100);

add(bar2);

bar2.addAdjustmentListener(this);

// NOTE just copied with editor this whole slide with minor changes from last slide

save keying in all again

Page 21: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

// some new stuff (labels)

public void paint (Graphics g) {

g.drawString(“Up value is “ + barValue, 100, 100);

g.drawString(“DOWN value is “ + bar2Value, 100. 150) ; }

public void adjustmentValueChanged(AdjustmentEvent e) { bar1Value = bar1.getValue( );

bar2Value = bar2.getValue( );

repaint( ); } }

Page 22: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Inches to CentimetersImport java.awt.*;

import applet.Applet;

import.awt.event.*; // new one here -Events

public class inchesToCm extends Appletimplements AdjustmentListener {

Private Scrollbar slider;

private int sliderValue = 0;

Page 23: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

public void init( ) {

slider = new Scrollbar (Scrollbar.HORIZONTAL, 0,1,0, 100);

add(slider);

slider.addAdjustmentListener(this); }

public void paint (Graphics g) {

float cmEquivalent;

cmEquivalent = sliderValue * 2.54f;

g.drawstring(“ Inches =“+ slider.getValue +

“ Cm =“ + cmEquivalent, 100, 100); }

Page 24: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

public void adjustmentValueChanged(AdjustmentEvent e) {

sliderValue = (float) slider.getValue( ) / 10;

repaint ( );

}

}

Page 25: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Import java.awt.*;

import applet.Applet;

import.awt.event.*; // new one here -Events

public class ScrollbarValues extends Appletimplements AdjustmentListener {

Private Scrollbar slider;

private int currentX = 1;

private int currentY = 5;

Page 26: Events Chapter Six. Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user”

Public void init( ) {

slider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 100);

add(slider);

slider.addAdjustmentValueChanged(AdjustmentEvent e)

Graphics g = getGraphics( );

currentX = slider.getValue( );

g.drawLine( 0, currentY, currentX, currentY);

currentY = currentY + 5;

}

}