1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 5 - Graphics.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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.
Using Classes Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Not Glasses, Classes!!!
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.
©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.
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.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
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.
©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.
APPLETS CSC 171 FALL 2004 LECTURE 6. APPLETS Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
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.
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.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Swing Graphics ● Empty Swing containers have no visual appearance except for a background color ● Every JComponent must have a paintComponent method that.
Java Concepts Chapter 2 – Graphical Applications Mr. Smith AP Computer Science A.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
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.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CHAPTER 2 Using Objects. Basic Programming Terminology  Computer program process values.  Numbers (digits)  Words (Strings)  These values are different.
Chapter 2 – Using Objects Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
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.
1 Applets and Course Evaluation Applet Example - Sailing Applet UML Diagrams Applet Web Page Encoding (HTML) Applet Java Coding – Hello World Course Evaluation.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
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.
Copyright © 2013 by John Wiley & Sons. All rights reserved. GRAPHICAL USER INTERFACES CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Applets Yong Choi School of Business CSU, Bakersfield.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Fall 2006Adapted from Java Concepts Companion Slides1 Programming Graphics Advanced Programming ICOM 4015 Lecture 14 Reading: Java Concepts Chapter 5.
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.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
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,
Break Time! Graphical User Interface (GUI) NO EXAM….ONLY PROJECT!
Chapter 2 Not Glasses, Classes!!! Using Classes
Chapter 5 Programming Graphics
Java Graphics CS 2511.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Chapter 2 – Using Objects
UNIT-5.
Java Applets.
Handout-14 Applets and Graphics
Presentation transcript:

1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 5 - Graphics

2 Lecture Outline Frame windows Drawing with shapes, colors, and text Programming applets Developing test cases CSC120 — Berry College — Fall 2005

3 Frame Windows Graphical application (GUI = Graphical User Interface) shows information in a frame window To show a frame window in Java Import javax.swing.* package Construct JFrame object Set its size, title, close behavior Make it visible

4 Showing a Frame Window 1. Construct a JFrame object JFrame frame = new JFrame(); 2. Set frame size: width and height frame.setSize(300, 400); 3. Set title of frame frame.setTitle("An Empty Frame"); 4. Set ‘default close operation’ (so that program exits when user closes the frame) frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 5. Make the frame visible frame.setVisible(true); EmptyFrameViewer.java

5 Drawing Shapes You do not draw directly on a frame To show anything in a frame (button, text, drawing, etc.) construct an appropriate component object and add it to the frame JComponent class represents blank component We extend the JComponent class to have it draw some shapes Then add our modified version of JComponent to a frame to display the drawing

6 Extending JComponent extends keyword indicates that our class, RectangleComponent, inherits all the definitions and functionality of JComponent But, we override the definition of the paintComponent method so that it does something we want paintComponent method is called by the Java system whenever the component needs to be redrawn public class RectangleComponent extends JComponent { public void paintComponent(Graphics g) { // drawing instructions go here... } // end paintComponent method } // end RectangleComponent class

7 paintComponent Method Called automatically the first time window is shown also called whenever window is resized or shown again after being hidden Takes a Graphics object parameter A Graphics object stores the current graphics state: current color, font, background color, line size, etc. Swing toolkit provides Graphics2D class Graphics2D : extended version of Graphics class that allows more sophisticated method to draw two-dimensional objects To recover Graphics2D object from more primitive Graphics object, use a cast: Graphics2D g2 = (Graphics2D) g;

8 Drawing with Graphics2D public void paintComponent(Graphics g) { // Recover Graphics2D Graphics2D g2 = (Graphics2D) g; // Construct a rectangle and draw it Rectangle box = new Rectangle(5, 10, 20, 30); g2.draw(box); // Move rectangle 15 units to the right and 25 units down box.translate(15, 25); // Draw moved rectangle g2.draw(box); } Graphics and Graphics2D classes part of java.awt package - needs to be imported AWT = Abstract Windowing Toolkit

9 Complete RectangleComponent import java.awt.*; // imports all classes in java.awt package import javax.swing.*; /** A component that draws two rectangles. */ public class RectangleComponent extends JComponent { public void paintComponent(Graphics g) { // Recover Graphics2D Graphics2D g2 = (Graphics2D) g; // Construct a rectangle and draw it Rectangle box = new Rectangle(5, 10, 20, 30); g2.draw(box); // Move rectangle 15 units to the right and 25 units down box.translate(15, 25); // Draw moved rectangle g2.draw(box); }

10 Displaying Rectangle Frame To display the RectangleComponent, you need to add it to a frame window 1. Construct frame as described earlier 2. Construct object of component class RectangleComponent component = new RectangleComponent(); 3. Add component to the frame frame.add(component); or frame.getContentPane().add(component); in earlier Java versions 4. Make frame visible

11 RectangleViewer Class import javax.swing.JFrame; public class RectangleViewer { public static void main(String[] args) { JFrame frame = new JFrame(); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("Two rectangles"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); RectangleComponent component = new RectangleComponent(); frame.add(component); frame.setVisible(true); }

12 Applets “Mini-application” embedded in a web page Run with browser or applet viewer Differences (from applications) Don’t have a main method Embedded within HTML document (web page) Subject to more security constraints Not in control of own execution--respond to browser or viewer Can program graphics in single class - no need for separate component and viewer classes

13 Applet Code Skeleton import java.awt.*; import javax.swing.*; public class MyApplet extends JApplet { public void paint(Graphics g) { // Recover Graphics2D Graphics2D g2 = (Graphics2D) g; // Drawing instructions go here... } Extend JApplet instead of JComponent Put drawing code inside paint method instead of paintComponent instead RectangleApplet.java

14 Running an Applet Need an HTML file with an applet tag HTML = Hypertext Markup Language Plain text file with formatting commands - ‘tags’ Browser displays the contents of the file according to the formatting tags To see HTML of file displayed in your browser, look for a ‘View Source’ command in the menus Simplest file to display an applet:

15 Another HTML File Two rectangles Here is my first applet : <applet code="RectangleApplet.class" width="300" height="400">

16 Viewing Applets Use applet viewer program with Java SDK appletviewer RectangleApplet.html Only looks for tag in a web page-- allows you to test your applets before putting them on a web page Or use a ‘Java 2-enabled’ browser WARNING: Browsers often save a copy of an applet in memory for a long time (e.g. until the entire application is exited) so if you change applet code and recompile, then reload the page in the browser, the browser may not use the latest version of the applet

17 The Internet 1960s - ARPANET Universities, research institutions Initial intent: allow remote execution of programs ‘Killer app’: electronic mail Internet (Bob Kahn) Collection of interoperable networks Share common protocols for transmitting data 1983: TCP/IP: Kahn and Vinton Cerf WWW (Tim Berners-Lee) Hyperlinked documents First interfaces clumsy to use 1993: Mosaic graphical ‘web browser’ (Marc Andreesen) Lots of different protocols over Internet FTP, telnet, gopher, file sharing, IM, smtp, …

18 Graphical Shapes Rectangles… Ellipses (and circles) Use Ellipse2D.Double (or Ellipse2D.Float ) Stores coordinates as double values Lines Use Line2D.Double (or Line2D.Float ) Strange class names indicate they are inner classes One class defined inside another Just import java.awt.geom.Ellipse2D as usual and use above names

19 Drawing Ellipses and Lines Must construct and then draw shape objects Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, width, height); g2.draw(ellipse); To construct a line object Line2D.Double segment = new Line2D.Double(x1, y1, x2, y2); or Point2D.Double from = new Point2D.Double(x1, y1); Point2D.Double to = new Point2D.Double(x2, y2); Line2D.Double segment = new Line2D.Double(from, to); g2.draw(segment);

20 Drawing Strings Specify string and basepoint of first character g2.drawString("Message", 50, 100); Give code to draw a circle with center (100, 100) and radius 25 Give code to draw a letter "V" by drawing two line segments

21 Colors Graphics context object keeps track of the current drawing color To change the color, supply a Color object to the setColor method Colors in Java specified by RGB (red-green-blue) model Components given as float values (use an F suffix) between 0.0F and 1.0F Color magenta = new Color(1.0F, 0.0F, 1.0F); g2.setColor( magenta );

22 Predefined Colors and Fills Color class defines commonly used colors Color.BLUE, Color.RED, Color.ORANGE, … (page 164) setColor method affects the line color To draw shapes filled in with current color Rectangle box =...; g2.fill( box );

23 Drawing Complex Shapes Good practice: Make a class for each grapical object Plan complex shapes by making sketches on (graph) paper public class Car {... public void draw(Graphics2D g2) { // Drawing instructions... }

24 Finding Shape Coordinates /** A car shape that can be positioned anywhere on the screen. */ public class Car { /** Constructs a car with a given top left x the x coordinate of the top left y the y coordinate of the top left corner */ public Car(int x, int y) { } /** Draws the g2 the graphics context */ public void draw(Graphics2D g2) { }

25 CarComponent Class Draw two cars – one in top left, one in bottom right corner of the component JComponent class provides getWidth/getHeight methods for dimensions of the component Car.java CarComponent.java

26 CarViewer import javax.swing.JFrame; public class CarViewer { public static void main(String[] args) { JFrame frame = new JFrame(); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("Two cars"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); CarComponent component = new CarComponent(); frame.add(component); frame.setVisible(true); } Try resizing the window

27 Object-Oriented Method of Programming Graphics Provide class for each object to be drawn Implement a draw method, taking a Graphics parameter Provide (a simple) component class that calls the objects’ draw methods Provide a viewer class that sets up a frame window

28 Reading Text Input Use JOptionPane String input = JOptionPane.showInputDialog("Enter x"); double x = Double.parseDouble(input); In general, a poor choice for user interface but we will use it for now – much simpler than the alternative ColorViewer.java ColoredSquareComponent.java

29 Comparing Visual and Numerical Information Compute intersection between circle and vertical line Circle has radius r = 100 and center (a, b) = (100, 100) Line has constant x value IntersectionComponent.java LabeledPoint.java IntersectionViewer.java

30 Using Test Cases Calculate test cases by hand to check your application Use special boundary conditions x = 0; x = 100 … Use a few typical input values Don’t be reluctant to double-check calculations by hand Random tinkering (switching + and - signs) is not a good way to debug a program