Java Unit 5 Applets and Graphics Web Pages

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="My. Class. 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 Ex: We includes various Graphics classes. Point, Rectangle 2 D 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, Point 2 D. Double, Point 2 D. Float All the Point classes come from java. awt. geom. Point 2 D. For all of these classes, Point 2 D is a superclass.

Point Hierarchy

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 Point 2 D are reliant on Point 2 D’s constructor, and they also inherit all of Point 2 D’s variables and methods.

Point and Point 2 D Historically, ‘Point’ came before ‘Point 2 D’. This is an example of new classes replacing old ones. When using Point 2 D. Double and Point 2 D. Float, you must use their full names. Besides, referring to Point 2 D. Double as ‘Double’ would create ambiguity with the ‘Double’ class.

Using Points Import into the code: import Use java. awt. geom. Point 2 D; in the code: Point 2 D. Double my. Point = new Point 2 D. 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 Point 2 D, Java has evolved and now has a Graphics and Graphics 2 D class. Graphics 2 D 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 Graphics 2 D Suppose you have a paint(Graphics g) method, which absolutely has to take in a Graphics as a parameter. We get a Graphics 2 D from it by casting like so: Graphics 2 D g 2 D = (Graphics 2 D) g;

Java Unit 5: Applets and Graphics Applets

Line. Applet. java import javax. swing. JApplet; import java. awt. Graphics 2 D; import java. awt. geom. Point 2 D; import java. awt. geom. Line 2 D;

Line. Applet. java public class Line. Applet extends JApplet { public void paint(Graphics g) { Graphics 2 D g 2 = (Graphics 2 D) g; Point 2 D. Double start = new Point 2 D. Double(10, 20); Point 2 D. Double end = new Point 2 D. Double(110, 120); Line 2 D. Double my. Line = new Line 2 D. Double(start, end); g 2. draw(my. Line); } }

Line. Applet. java

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 Line. Applet extends JApplet Here, our ‘Line. Applet’ is said to extend ‘JApplet‘, or it inherits from 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 Graphics 2 D g 2 = (Graphics 2 D) g; An applet’s paint method has to use a Graphics for its parameter. We use Graphics 2 D instead, so we cast it into another Graphics 2 D variable. Point 2 D. Double start = new Point 2 D. Double(10, 20); Point 2 D. Double end = new Point 2 D. Double(110, 120); Start and end points are made using numerical input for their x and y values.

Line 2 D. Double my. Line = new Line 2 D. Double(start, end); A new line is constructed using the start and end points. g 2. draw(my. Line); 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 ‘g 2’ object causes things to appear on screen. It would be useful to think of Graphics and Graphics 2 D as pens which we use to draw.
- Slides: 24