Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/

Slides:



Advertisements
Similar presentations
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.
Advertisements

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
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
Programming and Problem Solving With Java Copyright 1999, James M. Slack Applets What is an Applet? Applet Parameters Graphics in Applets Other Applet.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
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.
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:
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Applet class The Applet class provides the essential framework that enables applets to be run by a web browser Applet do not have main method Applet depend.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lectures 18 : Threads King Fahd University of Petroleum & Minerals College of Computer Science.
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.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
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.
OOP Languages: Java vs C++
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.
Internet Software Development Applets Paul J Krause.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Introduction to Applets Course Lecture Slides 29 th July 2010.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
1 Web Based Programming Section 8 James King 12 August 2003.
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 Lecture 5: Refactoring by Inheritance and Delegation - A simple Design Pattern for animation applets, A Generic Function Plotter.
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.
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,
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
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
Chapter 5 Introduction to Defining Classes
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.
Introduction to Object-Oriented Programming Lesson 2.
Creating a Java Application and Applet
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
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.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Multithreaded applets
Chapter 19 Java Never Ends
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
UNIT-5.
Java applets 1/3/2019.
Java Programming COMP-417 Applet
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
APPLET PROGRAMMING.
Presentation transcript:

Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/

Last time  Definition of Design Patterns  The Singleton pattern  The Iterator/Enumeration Pattern  The Collections Framework Data structures, Iterator, Enumeration  Abstract Coupling Explanation and definition of...

Shadowing of variables Public class { int myVariable;.... public doStuff(int test){ int myVariable; myVariable = test; this.MyVariable = test; } Inner scope myVariable shadows the outer scope myVariable global scope local scope

Design rule using Shadowing  In general - avoid using identical names for variables  For critical variables in sections where something unexpected might happen that would leave variables in an inconsistent state  An example could Parallel tasks in multi threaded environments accessing a global variable Unexpected exception  Otherwise Shadowing should be avoided  Should not be needed in properly written programs

Generics  Generics is a new feature in java 1.5  Avoids the need for type casts when working with collections  Usually the programmer knows what type is contained in a collection  Benefits: Type checking can be done at compile time and errors avoided during run time Reduces ”type casting clutter” in the code  Let’s see an example...

Refactoring  Refactoring is a technique that can be used to collect identical parts into a generic component  What is a generic component? Classes with general conent that can be extended, adapted and reused in different programs Can be reused without having to modify the current source code

Refactoring of methods Class ComputeThings { public void computeMany1(){ anything(); compute1(); compute2(); compute3(); } public void computeMany2(){ something(); compute1(); compute2(); compute3(); } These parts are similar....

Refactoring of methods Class computeThings { public void computeMany1(){ anything(); computeAlot(); } public void computeMany2(){ something(); computeAlot(); } public void computeAlot(){ compute1(); compute2(); compute3(); }

Refactoring of methods  Benefits with method refactoring  Reduces lines of written code  Changes can be done more safely without having to do the same changes at several places

Refactoring by Inheritance Class computeStuff1{ superCompute1(){ compute1(); compute2(); compute3(); }.... } Class computeStuff2{ somethingElse(){ compute1(); compute2(); compute3(); }... } Both classes uses identical code sections

Refactoring by Inheritance Class ComputeStuff1 extends Common{... superCompute1(){ super.computeAlot(); }.... } Class ComputeStuff2 extends Common{... somethingElse(){ super.computeAlot(); }... } Class Common{... computeAlot(){ compute1(); compute2(); compute3(); }... }

Refactoring by Delegation  Instead of inheriting factorized code  Let the object that collects the commonly recurring code be separate  Can be accessed by: A reference to the object Declaring the methods static  Example: The classes Sorting and Helpers in the laboratiorial exercise

Refactoring by Delegation Class ComputeStuff1{ Compute compVar; superCompute1(){ compVar.computeAlot(); }.... } Class ComputeStuff2{... somethingElse(){ compVar.computeAlot(); }... } Class Compute{... computeAlot(){ compute1(); compute2(); compute3(); }... }

Support in Netbeans  Refactor Classes and Interfaces Renames the class and updates all references in project to that class  Encapsulation of fields Create accessors and mutators (”setters” and ”getters” in Netbeans) updates all references to that field or method

The Applets Design Pattern  Java applets can be embedded in web pages, they are not standalone applications  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

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 that is implemented by a class that should run in a Thread  Classes that implements Runnable interface 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 template that can be reused for a recurring problem” It should be possible to customize and adapt for a specific problem  Commonly, applets produce some graphical output that changes without interaction from the user (animation)  Using the Animation Applet Idiom, we can extend AnimationApplet and just redefine the paint method

Double buffering  If painting directly on the screen, it will ”flicker”  Double buffering can be used to solve this problem  Double buffering means that we first invisibly draw each part of the screen in the background (memory)  Then this picture can be drawn entirely at once

Double buffering  When calling repaint(); it will in turn call the update(); method  update(); will clear the screen using the background color and call the paint method paint(Graphics g);  Solution: To avoid the the ”flicker”, we simply need to override the update method!  Instead of clearing, let update(); paint the double 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.  It seem very similar to the Animation Applet  In fact, we can extend and reuse the Animation Applet code!

DoubleBuffered Animation Applet – Refactoring by Inheritance 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  Extend the DBAnimationApplet  Use the appropriate constructor to choose doublebuffer/not doublebuffer  Define the abstract paintFrame() method ...and that is it