Applet which displays a simulated trackball in the upper half of its window.

Similar documents
Programmierpraktikum

CSIS 10A Assignment 14 SOLUTIONS

public void mouseexited (MouseEvent e) setminute(getminute()+increment); 11.2 public void mouseclicked (MouseEvent e) int x = e.getx(), y = e.gety();

PESIT Bangalore South Campus

G51PRG: Introduction to Programming Second semester Applets and graphics

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

Java Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,

Building Graphical User Interfaces with the MVC Pattern

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

NITI NITI I PRIORITET

Java Applet Basics. Life cycle of an applet:

CS2110. GUIS: Listening to Events

I/O Framework and Case Study. CS151 Chris Pollett Nov. 2, 2005.

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

GUI in Java TalentHome Solutions

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

The AWT Event Model 9

Graphical User Interfaces 2

Advanced Internet Programming CSY3020

Windows and Events. created originally by Brian Bailey

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently.

CS2110. GUIS: Listening to Events. Anonymous functions. Anonymous functions. Anonymous functions. Checkers.java. mainbox

CS2110. GUIS: Listening to Events Also anonymous classes versus Java 8 functions. Anonymous functions. Anonymous functions. Anonymous functions

CS2110. GUIS: Listening to Events

Which of the following syntax used to attach an input stream to console?

Advanced Java Programming

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Chapter 14: Applets and More

8/23/2014. Chapter Topics. Introduction to Applets. Introduction to Applets. Introduction to Applets. Applet Limitations. Chapter 14: Applets and More

AP Computer Science Unit 13. Still More Graphics and Animation.

Chapter 1 GUI Applications

Chapter 14: Applets and More

Java: Graphical User Interfaces (GUI)

Computer Science 210: Data Structures. Intro to Java Graphics

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

Java. GUI building with the AWT

AP CS Unit 12: Drawing and Mouse Events

Java - Applets. public class Buttons extends Applet implements ActionListener

Java TM Applets. Rex Jaeschke

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

CHAPTER 2. Java Overview

Event Driven Programming

Come & Join Us at VUSTUDENTS.net

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1)

Advanced Java Unit 6: Review of Graphics and Events

2. (True/False) All methods in an interface must be declared public.

EXCEPTIONS & GUI. Errors are signals that things are beyond help. Review Session for. -Ankur Agarwal

Lab 10: Inheritance (I)

Java - Dates and Threads

Module 5 Applets About Applets Hierarchy of Applet Life Cycle of an Applet

Lecture 5: Java Graphics

Lecture 3: Java Graphics & Events

CS 120 Fall 2008 Practice Final Exam v1.0m. Name: Model Solution. True/False Section, 20 points: 10 true/false, 2 points each

: 15. (x, y) (x, y ) (x, y) ( x x = 1 y = y ) (x = x y y = 1) (12.1.1)

Computer Science II - Test 2

1 Definitions & Short Answer (5 Points Each)

Introduction to Computer Science I

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

CSE 331 Software Design & Implementation

Java Applet & its life Cycle. By Iqtidar Ali

CS 351 Design of Large Programs Threads and Concurrency

import java.awt.*; import java.applet.*; import java.awt.event.*; import java.lang.*; import java.io.*; import java.util.*; import java.net.

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Science and Engineering

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CS 106A, Lecture 14 Events and Instance Variables

Swing Rocks - A Tribute to Filthy Rich Clients

News! Feedback after Lecture 4! About Java and OOP!

User interfaces and Swing

Lecture 19 GUI Events

CS 106A, Lecture 14 Events and Instance Variables

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Introduction to Java Applets 12

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Building Java Programs

Objectives. OO Exceptions (ch( ch.. 10) and a little Applets thrown in. Applets. Understand Applet model Understand Exceptions

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Programming: You will have 6 files all need to be located in the dir. named PA4:

Building Java Programs

CS 170 Java Programming 1. Week 9: Learning about Loops

Exam: Applet, Graphics, Events: Mouse, Key, and Focus

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

Twin A Design Pattern for Modeling Multiple Inheritance

FirstSwingFrame.java Page 1 of 1

Java for Interfaces and Networks (DT3010, HT10)

Outline. More on the Swing API Graphics: double buffering and timers Model - View - Controller paradigm Applets

Görünt Görün ülerin ölçeklenmesi ve yerle y ştirilmesi ş

Studying software design patterns is an effective way to learn from the experience of others

Programming graphics

Advanced Java Programming (17625) Event Handling. 20 Marks

ISO-2022-JP (JIS ) 1 2. (Windows95/98 MacOS ) Java UNICODE UNICODE. Java. .java.java.txt native2ascii. .java

TWO-DIMENSIONAL FIGURES

Java Applets / Flash

Java Threads. COMP 585 Noteset #2 1

Outline. Observer Pattern: Pitfalls. Observer Applications

Overloading Example. Overloading. When to Overload. Overloading Example (cont'd) (class Point continued.)

Transcription:

Example: Applet which displays a simulated trackball in the upper half of its window. By dragging the trackball using the mouse, you change its state, given by its x-y position relative to the window boundaries, expressed as a percentage from 0 to 100. The relative x-position is displayed by a bar chart at the lower left of the window, and the relative y-pos is displayed by a pie chart at the lower right. public class BarChart extends Applet implements Observer { public BarChart(Subject subject) { subject.addobserver(this); public void paint( Graphics g ) { super.paint(g); g.setcolor(color.black); g.drawstring("relative X position:", 0, 10); g.setcolor(color.red); g.fillrect(0, 15, thesize, 10); public void update(observable s, Object arg) { Point p = (Point) arg; thesize = p.x; repaint( ); private int thesize = 10;

public class PieChart extends Applet implements Observer { public PieChart(Subject subject) { subject.addobserver(this); public void paint(graphics g) { g.setcolor(color.black); g.drawstring("relative Y position:", 0, 10); g.setcolor(color.blue); g.fillarc(0, 15, 50, 50, 0, 360*theSize/100); public void update(observable s, Object arg) { Point p = (Point) arg; thesize = p.y; repaint( ); private int thesize = 10; public class Trackball extends Canvas implements Subject, MouseListener, MouseMotionListener { public Trackball() { setbackground(color.lightgray); addmousemotionlistener(this);

public Point getstate() { int width = getsize().width; int height = getsize().height; int relx = (int) (100.0 * (double) thex / ((double) width - DIAMETER) + 0.5); int rely = (int) (100.0 * (double) they / ((double) height - DIAMETER) + 0.5); return new Point(relx, rely); public void paint(graphics g) { g.setcolor(color.black); g.drawrect(0, 0, getsize().width -1, getsize().height -1); g.drawrect(1, 1, getsize().width -3,getSize().height - 3); g.drawrect(2, 2, getsize().width -5, getsize().height - 5); g.setcolor(color.darkgray); g.filloval(thex, they, DIAMETER, DIAMETER); public void addobserver(observer o) { thesubject.addobserver(o); public void mousereleased(mouseevent event) { isdragging = false; public void mouseclicked (MouseEvent event) { ; public void mouseentered (MouseEvent event) { ; public void mouseexited (MouseEvent event) { ; public void mousepressed (MouseEvent event) { ; public void mousemoved (MouseEvent event) { ;

public void mousedragged (MouseEvent event) { int x = thex + DIAMETER/2; int y = they + DIAMETER/2; int px = event.getx(); int py = event.gety(); int d = (int) Math.sqrt((x -px)*(x -px) + (y -py)*(y -py)); if (isdragging d < DIAMETER/2) { isdragging = true; thex = px - DIAMETER/2; they = py - DIAMETER/2; int width = getsize().width; int height = getsize().height; if (thex < 0) thex = 0; if (they < 0) they = 0; if (thex > width - DIAMETER) thex = width - DIAMETER; if (they > height - DIAMETER) they = height - DIAMETER; thesubject.setchanged(); thesubject.notifyobservers(getstate()); repaint( ); // The TrackBall extends Canvas but also needs the state and behavior of an Observable. It creates a captive Observable object which will do its bidding, and that implements the Subject interface that can handle addobserver(). private OwnedSubject thesubject = new OwnedSubject(); private static final int DIAMETER = 20; private int thex = DIAMETER; private int they = DIAMETER; private boolean isdragging = false;

import java.applet, awt, awt.event.*; public class MVC extends Applet { public void init() { setlayout(new GridLayout(2, 1)); thebarchart = new BarChart(theTrack); thepiechart = new PieChart(theTrack); Panel south = new Panel(); south.setlayout(new GridLayout(1, 2)); south.add(thebarchart); south.add(thepiechart); add(thetrack); add(south); private Trackball thetrack = new Trackball( ); private Applet thebarchart; private Applet thepiechart; // OwnedSubject combines the properties of an Obsevable and a Subject. import java.util.observable; public class OwnedSubject extends Observable implements Subject { public void setchanged() { super.setchanged(); // The promise made by the Subject interface is that it can handle the addobserver method import java.util.observer; public interface Subject { public void addobserver(observer o);

Example: - When the applet Test is executed, a GUI with a text field and a button is displayed. - The applet s init() instantiates the Clock c, adds the applet into the Clock s list of Observers (addobserver(this)), defines a new thread for the Clock (clockt), and getsthe date & time when the Clock was created (c.getdate()). - When the user clicks the button, the Clock s method c.setalarm() is called which sets the variable alarmset to true. - When the Clock awakens, it gets a new time & date (d.newdate()) and calls setchanged() to indicate that the state of the Clock has changed. - Method notifyobservers(d.tostring()) automatically invoke the update method of all its observers. - Method update() displays the new date and time in the text field and a message in the status bar area. public class Test extends Applet implements Observer { clock c; Label alabel; TextField atext; Button alarmon; Thread clockt; public void init() { c = new Clock; c.addobserver(this); clockt = new Thread(c); alabel = new Label( Current Date & Time: ); atext = new TextField(c.getDate(), 25); atext.seteditable(false); alarmon = new Button( Alarm on ); alarmon.addactionlistener(this); add(alabel); add(atext); add(alarmon); clockt.start();

public void actionperformed(actionevent e) { showstatus( Alarm set at + c.setalarm()); public void update(observable ob, Object arg) { atext.settext(arg.tostring()); showstatus( Alarm sounded, see new time ); class Clock extends Observable implements Runnable { Date d; boolean alarmset; public Clock() {d = new Date(); public String getdate() { return d.tostring(); public String setalarm() { alarmset = true; d = new Date(); return d.tostring(); public void run() { while (true) { if (alarmset) { try { Thread.sleep(10000); catch (InterruptedException e) { System.err.println( Sleep interrupted ); alarmset = false; d = new Date(); // get new time setchanged(); notifyobservers(d.tostring());