Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.

Slides:



Advertisements
Similar presentations
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Advertisements

Mouse Events and Keyboard Events
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
1 Lecturte 14Lecture 7 Applications of Graphics Overview  Conversions Between Applications and Applets  Handling Mouse Events  Handling Keyboard Events.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
APPLETS CSC 171 FALL 2004 LECTURE 6. APPLETS Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
Unit 071 Review of Applets Learning Outcomes oDistinguish between Java Applications and Java Applets. oWrite applet programs that can load images and play.
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.
Applets. What is an Applet?  According to Sun “An applet is a small program that is intended not to be run on its own, but rather to be embedded inside.
Lecture 17: Animation Yoni Fridman 7/27/01 7/27/01.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Applets. Applets The term Applet refers to a little application. In JAVA the applet is a java program that is embedded within a HTML document and.
Programming Task: Task 1 Controlled Assessment Practice.
CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Applets Java API.
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.
Internet Software Development Applets Paul J Krause.
Java applet basics, loading & displaying images After this section, you should be able to : –Use the applet tag and applet parameters –Describe what a.
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Introduction to Applets Course Lecture Slides 29 th July 2010.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
Applets Session 8. Java Simplified / Session 8 / 2 of 31 Review The Abstract Windowing Toolkit (AWT) is a set of classes that allow us to create a graphical.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Lec.10 + (Chapter 8 & 9) GUI Java Applet Jiang (Jen) ZHENG July 6 th, 2005.
12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Applets Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
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.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Graphics Methods public abstract void drawString(String str, int x, int y): is.
1 Java Applet Basics Chapter Eight. 2 Applets vs. Applications l Applications: Stand alone Java programs run by interpreter l Applets WWW browser embedded.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
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.
KeyListener and Keyboard Events Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) –to implement.
Lec 15 Writing an Applet Class. Agenda Writing an Applet class Java Graphics class.
Introducing, the JFrame Gives us a work area beside System.out.println.
Java Applets Getting Started. Import Files In order to run the applet properly, we have to import some files into our class. These files are: –import.
Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Java Applets.
Object Oriented Programming
Java Applets.
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Chapter 13: Advanced GUIs and Graphics
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
UNIT-5.
Java Applets.
Java Applets.
Java applets 1/3/2019.
11.1 Applets & graphics.
Advanced GUIs and Graphics
APPLET PROGRAMMING.
Presentation transcript:

Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class

Agenda More Applet stuff – Drawing Strings on the applet window – Responding to mouse events – Responding to key strokes

Two Ways to Use Java Applet—runs on the web – uses graphical interfaces (usually) – can't access files on local computer – downloaded from website by browser – no main method in class! Application—runs on a local computer – has main method – Can run "stand alone" on local computer – can use graphical or nongraphical interfaces

Drawing on Computer Normal vs Computer Graphics Coordinates

Simple Drawing Demo Applet import java.applet.*; //so we can extend the Applet class import java.awt.*; //so we can use graphics commands public class DrawDemo extends Applet { public void paint(Graphics g) { //draws a blue oval on the screen g.setColor(new Color(0, 0, 255)); g.fillRect(100,100,30,50); g.setColor(new Color(0, 255, 0)); g.drawLine(115,125, 315,125); g.setColor(new Color(255, 0, 0)); g.drawOval(275, 85, 80, 80); }

Java Graphics Class s/api/java/awt/Graphics.html s/api/java/awt/Graphics.html

For today's demo Create new project, Sign Download appletShell from Lab16, call it Sign Notice the "implements" and extra methods – required to be able to access keyboard and mouse

Adding Strings to an Applet Window let's us draw words on the screen – g.drawString("hello world",100,200); – g.drawString("WHAT?", 200,400); abstract void drawStringdrawString(String str, int x, int y) Draws the text given by the specified string, using this graphics context's current font and color.String

Changing the FontFont set the font size: – g.setFont( new Font(null,Font.PLAIN, 30)); (null means no value: use the default font) – g.setFont( new Font("Courier",Font.BOLD, 16)); takes awhile to load the font! FontFont(String name, int style, int size) Creates a new Font from the specified name, style and point size.String

Instance variables We can change the location and size of our String by making instance variables in our applet class. int x,y; // the x and y coordinates of the next String int size; // the size of the string Then, inside the init() method, we start them at their initial values: public void init() { //init is called when applet starts (like a constructor) addMouseListener(this); //registers this applet to receive mse events addKeyListener(this); //registers this applet to receive key events x=100; y=200; size=10; }

our paint method needs mods public void paint(Graphics g) { g.drawString("hello world",x,y); g.setFont( new Font(null,Font.BOLD,size)); g.drawString("hello back!",x,y+100); }

mouse handler changes x and y, repaints public void mousePressed(MouseEvent e) { x= e.getX(); y= e.getY(); repaint(); }

Similarly, in keyPressed... public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_UP) size=size+10; if (e.getKeyCode() == KeyEvent.VK_DOWN) size=size-10; repaint(); }

Write an Applet that draws an arrow the arrow should move to mouse when clicked the arrow should move when arrow keys pressed Lab16Target