Applets

  • Uploaded by: api-3814408
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Applets as PDF for free.

More details

  • Words: 1,073
  • Pages: 29
Applets and Graphics

Applet  

 

Graphical Java programs An application designed to be transmitted over the Internet. Run inside web browser or Applet Viewer Platform-neutral

Web Browsers Accessing a Web Page

User enters URL for a web site ……… …..

HTML file sent back from web server ………..

Applet byte code file sent down from server and interpreted by browser ..

Applet Basics 

All applets are subclasses of Applet.



Execution of an applet does not begin at main().



An applet is accessed by an HTML file.



HTML is a ‘mark-up’ language (it adds to text content by marking it up with tags)



Browsers display the text and process (render) the tags

HTML Tag here comes an applet When a browser renders this HTML file, it will display underlined text and the applet whose byte code is in the file “file.class” OR Include an applet tag: /* */

The Applet Class 

The Java class Applet contains all the behaviors (methods), which an applet container (browser) expects to see. (eg. init, start, paint, stop)



When creating an applet class, your class extends the Applet class, and inherits these behaviors.



Your class can then override any of the behaviors it wishes to replace

        

java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet

Applets 

Methods defined in the class Applet:  init(): Applet is being loaded by browser. Used for one-time initialization.  start(): Browser entered page containing applet  stop(): Browser leaves page containing applet  destroy(): Applet is discarded by browser.



paint() is used for drawing on applets. It is a method in the class Component.

Inheriting from above classes 



Some methods in Component: add(), remove() and setLayout(): controls the positions & sizes of components. Applets inherit the drawing and event handling methods from AWT Component class to produce user interfaces. 



Drawing: images, control of color and font. UI components: buttons, etc.. Event handling: detecting & responding to mouse dragging, button pushing, key pressing,..

Component, Container, and Panel 







From AWT Container, applets get methods to hold components & use layout managers. Panels and applets can only be displayed on other graphical surfaces. A panel must be added to another container in order to be displayed. A component is added to a container by using add() from the Container class.

Creating an Applet 

Your Java source file contains code which implements your applet



Compile the source file to produce class file



Make an HTML file with the applet tag that references that class file



Use browser to execute that HTML file OR run appletviewer utility.

An Applet public class MyApplet extends Applet { // called by browser whenever applet needs redrawing public void paint(Graphics g){ // code which draws applet } }

What’s in the Graphics class?? http://java.sun.com/j2se/1.4.2/docs/api/index.html

public class MyApplet extends Applet { public void paint(Graphics g) { g.drawRect(50,50,100,100); //draw rectangle g.fillRect(200,200,300,300); //draw filled in rectangle of default color } }

public class MyApplet extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; // use more sophisticated Java graphics class //need for 2D objects. . . } }

import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; /* */ public class MyApplet extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; // use newest Java graphics class . . . } } and Graphics classes must be imported for Applet

use ….

let’s see what methods Graphics2D has to offer… go to http://java.sun.com/j2se/1.4.2/docs/api/index.html and look at API specification for version 4.2 or above.

Note that Graphics2D extends from Graphics

public void draw (Shape s) When we looked at the specification for Graphics2D class, we saw that the draw method took a Shape parameter .. what is that? let’s look at the specifications for Shape…… (http://java.sun.com/j2se/1.4.2/docs/api/index.html )

Graphical Shapes Shape is an interface. It just provides method signatures. Any class which declares that it implements Shape is of type Shape. import java.awt.Rectangle; public void paint (Graphics g){ Graphics2D g2= (Graphics2D) g; // create the object Rectangle myRec = new Rectangle(50,50,100,300); // now draw it must create a Shape object in order to draw it g2.draw(myRec); }

Some Java 2D Shape classes The java.awt.geom package offers other classes which implement Shape. These shapes can be created with dimensions which are non-integral. Rectangle2D.Double Line2D.Double

Ellipse2D.Double

Point2D.Double

These classes are inner classes, that is why the ‘.’ appears in the class name.

To use these classes you must import them. To import these classes, you import only the outer class: import java.awt.geom.Ellipse2D;

// no .Double

The Line2D class http://java.sun.com/j2se/1.4.2/docs/api/index.html

To use this class you must import it.: import java.awt.geom.Line2D;

Specifying a Line

Create a line object: Line2D.Double myline = Line2D.Double(2.0,5.0,10.0,5.0); To change the line position : myline.setLine( double X1, double Y1, double X1, double Y2);

//draw a square w/ side length 100 … in paint method double xpos = 200; double ypos = 200; // upper left corner int len = 100; Line2D.Double myLine; myLine = new Line2D.Double(xpos, ypos, xpos+len, ypos); g2obj.draw(myLine);

myLine.setLine(xpos+len, ypos, xpos+len, ypos + len); g2obj.draw(myLine); myLine.setLine(xpos+len, ypos+len, xpos, ypos + len); g2obj.draw(myLine); myLine.setLine(xpos, ypos+ len, xpos, ypos); g2obj.draw(myLine);







the square we drew can be ‘redrawn’ by changing the value of xpos and ypos the square we drew can easily ‘change dimension’ by changing ONLY the value of len the square can be repositioned by changing the start point only.

The Graphics class provides: setColor(Color c) setFont (Font font)

Can a Graphics2D class use these methods?? Why or why not??

You can also create your own color objects: Color ( int red, int green, int blue) parameter values between 0 and 255 Color ( int red, int green, int blue , int alpha ) 4th parameter transparency measure 0 - transparent 255 - opaque

also exist with float parameters……. Color ( float red, float green, float blue) Color ( float red, float green, float blue , float alpha ) parameter values between 0.0 and 1.0

Text and Fonts 

g2.drawString("Applet", 50, 100);



A font object has a: face name (Serif, SansSerif, Monospaced, ...) style (Font.PLAIN, Font.BOLD, Font.ITALIC) point size (12 point = normal size)



g2.setFont(new Font("Serif", Font.BOLD, 36));

References  

Java Complete Reference Java docs

Related Documents

Applets
May 2020 9
Applets
November 2019 17
Applets
June 2020 7
Applets - Adv
November 2019 17
Java Applets
October 2019 10
Trigonometric Applets
June 2020 8