Java Ch4 Intro To Applets

¡Supera tus tareas y exámenes ahora con Quizwiz!

<Applet> Tag

<APPLET> CODE = Classname.class CODEBASE = dir directory of class file (. => current directory) WIDTH = nnn width of window in pixels HEIGHT = nnn height of window in pixels </APPLET> NOTE: We code and compile the applet in a Java environment. We only use the browser and HTML code to run the applet.

HTML File for FirstApplet

<HTML> <HEAD> <TITLE>My First Applet</TITLE> </HEAD> <BODY> <APPLET CODE="FirstApplet.class" CODEBASE=. WIDTH=400 HEIGHT=300> </APPLET> </BODY> </HTML>

Executing an Applet

A web page tells the browser to run the applet HTML code consists of pairs of tags that specify formatting for the web page. HTML tags come in pairs, data is inserted between start and end tags <HTML> </HTML> start and end of HTML code <HEAD> </HEAD> start and end of header <TITLE> </TITLE> text to display in title bar <BODY> </BODY> start and end of page content <APPLET> </APPLET> start and end of calling applet

Topics

Applet Structure Executing an Applet Drawing Shapes with Graphics Methods Using Colors

The Graphics Class

Browser or applet viewer sends a Graphics object to the paint method The Graphics object represents the applet window, current font, and current color The Graphics class provides methods to draw shapes and text on the window and to set the current color. Graphics is one of the Java predefined classes located in package java.awt. import java.awt.Graphics;

static Color Constants table0405 Custom Colors

Colors consist of red, green, and blue components (RGB). Example: Color green = new Color( 0, 255, 0 ); See Example 4.9 AstronautWithColor.java

Drawing a Line

Example: g.drawLine( 50, 100, 150, 100 ); (50, 100) (150, 100) See Example 4.5 LineDrawingApplet.java

Drawing the Polygon

Example: g.drawPolygon( p ); The polygon is drawn by connecting the points of the Polygon in the order they were added, then connecting the last point to the first. These methods are in the Graphics class. See Example 4.7 DrawingPolygons.java

Displaying Text

Example: g.drawString( "Hello", 100, 200 ); Hello (100, 200) See Example 4.4 DrawingTextApplet.java

Setting the Current Color

Graphics class. Example: g.setColor( Color.RED ); Color class defines colors using an RGB (Red, Green, Blue) system. Any RGB color is considered to be composed of red, green and blue components; the higher the value, the higher the concentration of that component in the color. The Color class provides a set of static Color constants representing 13 common colors.

Executing an Applet

If HTML file is named FirstApplet.html, you can execute the applet using this command: appletviewer FirstApplet.html Many IDEs automatically create and launch the HTML file for running an applet

Subclass and Superclass

In Java, classes can be derived from other classes. A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

The Graphics Coordinate System

In order to use methods provided by class Graphics, we need to specify the locations in the window. Locations are expressed using an (x,y) coordinate system. Coordinate (0,0) corresponds to the upper-left corner.

Subclass and Superclass

Inheritance: Each subclass has the characteristics of its superclass and also unique characteristics of its own (specialized behavior). Subclass is more specific than its superclass and represents a more specialized group of objects. With inheritance, programmers save time during program development by reusing proven and debugged high quality software.

Chapter 4

Introduction to Applets and Graphics

Applets

Java applets are run by an internet browser or an applet viewer. Applet viewer in included with the Java Development Kit (JDK) Benefits: adding interactivity to a web page, ease of adding graphics to the program An applet automatically opens a window where your program can draw shapes and texts. In order to understand the structure of applets, first we need to learn the concept of inheritance in Java.

Java Applets

Java applets are subclasses of an existing Java class called JApplet (package javax.swing). Class JApplet provides the basic functionality of an applet. The subclasses created from JApplet inherit all methods defined in JApplet as well as having their own methods. Syntax for declaring a JApplet: public class AppletName extends JApplet

paint method

Java applets don't use main method; instead they use JApplet paint method. paint method is invoked automatically when the browser or applet viewer launches the applet, as well as any time the applet window needs to redraw itself. Since paint method is a method of JApplet (which is the superclass), we use the keyword super to call it. See Example 4.01 ShellApplet.java

Running Applets

Like applications, applets need to be compiled before they are run. Unlike applications, applets do not run standalone. They are designed to run by an applet viewer or an internet browser: An applet viewer is provided as part of JDK and enables us to view the applet without needing to open a web browser. In order to use a web browser to view an applet, we need to use HTML code.

Graphics Class Methods

Methods are available for drawing lines, rectangles, ovals, and other shapes, and for setting the current color All Graphics methods have a void return type, so method calls are standalone statements draw... methods draw an outlined shape fill... methods draw a solid shape

Polygon Constructor and Methods

Polygon Constructor and Methods

Drawing Polygons

Polygons are custom shapes. The Polygon class is in the java.awt class. We define polygons as a set of points. The Polygon class represents a polygon as an ordered set of (x,y) coordinates; each (x,y) coordinate defines a vertex in the polygon. A line, called an edge, connects each (x,y) coordinate to the next one in the set. Finally, there is a line connecting the last (x,y) coordinate to the first one.

Subclass and Superclass

Syntax for creating a subclass: public class subclass extends superclass Keyword extends specifies that subclass is an extension of the superclass and inherits its properties. In a subclass, if we want to call a method of its superclass, we use the keyword super followed by '.' before the method name. super.methodName(arguments list)

Using Color

The Graphics context has a current foreground color All drawing is done in current color; the current color is in effect until changed. The default color is black. To use color, import the Color class from the java.awt package

Drawing Squares and Circles

To draw a square, use drawRect or fillRect with equal values for width and height. To draw a circle, use drawOval or fillOval with equal values for width and height See Example 4.6 ShapeDrawingApplet.java

Drawing An Oval

g.drawOval( x, y, width, height ); drawOval

Drawing A Rectangle

g.drawRect( x, y, width, height ); drawRect

Drawing A Solid Oval

g.fillOval( x, y, width, height ); fillOval

Drawing A Solid Rectangle

g.fillRect( x, y, width, height ); fillRectss

Template for creating Java Applets

import javax.swing.JApplet; import java.awt.Graphics; public class ShellApplet extends JApplet { public void paint(Graphics g) { super.paint(g); // includes graphics code here } }

paint method

paint method takes only one argument. This argument is in fact an object of Graphics class, which represents the graphics context. public void paint(Graphics g) { super.paint(g); // includes graphics code here } JApplet is not in java.lang package; therefore it should be imported: import javax.swing.JApplet;

Subclass and Superclass

subclasses inherit the characteristics (fields and methods) of their superclass. Class Object, located in the java.lang package, is the superclass of all classes in Java (Java predefined classes and user-defined classes). All Classes in Java platform, including Java predefined classes and user defined classes are descendants of class Object. If we draw the hierarchy of the classes in Java platform, class Object will be at the highest level.


Conjuntos de estudio relacionados

ac 612 - accounting master - forecasting and budgeting

View Set

ECO2023 Ch 4 The Market Forces of Supply and Demand

View Set

Match REASONS with EVIDENCE & Counter Claim with Rebuttal

View Set

Chapter 19: Display Technologies

View Set