Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.

Slides:



Advertisements
Similar presentations
1 Applets Programming Enabling Application Delivery Via the Web.
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Made with love, by Zachary Langley Applets The Graphics Presentation.
1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth.
Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved Static Methods In Java it is possible to declare.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 3.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
Java Applets A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
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.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
1 L44 Introduction to Java Applets. 2 OBJECTIVES  To differentiate between applets and applications.  To observe some of Java's exciting capabilities.
APPLETS CSC 171 FALL 2004 LECTURE 6. APPLETS Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
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.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
Applets Java API.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Internet Software Development Applets Paul J Krause.
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. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
Chapter 4: Applets and Graphics 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 4 Applets and Graphics.
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
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.
Drawing and Filling Geometric Shapes. Java comes with more than just points and lines. Within the Graphics2D class, there are also methods for drawing.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
1 Applets and Course Evaluation Applet Example - Sailing Applet UML Diagrams Applet Web Page Encoding (HTML) Applet Java Coding – Hello World Course Evaluation.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Please open JCreator and follow these steps: 1)Configure  Options 2)JDK Tools 3)Choose Run Applet from drop-down 4)Click Default, then Edit 5)Click the.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
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.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Applet: An applet is a java program that is transmitted over the network from the server to client & executed within clients browser. Applets are used.
Object Oriented Programming
Java Applets.
Interfaces.
Java Applets.
Java Applets.
Presentation transcript:

Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure

Web Browsers Accessing a Web Page User enters URL for a web site …………..

HTML file sent back from web server ………..

Applet byte code file sent down from server and interpreted by browser..

An applet is accessed by an HTML file. HTML is a ‘mark-up’ language (it adds to text content by marking it up with tags) Browsers display the text and process (render) the tags For example, a file might begin with the line: CIS255 Homework Assignment The browser would display: CIS255 Homework Assignment

Other HTML Tags bold, underline Link to another file: Java is an... Include an applet:

here comes an applet When a browser renders this HTML file, it will display underlined text and the applet whose byte code is in the file “file.class”

The Applet Class The Java class Applet contains all the behaviors (methods), which an applet container (browser) expects to see. (eg. init, start, paint, stop) * When creating an applet class, your class extends the Applet class, and inherits these behaviors. Your class can then override any of the behaviors it wishes to replace

Creating an Applet Your Java source file contains code which implements your applet Compile the source file to produce class file Make an HTML file with the applet tag that references that class file Use browser to execute that HTML file OR run appletviewer utility.

An Applet public class MyApplet extends Applet { // called by browser whenever applet needs redrawing public void paint(Graphics g){ // code which draws applet } What’s in the Graphics class??

Applet Coordinate System 0,0 largest_x, 0 0,largest_y largest_x, largest_y x increases  y increases 

public class MyApplet extends Applet { public void paint(Graphics g) { g.drawRect(50,50,100,100); //draw rectangle g.fillRect(200,200,300,300); //draw filled in rectangle of default color }

public class MyApplet extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; // use more sophisticated Java graphics class //need for 2D objects... } }

import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; public class MyApplet extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; // use newest Java graphics class... } } Applet and Graphics classes must be imported for use ….

let’s see what methods Graphics2D has to offer… (go to and look at API specification for version 4.2 or above. Select Graphics2D from class list on left) Note that Graphics2D extends from Graphics

public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; // draw rectangle g2.draw3DRect(500,100,20,30,true); g2.drawString(“my rectangle”,500,20); }

public void draw (Shape s) When we looked at the specification for Graphics2D class, we saw that the draw method took a Shape parameter.. what is that????????????? let’s look at the specifications for Shape…… ( )

Graphical Shapes Shape is an interface. It just provides method signatures. Any class which declares that it implements Shape is of type Shape. For example, the class Rectangle implements Shape, so a Rectangle object is a Shape. import java.awt.Rectangle; …. public void paint (Graphics g){ Graphics2D g2= (Graphics2D) g; // create the object Rectangle myRec = new Rectangle(50,50,100,300); // now draw it* Must create a Shape object in order to draw it g2.draw(myRec); }

The java.awt.geom package offers other classes which implement Shape. These shapes can be created with dimensions which are non-integral. Rectangle2D.Double Ellipse2D.Double Line2D.Double Point2D.Double These classes are inner classes, that is why the ‘.’ appears in the class name. To use these classes you must import them. To import these classes, you import the outer class: import java.awt.geom.Ellipse2D; // no.Double Some Java 2D Shape classes………..

Look at the spec for the Line2D class…. To use this class you must import it.: import java.awt.geom.Line2D; The Line2D class ………..

Specifying a Line Create a line object: Line2D.Double myline = Line2D.Double(2.0,5.0,10.0,5.0); To change the line position : myline.setLine( double X1, double Y1, double X1, double Y2);

Code a paint method which draws a square w/ side length 100, with upper left corner positioned at 200,200

//draw a square w/ side length 100 … in paint method double xpos = 200; double ypos = 200; // upper left corner int len = 100; Line2D.Double myLine; myLine = new Line2D.Double(xpos, ypos, xpos+len, ypos); g2obj.draw(myLine); myLine.setLine(xpos+len, ypos, xpos+len, ypos + len); g2obj.draw(myLine); myLine.setLine(xpos+len, ypos+len, xpos, ypos + len); g2obj.draw(myLine); myLine.setLine(xpos, ypos+ len, xpos, ypos); g2obj.draw(myLine);

Code for Reusability & Readability (our line calls did NOT use hard coded values) * the square we drew can be ‘redrawn’ by changing the value of xpos and ypos * the square we drew can easily ‘change dimension’ by changing ONLY the value of len * the square can be repositioned by changing the start point only

The Graphics class provides: setColor(Color c) setFont (Font font) Can a Graphics2D class use these methods?? (yes) Why or why not?? (because Graphics2D extends from Graphics)

Look at Color class specification……. ( etc.)

Look at Color class……. Fields exist …. Color.blue,Color.cyan these are color objects So they can be used as parameter to setColor g2obj.setColor(Color.blue);

You can also create your own color objects: Color ( int red, int green, int blue) parameter values between between 0 and 255 Color ( int red, int green, int blue, int alpha ) 4 th parameter transpareny measure 0 – transparent opaque also exist with float parameters……. Color ( float red, float green, float blue) Color ( float red, float green, float blue, float alpha ) parameter values between between 0.0 and 1.0 good for randomizing………..

Write applet which draws triangle which moves across screen, changing color as it does… (Class website, tri3.java)

Text and Fonts g2.drawString("Applet", 50, 100); A font object has a: face name (Serif, SansSerif, Monospaced,...) style (Font.PLAIN, Font.BOLD, Font.ITALIC) point size (12 point = normal size) g2.setFont(new Font("Serif", Font.BOLD, 36));

Reading Text Input Can use JOptionPane.showInputDialog method to provide values for applet, BUT.. Programmer cannot predict when/how many times the paint method will execute……… Most likely just want the values read 1 time!

A class constructor executes once when an object is created. An applet is an object, created by the browser (or appletviewer). The applet class can have a constructor. SO… Let constructor set instance variables with input results Then use variables in the paint method

Applet JOptionPane Dialog

File ColorApplet.java import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JOptionPane; // applet that lets a user choose a color for rectangle public class ColorApplet extends Applet { private Color fillColor;

public ColorApplet() { String input; // ask the user for red, green, blue values input = JOptionPane.showInputDialog("red:"); float red = Float.parseFloat(input); input = JOptionPane.showInputDialog("green:"); float green = Float.parseFloat(input); input = JOptionPane.showInputDialog("blue:"); float blue = Float.parseFloat(input); fillColor = new Color(red, green, blue); }

public void paint(Graphics g){ Graphics2D g2 = (Graphics2D)g; final int startX = 50; final int startY = 50; final int sqrLen = 50; // select color into graphics context g2.setColor(fillColor); // construct and draw a filled square Rectangle square = new Rectangle( startX, startY, sqrLen, sqrLen); g2.fill(square); }

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 a graphics environment (ie. object of type Graphics or Graphics2D), then the method can draw also eg.) public void xxx (Graphics g){ g.drawRect(10,10, 200,150); }

The Car Drawer Applet

An object that is going to be drawn should know how to draw itself……… In the CarDrawer applet, the Car class provides a draw method for drawing itself. Of course, in order to draw, the graphics environment is needed. The Car class’ draw method will get this information as a parameter……..

File CarApplet.java import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; //An applet that draws two cars. public class CarApplet extends Applet { public void paint(Graphics g){ Graphics2D g2 = (Graphics2D)g; Car car1 = new Car (100, 100); Car car2 = new Car (200,200); car1.draw(g2); car2.draw(g2); }

File Car.java import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; //A car that can be positioned anywhere on the screen public class Car { private double xLeft; //draw position private double yTop; //…Continue

public Car(double x, double y) { xLeft = x; yTop = y; } //.…Continue Constructor for Car accepts start position for drawing ……

public void draw(Graphics2D g2){ Rectangle2D.Double body; body= new Rectangle2D.Double(xLeft, yTop +10, 60, 10); Ellipse2D.Double frontTire; frontTire = new Ellipse2D.Double( xLeft + 10, yTop + 20, 10, 10); Ellipse2D.Double rearTire = new Ellipse2D.Double (xLeft + 40, yTop + 20, 10, 10); creating the objects to be drawn….. note: all positions are based on the start position values for the figure.

// the bottom of the front windshield Point2D.Double r1 = new Point2D.Double(xLeft + 10, yTop + 10); // the front of the roof Point2D.Double r2 = new Point2D.Double(xLeft + 20, yTop); // the rear of the roof Point2D.Double r3 = new Point2D.Double(xLeft + 40, yTop); // the bottom of the rear windshield Point2D.Double r4 = new Point2D.Double(xLeft + 50, yTop + 10);

//using Point objects to create line (overloaded constructor) Line2D.Double frontWindshield = new Line2D.Double(r1, r2); Line2D.Double roofTop = new Line2D.Double(r2, r3); Line2D.Double rearWindshield = new Line2D.Double(r3, r4); //now, draw the car using the Graphics2D parameter g2.draw(body); g2.draw(frontTire); g2.draw(rearTire); g2.draw(frontWindshield); g2.draw(roofTop); g2.draw(rearWindshield); }