Download presentation
Presentation is loading. Please wait.
Published byHenry Mosley Modified over 8 years ago
2
Project 2: Thoughts Problems/Solutions Multiple Vectors vs. One Vector + easy to draw, - z order Shape can draw itself +separation
3
Graphics & Applets Applet Material was modified from: www.devrycols.edu/facstaff/DChampion/dmcAppletsAndGraphics.ppt
4
Graphics Abstract class No instantiation Graphics g = someComponent.getGraphics()
5
Graphics API Graphics (1.1) Available fonts Lines drawn with a single-pixel width Shapes painted solid colors only
6
Java 1.1 Graphics public void paint(Graphics g) { // Set pen parameters g.setColor(someColor); g.setFont(someLimitedFont); // Draw a shape g.drawRect(...); // outline g.drawPolygon(...); // outline g.drawOval(...); // outline... } Example: Graphics 1.1
7
Graphics extended (1.2) Graphics2D Colors and patterns: gradient fills, fill patterns tiled images transparency Local fonts Pen thicknesses, dashing patterns, & segment connection styles Coordinate transformations Rotations & shearing
8
Java 2D: Common Methods public void drawString(String s, flt x, flt y) drawString(s, 2.0f, 3.0f) public void rotate(double theta) rotation of theta radians public void rotate(dbl theta, dbl x, dbl y) theta radians, point of rotation (x, y).
9
Continued public void scale(double xscale, yscale) Values greater than 1.0 expand the axis Values less than 1.0 shrink the axis public void setPaint(Paint paint) Solid Color, a GradientPaint, & TexturePaint public void setStroke(Stroke pen) Line thickness around object
10
Optional: modify drawing parameters g2d.setPaint(fillColorOrPattern); g2d.setStroke(penThicknessOrPattern); g2d.setComposite(someAlphaComposite); g2d.setFont(someFont); g2d.rotate(...); g2d.scale(...); g2d.setTransform(someAffineTransform);
11
Drawing a shape Call paintComponent method of superclass maintains the component look and feel clears off-screen pixmap (Swing implements double buffering) public void paint(Graphics){ super.paint(g); ….
12
Graphics 2D: Cast public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D)g; …stuff here… }
13
Drawing Shape Objects public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; // Assume x, y, and diameter are instance variables. Ellipse2D.Double circle = new Ellipse2D.double(x, y, diameter, diameter); g2d.fill(circle);... } There is double and float…idea being performance
14
Other shapes… Arc2D Ellipse2D GeneralPath, Line2D Rectangle2D, & RoundRectangle2D
15
Complex Constructors public Arc2D.Double( x, y, width, height, startAngle, deltaAngle, closure) A lot of built in functionality and methods ARC2D.OPEN ARC2D.PIE ARC2D.CHORD
16
General paths: Custom Shapes GeneralPath path = new GeneralPath(); path.moveTo(50, 50); path.lineTo(150, 150); path.quadTo(200, 200, 250, 150); path.curveTo(250, 250, 150, 250, 150, 200); path.closePath(); g.draw(path);
17
Creating…
18
Some Examples… Graphics Assortment of shapes Graphics 2D Added functionality Fonts Using fonts
19
APPLETS Compare/contrast applets and applications Explain applet basics Create simple applets Embed applets in web pages
20
APPLETS VS. APPLICATIONS APPLICATION: Free-standing programs Can be run from command line APPLET: Must be run from a Web browser Reference to applet is placed on Web page via special HTML tags Allows applet to be downloaded and executed on system with browser (“local system”). What are the issues running it locally?
21
SUPPORT FEATURES Applets have use of browser-provided features: an existing window, event-handling, graphics context, and a user interface Applications can also create these features, but they are NOT necessary The components and containers available in an applet can be placed in an application, but graphical context is not inherited as it is in an applet.
22
RESTRICTIONS ON APPLETS Reason for restrictions: necessary to protect integrity and security of client system downloading applet Some restrictions : Can’t read/write with client’s file system except for specific folders specified by client. Can’t run any programs on client Can’t run programs native to the local platform (e.g., shared libraries and DLLs).
23
ADDITIONAL PRECAUTIONS The compiler and interpreter have additional security and consistency checks built-in Check to ensure the.class file is a.class file Parity checks, cyclic redundancy checks (CRC) All of the foregoing minimizes accidents and hacking, but is no guarantee.
24
APPLET BASICS Basic features and behaviors of an applet? From the Applet class: any applet is a subclass of this class public class myAppClass extends JApplet {…} NOTE: Java requires the keyword “public” Allows the applet to be used by any agency outside the class, such as the users on the internet.
25
APPLETS VS. APPLICATIONS: EXECUTION APPLICATION: main() APPLET: Upon starting an instance of the applet class is created. It calls a series of methods that have been inherited from java.applet.Applet. Note: 2 versions of the same applet use different instances of the applet’s class so that there is no conflict.
26
THE MAJOR BEHAVIORS/METHODS OF AN APPLET The behavior of Applets is more complex than that of application. An application’s behavior depends just on main(). An applet starts, there is a series of methods that are executed corresponding to the applet’s major behaviors: initialization, starting, stopping, destroying, and painting
27
APPLET INITIALIZATION Occurs just once when applet is loaded Can include creation of objects needed by applet, setting initial state, loading images/fonts To specify initialization behavior, just overload default init() method: public void init(){…} a constructor
28
APPLET STARTING Applet is started after it’s initialized. Starting can occur if applet was previously stopped Example: Reader follows a link off the page and then returns. To define startup behavior, overload the start method: public void start(){…} Useful start actions: start a thread, send a message to a helper object, tell the applet to begin running Note: can start an applet many times, although it’s initialized only once.
29
STOPPING Occurs when reader leaves a page holding a running applet. Can also stop an applet via call, stop() DEFAULT: When reader leaves a page, any running threads continue to run. To specify stop actions overload the following: public void stop(){…} Note: We could override stop and stop these thread when page is left, then restart them when we return to page.
30
DESTROYING Purpose: to free resource just before browser exits or applet finishes Normally, destroy() is not used unless there are resources to free explicitly To control cleanup after an applet override the destroy() method: public void destroy() {…} Similar to automatic garbage Collection
31
PAINTING This method controls the drawing of anything to the screen The paint() method is invoked often: if applet is started; if applet is moved or moved on top of another page item; if one is doing animation. Graphics parm is created and passed to paint() by the browser It is almost guaranteed that you will need to override the paint() method in an applet.
32
A SIMPLE APPLET import javax.swing.JApplet; import java.awt.*; public class ASimpleApplet extends Applet { Font f = new Font("TimesRoman", Font.BOLD, 36); public void paint(Graphics g) { g.setFont(f); g.setColor(Color.red); g.drawString(“A simple applet example!", 5, 50); }//end paint method }//end class
33
COMMENTS ON A SIMPLE APPLET Only one method is overridden: paint() The applet is too simple to need any other method. f is an instance variable for the font to be used inside the graphics context, g. Action of paint(): Font f is added to the graphics context Next, color of the text is set to Color.red--a final class variable for red(i.e., a constant). Finally, the string, “A simple applet example!” is drawn 5 and 50 are the x,y coordinates of the bounding box’s lower left hand corner.
34
EMBEDDING AN APPLET ON A WEB PAGE IDEA: Browser is made to execute an applet via info within special HTML tags on the Web page. EXAMPLE: Can you have images in your applet?
35
COMMENTS ON SIMPLE APPLET’S HTML All info regarding applet is embedded in the and pair Features of the applet tags: CODE: name and extension of class file of applet to be run Note: It’s assumed that class file is in same folder as HTML file CODEBASE: file is in a different folder; it gives the name of the folder. WIDTH and HEIGHT: too small and parts of applet may be lost. Text between APPLET tags: displayed by browsers that cannot work with Java. (HTMLConverter)
36
Demo grade applet What is meant by Signing an applet? How to sign an applet What are the advantages/disadvantages?
37
1.Generate the key to sign the applet keytool -genkey -alias prompt for cert info and keystore,cert password 2.Siginig the jar file jarsigner -signedjar (target) (source) (cert name) Finally, make sure you include archive = "signedjar.jar" in the html file 3.Convert HTML for plugin HtmlConverter
38
Project 3 What is project 2 missing?
39
Next Week… Project 3 in more detail The logic of the mouse listeners Drawing Selection SplitPanes, Tabbed Panes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.