Enahnced Digital Clock Applet Setting applet parameters in the web page. The applet tag in HTML: <applet code=DigitalClock2.class width=250 height=80>

Slides:



Advertisements
Similar presentations
Chapter 13 Graphics.
Advertisements

Custom Painting Gestione della Grafica customizzata Vedi anche:
Applets and Graphics.
Chapter 15 Graphics. To paint, you need to specify where to paint. Each component has its own coordinate system with the origin (0, 0) at the upper-left.
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Made with love, by Zachary Langley Applets The Graphics Presentation.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
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.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Jan AWT Widgets Yangjun Chen Dept. Business Computing University of Winnipeg.
Graphics Programming With Applets Feb 23, Applets There are three different types of executable java code. –Standalone application, which has main()
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
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.
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.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
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.
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.
Drawing pictures with Java. JFrame: the basic Java window The swing package contains classes, objects and methods that can be used to create a consistent.
1 Features of Java CS 3331 Fall Outline  Abstract class  Interface  Application --- animation applets.
Chapter 16: Graphics. Objectives Learn about the paint() and repaint() methods Use the drawString() method to draw String s using various fonts and colors.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Applets Java API.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
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.
JAPPLET.
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
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.
Object Oriented Programming Lecture 5: Refactoring by Inheritance and Delegation - A simple Design Pattern for animation applets, A Generic Function Plotter.
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.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
1 Windows program example import java.awt.*; import java.awt.event.*; public class wpexample extends Frame { public wpexample(String title) { super(title);
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Features of Java CS 3331 Sections and 5.5.
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
Your name CSCI/CMPE 3326 Object-Oriented Programming in Java Dongchul Kim Department of Computer Science University of Texas – Pan American 1.Applet.
Creating Graphics in Java CSE301 University of Sunderland Harry R Erwin, PhD.
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
1 Contents Introduction Applet Vs Application Security Restrictions on Applet A simple example “Hello World!” applet Compiling & Running Applet HTML document.
1 Graphics, Fonts and Color Chapter 9. 2 What is in this chapter: l Graphics class and coordinates l graphics primitives (lines,rectangles,ovals and arcs)
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
1 Java Applet Basics Chapter Eight. 2 Applets vs. Applications l Applications: Stand alone Java programs run by interpreter l Applets WWW browser embedded.
Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;
Java Applets. Introduction to Java and Java Applets Java applications Run in stand-alone mode No additional software required (such as a Web browser)
Applet: An applet is a java program that is transmitted over the network from the server to client & executed within clients browser. Applets are used.
Intro to Graphics from Chapter 2 of Java Software Solutions
Sachin Malhotra Saurabh Choudhary
Topic: Applets Course : JAVA PROGRAMMING Paper Code: ETCS-307
Stock Market Quotes – Socket Version
Fundamental of Java Programming
Java Applets.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
Java applets 1/3/2019.
APPLET PROGRAMMING.
Presentation transcript:

Enahnced Digital Clock Applet Setting applet parameters in the web page. The applet tag in HTML: <applet code=DigitalClock2.class width=250 height=80> Syntax:...

Getting Applet Parameters import java.awt.Color; public class DigitalClock2 extends DigitalClock { public void init () { String param = getParameter("color"); if ("red".equals(param)) { color = Color.red; } else if ("blue".equals(param)) { color = Color.blue; } else if ("yellow".equals(param)) { color = Color.yellow; } else if ("orange".equals(param)) { color = Color.orange; } else { color = Color.green; }

The java.awt.Graphics Class Represent Graphics Context. A graphics context is an abstraction of various drawing surfaces: screen printer off-screen image (an image stored in memory) Provide a rich set of graphics methods. drawString() drawLine() drawArc() fillArc() drawOval() fillOval() drawPolygon() fillPolygon() drawRect() fillRect() drawRoundRect() fillRoundRect()

The java.awt.Graphics Class (cont'd) Methods: setColor(color) set the current color setFont(font) set the current font setPaintMode() set the paint, or overwrite mode setXORMode(color) set the XOR mode getColor() get the current color getFont() get the current font getFontMetrics() get the font metrics of the current font getFontMetrics(font) get the font metrics for the specified font

The java.awt.FontMetrics Class Methods: getAscent() getDescent() getHeight() getLeading() stringWidth(s)

Scrolling Banner Applet public class ScrollingBanner extends java.applet.Applet implements Runnable { public void init() {... } public void paint(Graphics g) {... } public void run() {... } public void start() {... } public void stop() {... } }

Field Declarations protected Thread bannerThread; protected String text; protected Font font = new java.awt.Font("Sans-serif", Font.BOLD, 24); protected int x, y; protected int delay = 100; protected int offset = 1; protected Dimension d;

Initialization public void init() { // get parameters "delay" and "text" String att = getParameter("delay"); if (att != null) { delay = Integer.parseInt(att); } att = getParameter("text"); if (att != null) { text = att; } else { text = "Scrolling banner."; } // set initial position of the text d = getSize(); x = d.width; y = font.getSize(); }

Paint the Current Frame public void paint(Graphics g) { // get the font metrics to determine the length of the text g.setFont(font); FontMetrics fm = g.getFontMetrics(); int length = fm.stringWidth(text); // adjust the position of text from the previous frame x -= offset; // if the text is completely off to the left end // move the position back to the right end if (x < -length) x = d.width; // set the pen color and draw the background g.setColor(Color.black); g.fillRect(0,0,d.width,d.height); // set the pen color, then draw the text g.setColor(Color.green); g.drawString(text, x, y); }

The start(), stop(), and run() Methods public void start() { bannerThread = new Thread(this); bannerThread.start(); } public void stop() { bannerThread = null; } public void run() { while (Thread.currentThread() == bannerThread) { try { Thread.currentThread().sleep(delay); } catch (InterruptedException e){} repaint(); }

How to Avoid Flickering?  Flickering is caused by repaint()  repaint() calls the update() method.  The default update() method does the following:  paint the whole area with the background color;  set the foreground color;  call the paint() method.  The update() method is also called by the system to update windows. Á Solution:  override the update() method  use an off-screen image

Using An Off-Screen Image A.k.a. double-buffering import java.awt.*; public class ScrollingBanner2 extends ScrollingBanner { protected Image image; // The off-screen image protected Graphics offscreen; // The off-screen graphics public update(Graphics g) {... } public paint(Graphics g) {... } }

Using An Off-Screen Image(cont'd) public void update(Graphics g) { // create the offscreen image if it is the first time if (image == null) { image = createImage(d.width, d.height); offscreen = image.getGraphics(); } // draw the current frame into the off-screen image // using the paint method of the superclass super.paint(offscreen); // copy the off-screen image to the screen g.drawImage(image, 0, 0, this); } public void paint(Graphics g) { update(g); }

Animation Applet Idiom Category Behavioral implementation idiom. Intent For an applet to continuously update its appearance without user input or intervention. Also known as Active Applet. Applicability Use the Animation Applet Idiom to animate dynamic processes.

Animation Applet Idiom (cont'd) public class AnimationApplet extends java.applet.Applet implements Runnable { Thread mainThread = null; int delay; public void start() { if (mainThread == null) { mainThread = new Thread(this); mainThread.start(); } } public void stop() { mainThread = null; }

Animation Applet Idiom (cont'd) public void run(){ while (Thread.currentThread() == mainThread) { repaint(); try{ Thread.currentThread().sleep(delay); } catch(InterruptedExceptione){} } } public void paint(java.awt.Graphics g) { }

Another Applet -- Bouncing Ball import java.awt.*; public class BouncingBall extends java.applet.Applet implements Runnable { protected Color color = Color.green; protected int radius = 20; protected int x, y; protected int dx = -2, dy = -4; protected Image image; protected Graphics offscreen; protected Dimension d; //... }

Bouncing Ball (cont'd) public void init() { String att = getParameter("delay"); if (att != null) { delay = Integer.parseInt(att); } d = getSize(); x = d.width * 2 / 3 ; y = d.height - radius; }

Bouncing Ball (cont'd) public void update(Graphics g) { // create the off-screen image buffer // if it is invoked the first time if (image == null) { image = createImage(d.width, d.height); offscreen = image.getGraphics(); } // draw the background offscreen.setColor(Color.white); offscreen.fillRect(0,0,d.width,d.height);

Bouncing Ball (cont'd) (method update() continued.) // adjust the position of the ball // reverse the direction if it touches // any of the four sides if (x d.width - radius) { dx = -dx; } if (y d.height - radius) { dy = -dy; } x += dx; y += dy;

Bouncing Ball (cont'd) (method update() continued.) // draw the ball offscreen.setColor(color); offscreen.fillOval(x - radius, y - radius, radius * 2, radius * 2); // copy the off-screen image to the screen g.drawImage(image, 0, 0, this); } public void paint(Graphics g) { update(g); }

Bouncing Ball (cont'd) // The animation applet idiom protected Thread bouncingThread; protected int delay = 100; public void start() { bouncingThread = new Thread(this); bouncingThread.start(); } public void stop() { bouncingThread = null; }

Bouncing Ball (cont'd) public void run() { while (Thread.currentThread() == bouncingThread) { try { Thread.currentThread().sleep(delay); } catch (InterruptedException e){} repaint(); }