Chapter 4: Applets and Graphics 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 4 Applets and Graphics.

Slides:



Advertisements
Similar presentations
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.
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.
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.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Intro to Graphics.
CPSC 2100 University of Tennessee at Chattanooga – Fall 2013 Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 4: Interface Types.
Using Classes Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Not Glasses, Classes!!!
Chapter 3: Implementing Classes Part 1. To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
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.
©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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
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.
©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.
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.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Java Concepts Chapter 2 – Graphical Applications Mr. Smith AP Computer Science A.
Java Unit 5: Applets and Graphics Web Pages and Viewing 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.
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.
©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 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 5 - Graphics.
JAPPLET.
CHAPTER 2 Using Objects. Basic Programming Terminology  Computer program process values.  Numbers (digits)  Words (Strings)  These values are different.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Three: Implementing Classes.
Chapter 3 – Implementing Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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.
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.
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.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
ICOM 4015: Advanced Programming Lecture 3 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Three:
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
Fall 2006Adapted from Java Concepts Companion Slides1 Programming Graphics Advanced Programming ICOM 4015 Lecture 14 Reading: Java Concepts Chapter 5.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
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.
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.
1 Java Applet Basics Chapter Eight. 2 Applets vs. Applications l Applications: Stand alone Java programs run by interpreter l Applets WWW browser embedded.
Chapter 3: Implementing Classes Part 1. To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Lec 15 Writing an Applet Class. Agenda Writing an Applet class Java Graphics class.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Break Time! Graphical User Interface (GUI) NO EXAM….ONLY PROJECT!
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Chapter 2 Not Glasses, Classes!!! Using Classes
User Interface Programming In Java
Chapter 5 Programming Graphics
Chapter Three: Implementing Classes
Java Applet.
Java Applets.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
Chapter Three - Implementing Classes
Fundamental Data Types
Java Applets.
Java Applets.
Java applets 1/3/2019.
Handout-14 Applets and Graphics
Presentation transcript:

Chapter 4: Applets and Graphics 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 4 Applets and Graphics

Chapter 4: Applets and Graphics 2 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Java Programs Console application –Plain looking terminal window Graphical application –User interface components (buttons, text input) Applets –Graphical applications that run inside a web browser.

Chapter 4: Applets and Graphics 3 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 1 A Console Application

Chapter 4: Applets and Graphics 4 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 2 A Graphical Application

Chapter 4: Applets and Graphics 5 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Applets Web server (Store a code) Web pages Browser (downloaded) Applets are programs that run inside a web browser. Advantage: others can access your program –Multiple platform (java bytecode) Disadvantage: download time, security

Chapter 4: Applets and Graphics 6 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 3 Web Browsers Accessing a Web Server

Chapter 4: Applets and Graphics 7 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e HTML Java is an object-oriented programming language. An animation of Harry, the horrible Hamster

Chapter 4: Applets and Graphics 8 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e A Simple Applet – – Here is my first applet –

Chapter 4: Applets and Graphics 9 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e A Simple Applet appletviewer RectangleApplet.html – public calss RectangleApplet extends Applet –{ public void paint(Graphic g) – { … – }

Chapter 4: Applets and Graphics 10 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e A Simple Applet Applets don’t have a main method. The web browser is responsible for starting up the Java virtual machine. Graphics (Object)

Chapter 4: Applets and Graphics 11 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 4 The Rectangle Applet in the Applet Viewer

Chapter 4: Applets and Graphics 12 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 5 The Rectan- gle Applet in a Java 2– Enabled Browser

Chapter 4: Applets and Graphics 13 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Program RectangleApplet.java import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; public class RectangleApplet extends Applet { public void paint(Graphics g) { // recover Graphics2D Graphics2D g2 = (Graphics2D)g; // construct a rectangle and draw it Rectangle cerealBox = new Rectangle(5, 10, 20, 30); g2.draw(cerealBox);

Chapter 4: Applets and Graphics 14 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e // move rectangle 15 units sideways and 25 units down cerealBox.translate(15, 25); // draw moved rectangle g2.draw(cerealBox); }

Chapter 4: Applets and Graphics 15 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Graphical Shapes Ellips Ellipse2D.Double easterEgg = new Ellipse2D.Double(5, 10, 15, 20) Import java.awt.geom.Ellipse; Line2D.Double segment = new Line2D.Double (x1, y1, x2, y2);

Chapter 4: Applets and Graphics 16 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 6 An Ellipse and Its Bounding Box

Chapter 4: Applets and Graphics 17 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Colors RGB Color magenta = new Color(1.0F, 0.0F, 1.0F); P152

Chapter 4: Applets and Graphics 18 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Fonts G2.drawString(“Applet”, 50, 100); final int HUGE_SIZE = 36; String message = “Applet”; Font hugeFont = new Font (“Serif”, Font. Bold, Huge_SIZE); g2.setFont(hugeFont); g2.setColor(Color.pint); g2.drawString(message, 50, 100);

Chapter 4: Applets and Graphics 19 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 7 Basepoint and Baseline

Chapter 4: Applets and Graphics 20 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 8 Common Fonts

Chapter 4: Applets and Graphics 21 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 9 Text Layout

Chapter 4: Applets and Graphics 22 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 10 The Font Applet

Chapter 4: Applets and Graphics 23 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Program FontApplet.java import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Font; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; public class FontApplet extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; // select the font into the graphics context final int HUGE_SIZE = 48; Font hugeFont = new Font("Serif", Font.BOLD, HUGE_SIZE); g2.setFont(hugeFont); String message ="Applet";

Chapter 4: Applets and Graphics 24 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e // create a text layout to measure the string FontRenderContext context = g2.getFontRenderContext(); TextLayout layout = new TextLayout(message, hugeFont, context); // measure the message width and height float xMessageWidth = layout.getAdvance(); float yMessageHeight = layout.getAscent() + layout.getDescent(); // center the message in the window float xLeft = 0.5F * (getWidth()- xMessageWidth); float yTop = 0.5F * (getHeight()- yMessageHeight); float yBase = yTop + layout.getAscent(); g2.drawString(message, xLeft, yBase); }

Chapter 4: Applets and Graphics 25 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Simple Drawings Car drawings

Chapter 4: Applets and Graphics 26 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 11 A Graphical Applet That Draws a Sketch of a Car

Chapter 4: Applets and Graphics 27 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Program CarDrawer.java import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; public class CarDrawer extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Rectangle body = new Rectangle(100, 110, 60, 10); Ellipse2D.Double frontTire = new Ellipse2D.Double(110, 120, 10, 10); Ellipse2D.Double rearTire = new Ellipse2D.Double(140, 120, 10, 10);

Chapter 4: Applets and Graphics 28 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Point2D.Double r1 = new Point2D.Double(110, 110); // the bottom of the front windshield Point2D.Double r2 = new Point2D.Double(120, 100); // the front of the roof Point2D.Double r3 = new Point2D.Double(140, 100); // the rear of the roof Point2D.Double r4 = new Point2D.Double(150, 110); // the bottom of the rear windshield Line2D.Double frontWindshield = new Line2D.Double(r1, r2); Line2D.Double roofTop = new Line2D.Double(r2, r3); Line2D.Double rearWindshield = new Line2D.Double(r3, r4);

Chapter 4: Applets and Graphics 29 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e g2.draw(body); g2.draw(frontTire); g2.draw(rearTire); g2.draw(frontWindshield); g2.draw(roofTop); g2.draw(rearWindshield); g2.drawString("JavaMobile 1.2ti", 100, 150); }

Chapter 4: Applets and Graphics 30 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 12 Using Graph Paper to Find Shape Coordinates

Chapter 4: Applets and Graphics 31 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 13 Diagrams

Chapter 4: Applets and Graphics 32 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 14 Scene

Chapter 4: Applets and Graphics 33 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 15 Manipulated Image

Chapter 4: Applets and Graphics 34 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Reading Text Input JOptionPane calss showInputDialog method Javax.swing package String input = JOPtionPane.shwInputDialog(“Please enter your age:”); int age = Integer.parseInt(input);\

Chapter 4: Applets and Graphics 35 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 16 An Input Dialog

Chapter 4: Applets and Graphics 36 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Program ColorSelect.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; public class ColorSelect extends Applet { public void init() { 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); }

Chapter 4: Applets and Graphics 37 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e public void paint(Graphics g) { final int SQUARE_LENGTH = 100; Graphics2D g2 = (Graphics2D)g; // select color into graphics context g2.setColor(fillColor); // construct and fill a square whose center is // the center of the window Rectangle square = new Rectangle( (getWidth() - SQUARE_LENGTH) / 2, (getHeight() - SQUARE_LENGTH) / 2, SQUARE_LENGTH, SQUARE_LENGTH); g2.fill(square); } private color fillColor; }

Chapter 4: Applets and Graphics 38 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 17 Intersection of a Line and a Circle

Chapter 4: Applets and Graphics 39 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Program Intersect.java import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import javax.swing.JOptionPane; public class Intersect extends Applet { public void init() { String input = JOptionPane.showInputDialog("x:"); x = Integer.parseInt(input); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; double r = 100; // the radius of the circle // draw the circle

Chapter 4: Applets and Graphics 40 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Ellipse2D.Double circle = new Ellipse2D.Double(0, 0, 2 * r, 2 * r); g2.draw(circle); // draw the vertical line Line2D.Double line = new Line2D.Double(x, 0, x, 2 * r); g2.draw(line); // compute the intersection points double a = r; double b = r;

Chapter 4: Applets and Graphics 41 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e double root = Math.sqrt(r * r - (x - a) * (x - a)); double y1 = b + root; double y2 = b - root; // draw the intersection points final double SMALL_CIRCLE_RADIUS = 2; Ellipse2D.Double circle1 = new Ellipse2D.Double( x - SMALL_CIRCLE_RADIUS, y1 - SMALL_CIRCLE_RADIUS, 2 * SMALL_CIRCLE_RADIUS, 2 * SMALL_CIRCLE_RADIUS); Ellipse2D.Double circle2 = new Ellipse2D.Double( x - SMALL_CIRCLE_RADIUS, y2 - SMALL_CIRCLE_RADIUS, 2 * SMALL_CIRCLE_RADIUS, 2 * SMALL_CIRCLE_RADIUS);

Chapter 4: Applets and Graphics 42 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e g2.draw(circle1); g2.draw(circle2); // label the intersection points String label1 = "” + y1; String label2 = "” + y2; g2.drawString(label1, (float)x, (float)y1); g2.drawString(label2, (float)x, (float)y2); } private double x; }

Chapter 4: Applets and Graphics 43 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Program Phoenix.java import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; public class Phoenix extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; month = 0; units = new UnitConverter(0, 12, 0, 40, getWidth(), getHeight()); final int JAN_TEMP = 11; final int FEB_TEMP = 13; final int MAR_TEMP = 16; final int APR_TEMP = 20; final int MAY_TEMP = 25; final int JUN_TEMP = 31; final int JUL_TEMP = 33; final int AUG_TEMP = 32; final int SEP_TEMP = 29;

Chapter 4: Applets and Graphics 44 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e final int OCT_TEMP = 23; final int NOV_TEMP = 16; final int DEC_TEMP = 12; drawBar(g2, JAN_TEMP); drawBar(g2, FEB_TEMP); drawBar(g2, MAR_TEMP); drawBar(g2, APR_TEMP); drawBar(g2, MAY_TEMP); drawBar(g2, JUN_TEMP); drawBar(g2, JUL_TEMP); drawBar(g2, AUG_TEMP); drawBar(g2, SEP_TEMP); drawBar(g2, OCT_TEMP); drawBar(g2, NOV_TEMP); drawBar(g2, DEC_TEMP); }

Chapter 4: Applets and Graphics 45 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e public void drawBar(Graphics2D g2, int temperature) { // construct rectangle for this month and temperature Rectangle rect = new Rectangle(month, 0, 1, temperature); // convert to pixel coordinates and draw units.convert(rect); g2.draw(rect); month++; } private int month; private UnitConverter units; }

Chapter 4: Applets and Graphics 46 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 18 Plotting Temperature Data

Chapter 4: Applets and Graphics 47 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Figure 19 A Tic-Tac-Toe Board