Object Oriented Programming Lecture 5: Refactoring by Inheritance and Delegation - A simple Design Pattern for animation applets, A Generic Function Plotter.

Slides:



Advertisements
Similar presentations
1 Applets Programming Enabling Application Delivery Via the Web.
Advertisements

Made with love, by Zachary Langley Applets The Graphics Presentation.
Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
1 Object Oriented Programming Lecture XII The Adapter,Template, Strategy and Factory design patterns.
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.
Object Oriented Programming with Java (150704).   Applet  AWT (Abstract Window Toolkit)  Japplet  Swing Types of Applet.
Object Oriented Programming Lecture 7: Algorithm animation using strategy and factory patterns, The Adapter design pattern
Programming and Problem Solving With Java Copyright 1999, James M. Slack Applets What is an Applet? Applet Parameters Graphics in Applets Other Applet.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
 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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
A Simple Applet --- Digital Clock import java.awt.*; import java.util.Calendar; public class DigitalColok extends java.applet.Applet implements Runnable.
Abstract Classes An abstract class is a class with partial implementation. It implements behaviors that are common to all subclasses, but defers to the.
Enahnced Digital Clock Applet Setting applet parameters in the web page. The applet tag in HTML:
1 Recitation 11. Applet Applets. An applet is a Java program that is started by a browser (e.g. netscape or internet explorer) when an html file has a.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
APPLETS CSC 171 FALL 2004 LECTURE 6. APPLETS Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
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.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Lecture 17: Animation Yoni Fridman 7/27/01 7/27/01.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
1 Features of Java CS 3331 Fall Outline  Abstract class  Interface  Application --- animation applets.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
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.
JAVA APPLETS By: Ms. Humaira Siddiqui. Java and the Internet Java is considered to be a language well suited to be used in the internet. In contrast with.
Internet Software Development Applets Paul J Krause.
JAPPLET.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
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.
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.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
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.
Java Applets 1. What is an applet? An applet is a small Java program that is typically embedded in a Web page and can be run using the applet viewer or.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Design Patterns: Design by Abstraction
1 Contents Introduction Applet Vs Application Security Restrictions on Applet A simple example “Hello World!” applet Compiling & Running Applet HTML document.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
Copyright © Curt Hill Applets A different type of program.
Creating a Java Application and Applet
More OOP. Extending other’s classes extend Java platform classes, e.g. class Applet public class MyApplet extends Applet { public void init() { } public.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Creating an Object that can draw itself The paint method can ‘draw’ because it is passed a graphics environment as a parameter. If a class method is passed.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
UNIT-5.
Java applets 1/3/2019.
Applet in Java.
Java Programming COMP-417 Applet
11.1 Applets & graphics.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Enabling Application Delivery Via the Web
APPLET PROGRAMMING.
Presentation transcript:

Object Oriented Programming Lecture 5: Refactoring by Inheritance and Delegation - A simple Design Pattern for animation applets, A Generic Function Plotter

Setting the environment variables for JDK in UNIX  set the CLASSPATH environment variable in your ”.cshrc.private” file  If the CLASSPATH is not set: setenv CLASSPATH ~/test/myclasses  If the CLASSPATH already is set: setenv CLASSPATH ~/test/myclasses::~/classdir/test2::etc

The Applets Design Pattern  Java applets embedded in web pages  Applets are downloaded from a web server and executed on the client in a web browser or appletviewer  Applets must extend the Applet class  Can be found in java.applet package

Embedding the applets in HTML  The simplest form of applet HTML tags: <applet code = byte-code filename width in pixels height in pixels>  Will make the browser execute the applet in the webpage context

Interaction between the context and applet  An applet interacts with the context according to a contractual interface  Init() – initialize the applet when it is initially loaded  Start() – activates the applet and is invoked when entering the webpage  Stop() – deactivates the applet  Destroy() - destroys the applet when the webpage is discarded

Simple animation applet  The applet displays the current time HH:MM:SS  In order draw on the screen we must overload the paint() method  Requires a Thread to control the Animation  Problem: We can’t extend both Applet and Thread...?

The Runnable Interface  The Solution: Java provides the Runnable Interface Implemented by a class that should run in a Thread  By implemententing Runnable A class can pass itself as argument to the Thread() constructor  We need to define the run() method that is invoked when starting the Thread

The start() method public void start(){ if(clockThread != null){ clockThread = new Thread(this); clockThread.start(); } Public void stop() clockThread = null; Calls run()! Kill the Thread!

Controlling the Animation - the run() method public void run(){ while(Thread.currentThread() == clockThread){ repaint(); try{ Thread.currentThread().sleep(1000); }catch(InterruptedException e){}; } Calls paint() to draw the time on the screen Sleep 1 second before redrawing!

Drawing the time on screen - the paint() method Public void paint(graphics g){... g.setFont(font); g.setColor(color); g.drawString(hour + ”:” + minute + ”:” + second); }

Idiom: Animation Applet  An Idiom: ”How we can program a generic template that can be reused for a recurring problem” It should be possible to customize and adapt  Applets can produce some graphical output that changes without interaction from the user (animation)  Using the Animation Applet Idiom: extend AnimationApplet and redefine the paint method

Double buffering  If painting directly on the screen, the screen will ”flicker”  Double buffering can be used to solve this problem  Double buffering 1 ”draw invisibly” in a background buffer 2 Then update the screen with the buffer

Double buffering  When calling repaint() it will automatically call the update()  update() will clear the screen using the background color and call the paint method paint(Graphics g);  Solution: To avoid the the ”flicker” - override the update method  Instead of clearing - let update(); paint the buffered image

A Generic Double buffered Animation Applet  What do we need to do? We need to create a background image. We need to override the update() method. We need a method to do the drawing in the background image.  We can extend and reuse the simple Applet to define a generic Animation Applet

DoubleBuffered Animation Applet – Refactoring the applet Public class abstract DBAnimationApplet extends AnimationApplet{... Graphics backGraphics; Image backImage; Dimension dim; Boolean doubleBuffered;... }

The init() method Public final void init(){ dim = getSize(); backImage = new Image(dim.width, dim.height); backGraphics = backImage.getGraphics(); initAnimator(); } Protected void initAnimator(){}

The update() method... Public final void update(Graphics g){ if(doubleBuffered){ paintFrame(backGraphics); g.drawImage(backImage,0,0,this); }else super.update(); } Public void paint(Graphics g){ paintFrame(g); }

The constructors Protected DBAnimationApplet(boolean db){ this.doubleBuffered = db; } Protected DBAnimationApplet(){ this.doubleBuffered = true; }

How do we use the DoubleBuffered Animation Applet?  1. Extend the DBAnimationApplet  2. Use the appropriate constructor to choose doublebuffer/not doublebuffer  3. Define the abstract paintFrame() method ...and that is it

Design by Abstraction  Designing generic components Reusable Extensible  Using: Abstract classes Interfaces Design patterns (and idioms) Without having to modify the code!

Example  An applet for plotting functions.  Should be easy to adapt for different functions  A generic applet that captures the common code

A generic plotter

The class Plotter Factoring by inheritance: public class Plotter extends Japplet public init : read parameters form html-file size parameters scaling parameters public paint : draw the coordinate axis draw the function graph in the interval given by the parameters!

The class Plotter What function? A function can be implemented by a method: public double func(double x){???} better: protected abstract double func(double x); which also forces the class to be abstract! It has to be extended for instances to be allowed! public abstract class Plotter extends Japplet

Plotting a function public class CosPlotter extends Plotter{ protected double func(double x){ return Math.cos(x); } Or public class SinPlotter extends Plotter{ protected double func(double x){ return Math.sin(x); }

Looking inside Plotter public abstract class Plotter extends Japplet{ private int w,h,xorigin,yorigin,xratio,yratio; private Color color = Color.black; protected abstract double func(double x); public void init(){ w = Integer.parseInt(getParameter(“width”)); h = Integer.parseInt(getParameter(“height”)); xorigin =... yorigin =... xratio =... yratio =... }

Looking inside Plotter public void paint(Graphics g){ drawCoordinates(g); plotFunction(g); } private void plotFunction(Graphics g){ for(int px = 0; px < dim.width; px ++){ try{ double x =(double)(px - xorigin)/(double)xratio; double y =func(x); int py = yorigin - (int)(y * yratio); g.fillOval(px-1,py-1,3,3); }catch(Exception e){} }

Factoring by inheritance Design pattern: TEMPLATE In the abstract class a template method calls ( plotFunction ) a hook method (func) that is left abstract!

Factoring by delegation

The class MultiPlotter public class MultiPlotter extends Japplet public init as before: read parameters. public paint as before: draw coordinates and the function in the interval given by the params. What function? A function can be implemented by an object that can do apply(double)! private Function f;

The interface Function We need a type Function for objects that can do double apply(double x) Now, we want this method to behave sometimes as cos, sin, or other function. We leave the implementation thus unspecified and just define public interface Function{ public double apply(double x); }

MultiPlotter  We can now plot a number of functions in the same applet (By having an array with Function s and an array with Color s)  We have to offer a method to add functions  We have to decide when/how to add the functions.

MultiPlotter  Let the class that adapts (extends) MultiPlotter define init() where  Parameters are read  Functions are added (using the method for doing so)  A bad thing: what happens if the programmer forgets to read the parameters?  Let init be a template method and use a hook to allow the new class to add functions!

MultiPlotter public abstract class MultiPlotter extends Japplet private int w, h, xorigin, yorigin, xratio, yratio; private Function [] functions; private Color [] colors; public final void init(){ /* read parameters; */ functions = new Function[max]; colors = new Color[max]; initMultiPlotter(); } protected abstract void initMultiPlotter();

MultiPlotter.plotFunctions private void plotFunctions(Graphics g){ for(int i = 0; i < numOfFunctions; i++){ g.setColor(colors[i]); for(int px = 0; px < dim.width; px ++){ try{ double x = (double)(px... double y = functions[i].apply(x); int py = yorigin -... g.fillOval(px-1,py-1,3,3); } catch (Exception e){} }

Plotting sin and cos public class SinCos extends MultiPlotter{ protected void initMultiPlotter(){ addFunction(new Sin(),Color.red); addFunction(new Cos(),Color.blue); } public class Cos implements Function{ public double apply(double x){ return Math.cos(x); }

Plotting Sin and Cos

Design Guidelines  Maximize adaptability (extensibility) The more extensible a component is, the better chances it will be reused  Minimize risk for missuse! (make init a final method and force the definition of initMultiPlotter instead of allowing for redefinition of init!)

Factoring by delegation Design pattern: STRATEGY In the context (the general class, MultiPlotter ) one or more instances of strategy objects ( Function[] functions ) some concrete strategies (classes implementing the strategy: Cos, Sin)