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

Slides:



Advertisements
Similar presentations
Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth.
Advertisements

LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
Program: Little Crab Mr Gano.
Java Applets A First Program. Applet Example /* The world’s simplest program, repeated once more in another format in an attempt to turn all of you into.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Java Applets A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
APPLETS CSC 171 FALL 2004 LECTURE 6. APPLETS Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
A Simple Applet.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Swing Graphics ● Empty Swing containers have no visual appearance except for a background color ● Every JComponent must have a paintComponent method that.
PHY281Introduction to JavaSlide 1 Introduction to Java In this section we will learn how how to use Java and write our first Java Applet:  The Java Development.
Java Applets. Applets The term Applet refers to a little application. In JAVA the applet is a java program that is embedded within a HTML document and.
Applets Java API.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
Agenda For Feb Discussion (Assignments & Rubric) 2. PowerPoint Presentation (Drawing Basic Shapes). 4.Read Unit 3 carefully (pages ) then.
Internet Software Development Applets Paul J Krause.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 5 - Graphics.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
JAPPLET.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java.
Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets.
Chapter 4: Applets and Graphics 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 4 Applets and Graphics.
Chapter 4 Fundamental Data Types. Chapter Goals To be able to write simple applets To display graphical shapes such as lines and ellipses To use colors.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Chapter 3 Introduction to Java Applets 1 3 There is no main method in a Java Applet. A Java Applet can only run in a browser. An Applet is run.
1 Applets and Course Evaluation Applet Example - Sailing Applet UML Diagrams Applet Web Page Encoding (HTML) Applet Java Coding – Hello World Course Evaluation.
Java I--Copyright © Tom Hunter. Chapter 3 Introduction to Java Applets.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. In order to display a drawing in a frame, define a class that extends.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance Java Applets –what is an applet vs. an application? –where do applets.
Introduction to Java Simple Graphics. Objects and Methods Recall that a method is an action which can be performed by an object. –The action takes place.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
2.3 The Java API Documentation. What follows are the contents of the Java Application Programming Interface (API) Documentation for the system supplied.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Creating an Object that can draw itself The paint method can ‘draw’ because it is passed a graphics environment as a parameter. If a class method is passed.
9/13: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
Lecture 09 Applets.
Java Graphics CS 2511.
Java Applets.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
The Canvas.
Java I.
UNIT-5.
Java Applets.
Java applets 1/3/2019.
Java Programming COMP-417 Applet
Applet 2019/4/23.
Handout-14 Applets and Graphics
Outline Character Strings Variables and Assignment
Presentation transcript:

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: 

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.

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 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;

Java Unit 5: Applets and Graphics Applets

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); } }

LineApplet.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 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.