Professional Documents
Culture Documents
06 Applets and Graphics
06 Applets and Graphics
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Taught by the author of Core Servlets and JSP, More Servlets and JSP, and this tutorial. tutorial Available at public venues, Customized or customized Java EE Training: versions http://courses.coreservlets.com/ can be held on-site at your Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. organization. Contact hall@coreservlets.com for details. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Agenda
Applet restrictions Basic applet and HTML template The applet life-cycle Customizing applets through HTML parameters Methods M th d available il bl for f graphical hi l operations ti Loading and drawing images Controlling C t lli i image l loading di
Applet Basics
Customized Java EE Training: http://courses.coreservlets.com/
5
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Open network connections other than to the server from which the applet was loaded
This restriction p prevents applets pp from browsing g behind network firewalls
6
Applets Cannot
Link to client-side C code or call programs i installed ll d on the h browser b machine hi
Ordinary Java applications can invoke locally installed programs (Runtime.exec or ProcessBuilder.start) ( ) as well as link to local C/C++ modules (native methods) These actions are prohibited in applets because there is no way to p these local p programs g p perform are determine whether the operations safe
Applet Template
import java.applet.Applet; import po t ja java.awt.*; a.a t. ; public class AppletTemplate extends Applet { // Variable declarations. public void init() { // Variable initializations initializations, image loading loading, etc etc. } public void paint(Graphics g) { // Drawing operations. }
Eclipse solution
R-click, Run As Java Applet (calls appletviewer) Problem: doesnt use your HTML file, so you cant test if the WIDTH and HEIGHT are correct. Still, convenient.
Appletviewer solution
D During i d development, l t can call ll appletviewer l t i fil ht l or file.html appletviewer http://.../file.html from DOS window.
9
10
Applet Example
import java.applet.Applet; import java.awt.*; /** An applet that draws an image. */ public class JavaJump p p extends Applet pp { private Image jumpingJava; // Instance var declarations here public void init() { // Initializations here setBackground(Color.WHITE); setFont(new Font("SansSerif", Font.BOLD, 18)); jumpingJava = getImage(getCodeBase(), "images/Jumping-Java.gif"); add(new Label("Great Label( Great Jumping Java!")); Java! )); System.out.println("Yow! I'm jiving with Java."); } public bli void id paint(Graphics i t(G hi g) ) { // D Drawing i h here g.drawImage(jumpingJava, 0, 50, this); }
11
12
Note that this is for your d b debugging: i d t expect dont t end d user to ever notice this
13
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Nonstandard behavior in IE
In some versions of Internet Explorer, unlike in Firefox, init is called each time the user returns to the same page, and destroy is called whenever the user leaves the page containing the applet. I.e., applet is started over each time (incorrect behavior!).
16
getParameter
Retrieves the value from the associated HTML PARAM element
getWidth, getHeight
Returns R the h width/height id h/h i h of f the h applet l
getGraphics
Retrieves the current Graphics object for the applet The Graphics object does not persist across paint invocations
17
Asks the browser to retrieve and a display a Web page Can direct page to a named FRAME cell
showStatus
Displays a string in the status line at the bottom of the browser
getForeground, setForeground
Gets/sets foreground g color of applet pp (default ( color of drawing operations)
19
ARCHIVE
Designates g JAR file ( (zip p file with .jar j extension) ) containing g all classes and images used by applet Save considerable time when downloading multiple class files
NAME
Names the applet for interapplet and JavaScript communication
MAYSCRIPT (nonstandard)
21
Applet pp Parameters:
Letting the HTML Author Supply Data
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
23
25
Drawing in Applets
Customized Java EE Training: http://courses.coreservlets.com/
26
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Basic Approach
Ideas
Draw from the paint method (or, later, event handler) Use a Graphics (i.e., pen) object to draw
In paint, paint you are given Graphics object object. In other methods of Applet subclass, call getGraphics() to obtain it.
Syntax
public void paint(Graphics g) { g drawLine(0 0, g.drawLine(0, 0 100 100, 100); } D Draws a li line f from t top-left l ft corner of f applet, l t di diagonally ll down and to the right.
27
drawImage
Draws an image Images can be in JPEG or GIF (including animated GIF) format
29
Drawing Color
setColor, getColor
Specifies the foreground color prior to drawing operation By default, the graphics object receives the foreground color of the window AWT has 16 predefined colors (Color.RED, Color.BLUE, etc.) or create your own color: new Color(r, g, b) Changing the color of the Graphics object affects only the drawing that explicitly uses that Graphics object
To make permanent changes, call the applets setForeground method.
30
Graphics Font
setFont, getFont
Specifies the font to be used for drawing text Determine the size of a character through FontMetrics (in Java 2 use LineMetrics) Setting the font for the Graphics object does not persist to subsequent invocations of paint Set the font of the window (I.e., call the applets setFont method) for permanent changes to the font (e g Times New Standard Java font names: Serif (e.g., Roman), SansSerif (e.g., Arial), Monospaced (e.g., Courier New), Dialog, and DialogInput
31
setPaintMode
Set drawing mode back to normal (versus XOR) Subsequent S b d drawing i will ill use the h normal l foreground f d color l Remember that the Graphics object is reset to the default each time. eac t e. So, no o need eed to call ca g.setPaintMode() g.set a t ode() in paint pa t unless you do non-XOR drawing after your XOR drawing
32
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Drawing Images
Register the Image (from init)
myImage = getImage(getCodeBase(), "file"); myImage = getImage(url);
Y You should h ld declare d l the h image i as an instance i variable i bl so that paint can access it later
private Image myImage;
Loading is done in a separate thread If URL is absolute, then try/catch block is required
g.drawImage(myImage, x, y, window); g.drawImage(myImage, x, y, w, h, window);
34
May draw partial image or nothing at all Use the applet (this) for the window argument
36
Basic Form
try { statement1; statement2; ... } catch(SomeExceptionClass someVar) { handleTheException(someVar); }
38
If no matching , exception p is catch, thrown from method. If method First matching catch has no "throws" fires, so exceptions fires clause, l th then error should be ordered from most specific to finally always most general executed Due to inheritance,
more than one match possible
39
Starts loading image immediately (on separate thread), instead of when needed by drawImage Particularly useful if the images will not be drawn until the user initiates some action such as clicking on a button or choosing g a menu option p Since the applet thread immediately continues execution after the call to prepareImage, the image may not be completely loaded before paint is reached
40
41
42
Applet thread will block until all images are loaded Each image is loaded in parallel on a separate thread
43
44
46
Case 1
47
Case 2
Wrap-Up
Customized Java EE Training: http://courses.coreservlets.com/
48
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Summary
Applet operations are restricted
Applet cannot read/write local files, call local programs, or connect to any host other than the one from which it was loaded Called only when applet loaded, not each time executed This is where you use getParameter to read PARAM data Called each time applet is displayed Coordinates in drawing operations are wrt top-left corner getImage(getCodeBase(), "imageFile") to load drawImage(image, x, y, this) to draw
49
Questions?
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.