events chapter six. old (procedural) vs. oops programs §oops programs react to the users demands...

Post on 01-Jan-2016

219 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Events

Chapter Six

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.

Event Program

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

ActionListener AdjustmentListener

Mouse dragHandle it

Key Press

Mouse Click

Example Loop Event

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

Private Scrollbar slider;

private int sliderValue = 0;

public void init( ) {

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

add(slider);

slider.addAdjustmentListener(this); }

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

100,100); }

public void adjustmentValueChanged (AdjustmentEvent e) {

sliderValue = slider.getValue( );

repaint( );

}

}

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

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

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)

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

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

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

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 {

Private Scrollbar slider;private int sliderValue ;

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

100);add(slider);

slider.addAdjustmentListener(this); }

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

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

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;

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

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

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

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;

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

public void adjustmentValueChanged(AdjustmentEvent e) {

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

repaint ( );

}

}

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;

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;

}

}

top related