java unit 5: applets and graphics web pages and viewing applets

24
Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Upload: emil-mathews

Post on 24-Dec-2015

237 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Java Unit 5: Applets and GraphicsWeb Pages and Viewing Applets

Page 2: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

What is an Applet? An applet is a special Java program that

can be included on a web page. The inclusion of an applet would be

written in the html file of the page. Most applets are graphical in nature. Here, we’re going to take a very basic

approach to applets and graphics.

Page 3: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

HTML!? Oh noooooo…. We don’t have to become masters of HTML

in order to write an applet page. In fact, this is so basic, we could write this in Notepad.

The code for the inclusion of an applet would go something like this:

<html><applet code="MyClass.class"

width="500" height="500"></applet>

</html>

Page 4: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

No HTML? No Problem! For the purposes of debugging and

testing out applets, there is an applet viewer available.

Whenever an applet class is present, the applet viewer will run that code when you press the run button in Eclipse.

Page 5: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Java Unit 5: Applets and GraphicsGeometric and Graphics classes

Page 6: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Graphics Java includes various Graphics classes.

Ex: Point, Rectangle2D We need to keep in mind that since Java

has evolved over time, there are newer and older versions of classes, each with differing features.

Page 7: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

It’s a starting… POINT! The Point class would be a useful place

to start. There are four subclasses to this: Point,

Point2D, Point2D.Double, Point2D.Float All the Point classes come from

java.awt.geom.Point2D. For all of these classes, Point2D is a

superclass.

Page 8: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Point Hierarchy

Page 9: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Superclasses? Inheritance? Superclasses can be thought of as a

class that other ‘subclasses’ model themselves off of. That is, the subclass ‘inherits’ from the superclass.

A superclass provides all of its variables and methods to its subclasses.

A subclass can also have its own unique variables and methods in addition to the ones in the superclass.

Page 10: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Point and Inheritance It can be said that all of these Point

classes are in an inheritance hierarchy. The subclasses of Point2D are reliant on

Point2D’s constructor, and they also inherit all of Point2D’s variables and methods.

Page 11: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Point and Point2D Historically, ‘Point’ came before

‘Point2D’. This is an example of new classes

replacing old ones. When using Point2D.Double and

Point2D.Float, you must use their full names. Besides, referring to Point2D.Double as

‘Double’ would create ambiguity with the ‘Double’ class.

Page 12: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Using Points Import into the code:

import java.awt.geom.Point2D; Use in the code:

Point2D.Double myPoint = new Point2D.Double();

This kind of pattern of simple geometric classes integrated into more complex classes is repeated in Java.

Page 13: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

But Points have zero dimensions! You cannot literally draw points, but you

would use it to draw other shapes. Circles, for a basic point representation. You could also draw a line using two

points, despite a line having only one dimension.

Page 14: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Graphics Like with Point and Point2D, Java has

evolved and now has a Graphics and Graphics2D class.

Graphics2D is the newer class which we will be using.

In order for older applications to continue working, the inner machinery still relies on Graphics.

Page 15: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Using Graphics2D Suppose you have a paint(Graphics g)

method, which absolutely has to take in a Graphics as a parameter.

We get a Graphics2D from it by casting like so: Graphics2D g2D = (Graphics2D) g;

Page 16: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Java Unit 5: Applets and GraphicsApplets

Page 17: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

LineApplet.javaimport javax.swing.JApplet;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.geom.Point2D;import java.awt.geom.Line2D;

Page 18: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

LineApplet.javapublic class LineApplet extends JApplet {

public void paint(Graphics g) {Graphics2D g2 = (Graphics2D) g;Point2D.Double start = new

Point2D.Double(10, 20);Point2D.Double end = new

Point2D.Double(110, 120);Line2D.Double myLine = new

Line2D.Double(start, end);g2.draw(myLine);

}}

Page 19: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

LineApplet.java

Page 20: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

The Cartesian Grid: Defied The coordinate system in Java is a little

different from what you might expect. The X value increases from left to right, so

higher values will appear further to the right.

However, the Y value increases from top to bottom, so higher values of Y will appear further down instead of up.

So, our ‘starting point’ (0, 0) for drawing is in the upper-left hand corner.

Page 21: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Looking at the code public class LineApplet extends JApplet

Here, our ‘LineApplet’ is said to extend ‘JApplet‘, or it inherits from JApplet. JApplet is a superclass which all applets we build will subclass from.

public void paint(Graphics g) Rather than having a main() method,

applets have a paint() method, which will be used to draw onto the screen.

Page 22: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Looking at the code Graphics2D g2 = (Graphics2D) g;

An applet’s paint method has to use a Graphics for its parameter. We use Graphics2D instead, so we cast it into another Graphics2D variable.

Point2D.Double start = new Point2D.Double(10, 20);Point2D.Double end = new Point2D.Double(110, 120); Start and end points are made using numerical

input for their x and y values.

Page 23: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Line2D.Double myLine = new Line2D.Double(start, end); A new line is constructed using the start

and end points. g2.draw(myLine);

This draws the line that we have defined for it.

It turns out that draw() takes in many different parameters, and a line object is just one of them.

Page 24: Java Unit 5: Applets and Graphics Web Pages and Viewing Applets

Producing the results Unlike a regular application, applets use a

paint() method instead of a main() method. If a class extends JApplet, Java knows that it

has to have a paint() method, so the system calls that method, and passes in the Graphics object.

Once the paint() method is run, our ‘g2’ object causes things to appear on screen. It would be useful to think of Graphics and

Graphics2D as pens which we use to draw.