240 likes | 418 Views
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.
E N D
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.
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>
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.
Java Unit 5: Applets and Graphics Geometric and Graphics classes
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.
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.
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.
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.
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.
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.
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.
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.
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;
LineApplet.java import javax.swing.JApplet;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.geom.Point2D;import java.awt.geom.Line2D;
LineApplet.java public 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); }}
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.
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.
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.
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.
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.