Java applet basics, loading & displaying images After this section, you should be able to : –Use the applet tag and applet parameters –Describe what a.

Slides:



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

Made with love, by Zachary Langley Applets The Graphics Presentation.
1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
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.
Standard Components and Their Events GUI interface elements are represented by subclasses of component: –Button class –Label class –TextField class –Checkbox.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Graphics Programming With Applets Feb 23, Applets There are three different types of executable java code. –Standalone application, which has main()
1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
 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.
©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.
1 Review of applets & Computer Graphis GUILecture 6 Review of Applets & Compute Graphics GUI Overview  Introduction to Graphics. Applets: a quick review.
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.
Unit 071 Review of Applets Learning Outcomes oDistinguish between Java Applications and Java Applets. oWrite applet programs that can load images and play.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
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.
Client Side Programming Using Java Applet
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.
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.
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.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Agenda For Feb Finish up Unit 3 Exercises on page Unit 4 Exercises on page 33. Question #2 3. Create a Happy Face Java Applet (due next class).
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
Internet Software Development Applets Paul J Krause.
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Java Applet Presented by Fitsum Okubu. Introduction Introduction Graphics Graphics Methods and Variables Methods and Variables Events Events Decision.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
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.
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.
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.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
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.
25/2/00SEM107 - © Kamin & ReddyClass 9 - HTML - 1 Class 9 - HTML r Creating a personal web page r Structure of HTML documents r HTML tags r The APPLET.
Features of Java CS 3331 Sections and 5.5.
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
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.
Copyright © Curt Hill Applets A different type of program.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Graphics Methods public abstract void drawString(String str, int x, int y): is.
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.
Creating a Java Application and Applet
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
Java Applets. Introduction to Java and Java Applets Java applications Run in stand-alone mode No additional software required (such as a Web browser)
Lecture 09 Applets.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
Java applets 1/3/2019.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Presentation transcript:

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 package is –Import a package containing additional Java types –Describe what the paint method do –Understand the function of Graphics class –Declare a reference to an image object –Load an image in an applet –Code a paint method –Draw an image into a Graphics object

The Applet tag and Applet Parameters The tag is used to add a Java applet to a Web page Applets use applet parameters to customize their behaviour. Applet parameters are specified by using tags, which can only occur between an tag and the closing. The PARAM tag has required modifiers named NAME and VALUE, and it takes the form

An example of an Applet tag with PARAMs Sorry, but your browser doesn't support Java! String display; // Instance variable: message to be displayed. String fontName; // Instance variable: font to use for display. public void init() { String value; value = getParameter("message"); // Get message PARAM, if any. if (value == null) display = "Hello World!"; // default value else display = value; // Value from PARAM tag. value = getParameter("font"); if (value == null) fontName = "SansSerif" else fontName = value; ……………………}

Packages A package is a named group of classes for a common domain: java.lang, java.awt, java.util, java.io. Packages can be imported by other source files making the names available: import java.awt.*; // All classes import java.awt.Image; // One class Explicit type name: java.awt.Image i1; Implicit type name: import java.awt.*; Image i2; The java.lang package is automatically imported by the compiler.

The java.lang package The java.lang package contains more than 20 classes, of which the most useful are System, String, and Math. It also contains the Thread class, the Runnable interface, and the various wrapping classes (such as Integer). 1.java.lang.System class provides the standard streams in, out, and err as public class variables. 2.java.lang.String class contains methods that provide functions similar to C's strxxx functions, including charAt, compareTo, concat, endsWith equals, length, replace, startsWith, subString, toLowerCase, and trim. 3.java.lang.Math class contains a number of mathematical methods such as abs, sin, cos, atan, max, min, log, random, and sqrt. It also contains E and PI as class constants (static final).

Applet class The Applet class, defined in the package java.applet, is really only useful as a basis for making subclasses. An object of type Applet has certain basic behaviours, but doesn't actually do anything useful. It's just a blank area on the screen that doesn't respond to any events. To create a useful applet, a programmer must define a subclass that extends the Applet class. There are several methods in the Applet class that are defined to do nothing at all. The programmer must override at least some of these methods and give them something to do.

Paint method One of the methods that is defined in the Applet class to do nothing is the paint() method. The paint() method is called by the system when the applet needs to be drawn. In a subclass of Applet, the paint() method can be redefined to draw various graphical elements such as rectangles, lines, and text on the applet. The definition of this method must have the form: public void paint(Graphics g) { // draw some stuff } The parameter g, of type Graphics, is provided by the system when it calls the paint() method. In Java, all drawing of any kind is done using methods provided by a Graphics object. There are many such methods and I will introduce them to you soon

Something paint cannot do The paint() method of an applet does not draw GUI components such as buttons and text input boxes that the applet might contain. Such GUI components are objects in their own right, defined by other classes. All component objects, not just applets, have paint() methods. Each component is responsible for drawing itself, in its own paint() method.

Graphics class A Graphics object is usually only obtained as an argument to update and paint methods: public void update(Graphics g) {...} public void paint(Graphics g) {...} The Graphics class provides a set of drawing tools that include methods to draw: rectangles (drawRect, fillRect) ovals (drawOval, fillOval) arcs (drawArc, fillArc) polygons (drawPolygon, fillPolygon) rounded rectangles (drawRoundRect, fillRoundRect) strings (drawString) images (drawImage) For example: g.drawImage(i, 0, 0, this);

“Hello World” applet example import java.awt.*; import java.applet.*; public class HelloWorldApplet extends Applet { // An applet that simply displays the string Hello World! public void paint(Graphics g) { g.drawString("Hello World!", 10, 30); } } // end of class HelloWorldApplet

// An applet that says "Hello World" in a big bold font, // with a button to change the color of the message. import java.awt.*; // Defines basic classes for GUI programming. import java.awt.event.*; // Defines classes for working with events. import java.applet.*; // Defines the applet class. public class ColoredHelloWorldApplet extends Applet implements ActionListener { // Defines a subclass of Applet. The "implements ActionListener" // part says that objects of type ColoredHelloApplet are // capable of listening for ActionEvents. This is necessary // if the applet is to respond to events from the button. int colorNum; // Keeps track of which color is displayed; // 1 for red, 2 for blue, 3 for green. Font textFont; // The font in which the message is displayed. // A font object represent a certain size and // style of text drawn on the screen. Another “Hello World” applet example

public void init() { // This routine is called by the system to initialize // the applet. It sets up the font and initial colors // the applet. It adds a button to the applet for // changing the message color. setBackground(Color.lightGray); // The applet is filled with the background color before // the paint method is called. The button and the message // in this applet will appear on a light gray background. colorNum = 1; // The color of the message is set to red. textFont = new Font("Serif",Font.BOLD,24); // Create a font object representing a big, bold font. Button bttn = new Button("Change Color"); // Create a new button. "ChangeColor" is the text // displayed on the button. bttn.addActionListener(this); // Set up bttn to send an "action event" to this applet // when the user clicks the button. The parameter, this, // is a name for the applet object that we are creating. add(bttn); // Add the button to the applet, so that it // will appear on the screen. } // end init()

public void paint(Graphics g) { // This routine is called by the system whenever the content // of the applet needs to be drawn or redrawn. It displays // the message "Hello World" in the proper color and font. switch (colorNum) { // Set the color. case 1: g.setColor(Color.red); break; case 2: g.setColor(Color.blue); break; case 3: g.setColor(Color.green); break; } g.setFont(textFont); // Set the font. g.drawString("Hello World!", 20,70); // Draw the message. } // end paint()

public void actionPerformed(ActionEvent evt) { // This routine is called by the system when the user clicks // on the button. The response is to change the colorNum // which determines the color of the message, and to call // repaint() to see that the applet is redrawn with the // new color. if (colorNum == 1) // Change colorNum. colorNum = 2; else if (colorNum == 2) colorNum = 3; else colorNum = 1; repaint(); // Tell system that this applet needs to be redrawn } // end init() } // end class ColoredHelloWorldApplet

Loading and drawing images The typical way to load images in Applets is via the getImage() method. Image getImage(URL) // Absolute URL Image getImage(URL, String) // Relative URL For example: Image img = getImage(getDocumentBase(), "x.gif"); This example returns a reference to an image object that is being asynchronously loaded. The getDocumentBase() method returns the address of the current Web site where the applet is being executed. The x.gif is the actual image being loaded After the image is loaded, you would typically render it to the screen in the Applet's paint method using the Graphics method. For example: g.drawImage(img, 0, 0, this); // img is the image that is drawn on the // screen in the 0, 0 position.

Simple applet that loads and draws Image import java.awt.*; public class ImgLoad extends java.applet.Applet { Image i; public void init() { System.out.println("In init"); i = getImage(getDocumentBase(), "Test.gif"); } public void paint(Graphics g) { System.out.println("In paint"); int x = (int)(Math.random() * size().width); int y = (int)(Math.random() * size().height); g.drawImage(i, x, y, this); }