©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.

Slides:



Advertisements
Similar presentations
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Advertisements

LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
© 2004 Pearson Addison-Wesley. All rights reserved2-1 Introduction to Graphics The last few sections of each chapter of the textbook focus on graphics.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
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 Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
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.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
©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.
COMP 14: Applets June 21, 2000 Nick Vallidis. Announcements zP6 is due Friday.
©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.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
A Simple Applet.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 5 Graphics. Topics Applets Classes used for graphics –Graphics –Point –Dimension –Color.
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.
Drawing pictures with Java. JFrame: the basic Java window The swing package contains classes, objects and methods that can be used to create a consistent.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
Java Applets. Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java.
Applets Java API.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
©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.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java,
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
JAPPLET.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
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.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
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.
Intro to Applets August 19, 2008 Mrs. C. Furman. Java Applets vs. Java Applications Java Applet: a program that is intended for use on the web. Java Applet:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
Graphics & Applets CSC 1051 – Data Structures and Algorithms I
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
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 Contents Introduction Applet Vs Application Security Restrictions on Applet A simple example “Hello World!” applet Compiling & Running Applet HTML document.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
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 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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Intro to Graphics from Chapter 2 of Java Software Solutions
Chapter 02 Data and Expressions.
Chapter 8 Graphics.
Object Oriented Programming
CSC 1051 – Data Structures and Algorithms I
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Graphics Applets By Mr. Dave Clausen
Chapter 10 Graphics.
Chapter 8 Graphics.
Outline Character Strings Variables and Assignment
Presentation transcript:

©2004 Brooks/Cole Applets Graphics & GUIs

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are graphical The user controls what happens when by interacting with components like buttons and menus. Two types of graphical application in java –GUI application –Applet

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Applets A Java applet is a program that is intended to transported over the Web and executed using a web browser –An applet also can be executed using the appletviewer tool of the Java Software Development Kit An applet is embedded into an HTML file using a tag that references the bytecode file of the applet class The bytecode version of the program is transported across the web and executed by a Java interpreter that is part of the browser

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The HTML applet Tag My Applet <applet code="Einstein.class" width=350 height=175>

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Applets The class that defines an applet extends the Applet class –The class inherits all the behavior of the Applet class –You can add whatever specific behavior you need for your applet An applet doesn't have a main method. Instead, there are several instance methods that serve specific purposes –public void paint( Graphics g) –public void init()

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphics Class Frames and Applets have a graphics context associated with them. A Graphics object defines a graphics context on which we can draw shapes and text. The Graphics class is in the java.awt package. The state of a Graphics object includes such properties as height, width, foreground and background color and font. Positions within the Graphics context are measured from the upper left corner and they have units of pixels.

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphics Methods

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Drawing a Line X Y g.drawLine (10, 20, 150, 45); g.drawLine (150, 45, 10, 20); or

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Drawing a Rectangle

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Bounding Rectangles How do we specify the size of a shape that isn't rectangular? A bounding rectangle is the smallest rectangle that completely surrounds the shape. The java.awt.Dimension class can be used to create bounding rectangles that surround shapes. Dimension dim = new Dimension(); dim.width = 40; dim.height = 70;

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Drawing an Oval X Y g.drawOval (175, 20, 50, 80); bounding rectangle

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Drawing an Arc An arc is a part of an oval To draw you need to specify –the bounding rectangle for the oval –the starting angle (measured from the x-axis in degrees –the angle the arc subtends

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Drawing an Arc g.drawArc (75, 20, 50, 80, 90, 90);

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Drawing a String The drawString method can be used to display text in a Graphics context. void drawString( String text, int x, int y; The position of the text relative to x and y is shown below

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Color Class The java.awt.Color class allows us to designate the color of an object. The RGB scheme combines three values ranging from 0 to 255 for red, green, and blue. Color pinkColor; pinkColor = new Color(255,175,175)

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Color Class There are public class constants defined in the Color class for common colors: –Color.black –Color.blue –Color.green –Color.magenta –Color.orange

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Color Class Every drawing surface has a background color g.setBackground(Color.white); Every graphics context has a current foreground color –The most recently set foreground color is what is used for drawing g.setColor(Color.blue); g.drawRect(50, 50, 100, 30);

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 GUI Applications GUI applications usually start with a JFrame –Create a class that extends JFrame Each frame has a content pane which is where the GUI components go –Use a LayoutManager to control the orgranization of components The frame also has a MenuBar

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Typical Swing-Based Containment Hierarchy

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Control Flow in a GUI Application Components used to interact with the program generate Events Your program needs to listen for the events generated by the components –There are a number of different kinds of events –There are Listener interfaces for each type of event

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Figure 11.2: An Event “Triggers” the Initiation of an Event Object

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Various Registration Configurations