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

Slides:



Advertisements
Similar presentations
Life Cycle of an Applet, along with response to mouse events and other simple features. (ps:only two possible examples for life circle of an Applet)
Advertisements

Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Applets What is an Applet? Applet Parameters Graphics in Applets Other Applet.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Chapter Day 23. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 23 Problem set 4 Due Problem set 5 Posted (Last one)  Due Dec 8 Capstones.
Applets. An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser You.
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
A Simple Applet.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Programming Task: Task 1 Controlled Assessment Practice.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
JAPPLET.
Repetition - while, for and do “Loops - iterations Chapter Eight.
Session 9 CannonWorld, Event-driven Programming, and Border Layout.
Java Applet Presented by Fitsum Okubu. Introduction Introduction Graphics Graphics Methods and Variables Methods and Variables Events Events Decision.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
Session 10 CannonGame and Event-driven Programming.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
Applications Development
Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Object Oriented Programming.  Interface  Event Handling.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
May 12, 1998CS102-01Lecture 7-3 Building GUIs in Java I CS Lecture 7-3 A picture's worth a thousand words.
Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and.
Applets. Internet/WWW How does it work? Internet/WWW.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
Creating User Interfaces Chapter 13 CSCI CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.
CSI 3125, Preliminaries, page 1 AWT Control. CSI 3125, Preliminaries, page 2 AWT Control The AWT supports the following types of controls: ■ Labels ■
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
1 Java Applet Basics Chapter Eight. 2 Applets vs. Applications l Applications: Stand alone Java programs run by interpreter l Applets WWW browser embedded.
Events (Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth.
Laboratory Study November, Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Introducing, the JFrame Gives us a work area beside System.out.println.
CSC 205 Programming II Lecture 5 AWT - I.
Aum Amriteshwaryai Namah
A First Look at GUI Applications
Lecture 09 Applets.
Decisions - if and switch
Object Oriented Programming
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
11.1 Applets & graphics.
Session 29 Introducing CannonWorld (Event-driven Programming)
Presentation transcript:

Events Chapter Six

Old (procedural) vs. OOPS Programs §OOPS programs react to the users demands §Anytime/anyplace §EVENT = “An Action taken by a user” l Program waits (LOOKS) for an event to happen l when it does the program handles the event.

Event Program §Button event §Scrollbar event §key pressed event §any other event program “Looks” for §in a LOOP … l ActionListener l AdjustmentListener

Mouse drag Handle it Key Press Mouse Click Example Loop Event

FirstEvent Program Import java.awt.*; import applet.Applet; import.awt.event.*; // new one here -Events public class FirstEvent extends Applet implements 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 l displays a scrollbar l User can move mouse to slider and “Operate” it l Program shows “where/what numeric position slider is on” Range 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( ) { l int a; // a is local l x = 42; l // etc more stuff we don’t care about l } l 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 Applet implements 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 lot this 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 -Events public 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, ) ; } public void adjustmentValueChanged(AdjustmentEvent e) { bar1Value = bar1.getValue( ); bar2Value = bar2.getValue( ); repaint( ); } }

Inches to Centimeters Import java.awt.*; import applet.Applet; import.awt.event.*; // new one here -Events public class inchesToCm extends Applet implements 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 Applet implements 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; }