COMP 110: Spring 20091 Announcements Quiz Wednesday No Class Friday Assignment 5 Due next Monday Q&A Review Session Monday.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Apr 20, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

Made with love, by Zachary Langley Applets The Graphics Presentation.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
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.
Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.
1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
CS 106 Introduction to Computer Science I 01 / 29 / 2008 Instructor: Michael Eckmann.
Flow of Control Recitation – 09/(11,12)/2008 CS 180 Department of Computer Science, Purdue University.
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.
©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.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
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.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 24 - Introduction to Java Applications and Applets Outline 24.1Introduction 24.2Basics of a Typical.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
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.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Agenda For Feb Finish up Unit 3 Exercises on page Unit 4 Exercises on page 33. Question #2 3. Create a Happy Face Java Applet (due next class).
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
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.
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
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.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
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.
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.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
Option Panes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Dialog Boxes.
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Chapter 2 Creating a Java Application and Applet.
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.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
JOptionPane. Import javax.swing.JOptionPane Use showInputDialog() for input. Only string values can be input. To convert an input value from a string.
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.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Lecture 09 Applets.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Chapter 13: Advanced GUIs and Graphics
CS18000: Problem Solving and Object-Oriented Programming
Chapter 24 - Introduction to Java Applications and Applets
Message, Input, and Confirm Dialogs
Topics Graphical User Interfaces Using the tkinter Module
JOptionPane class.
Graphics Reading: Supplement 3G
Advanced GUIs and Graphics
Presentation transcript:

COMP 110: Spring Announcements Quiz Wednesday No Class Friday Assignment 5 Due next Monday Q&A Review Session Monday

COMP 110: Spring Today in COMP 110 Java Graphics Programming Demo

COMP 110: Spring Computer Graphics Computer Graphics is about producing images with a computer This can be useful in a variety of ways User interfaces (dialog boxes, icons, windows) Special effects (movies, games, etc) Image editing (photoshop) Visualizing scientific data

COMP 110: Spring Graphics in Java Java provides some useful classes & functionality for adding graphics to your programs

COMP 110: Spring Your First Graphics Program import javax.swing.JApplet; import java.awt.Graphics; public class HappyFace extends JApplet { public void paint(Graphics canvas) { canvas.drawOval(100, 50, 200, 200); canvas.fillOval(155, 100, 10, 20); canvas.fillOval(230, 100, 10, 20); canvas.drawArc(150, 160, 100, 50, 180, 180); } Where’s the main method?

COMP 110: Spring Applets public class HappyFace extends JApplet Our HappyFace class inherits from the class JApplet This means that our program is an applet instead of a normal application An applet is program that is meant to be run in a web browser We’ll use it as an easy way to get some graphics capability

COMP 110: Spring Applets Applets do not have a main method They can’t be run like a normal application (using the red running man icon in jGRASP We need to use the apple icon to run the applet

COMP 110: Spring Drawing to Screen public void paint(Graphics canvas) { canvas.drawOval(100, 50, 200, 200); canvas.fillOval(155, 100, 10, 20); canvas.fillOval(230, 100, 10, 20); canvas.drawArc(150, 160, 100, 50, 180, 180); } Applets use a method called paint in order to draw to screen This method is called for you automatically when you start the applet

COMP 110: Spring Screen Coordinate System (0,0) x-axis y-axis (100,50) 50 pixels 100 pixels

COMP 110: Spring Drawing Shapes //draw oval at position 100, 50, of width and height 200 canvas.drawOval(100, 50, 200, 200); //draw a “filled-in” oval at position 155, 100 with w=10, h=20 canvas.fillOval(155, 100, 10, 20); //draw a “filled-in” oval at position x=230, y=100 with w=10, h=20 canvas.fillOval(230, 100, 10, 20); //draw an arc, details in API canvas.drawArc(150, 160, 100, 50, 180, 180);

COMP 110: Spring The Graphics Class What is the canvas object? An object of the Graphics class The graphics class knows how to draw things like shapes to the screen

COMP 110: Spring Setting the Color The Graphics class also provides the capability to change the color that shapes are drawn with Example //draw a yellow oval canvas.setColor(Color.YELLOW); canvas.drawOval(100, 50, 200, 200); //now draw a black oval canvas.setColor(Color.BLACK); canvas.drawOval(100, 50, 200, 200);

COMP 110: Spring Drawing Text to the Screen We can display text on the screen using the drawString method drawString(string, xPos, yPos) Example //print at string and position (60, 75) canvas.drawString("This will be printed to the screen", 60, 75);

COMP 110: Spring String Drawing Example import javax.swing.JApplet; import java.awt.Graphics; public class HappyFace extends JApplet { public void paint(Graphics canvas) { canvas.drawOval(100, 50, 200, 200); canvas.fillOval(155, 100, 10, 20); canvas.fillOval(230, 100, 10, 20); canvas.drawArc(150, 160, 100, 50, 180, 180); canvas.drawString("This is a happy face!", 50, 50); }

COMP 110: Spring Drawing Polygons A polygon is a closed figure made up of line segments that do NOT cross

COMP 110: Spring Drawing Polygons You can draw a polygon by giving the locations of the corners There are two functions you can use drawPolygon Draws an unfilled polygon (just the edges) fillPolygon Draw a filled polygon p1 p2 p3 p4 p5

COMP 110: Spring Drawing Polygons Syntax canvas.drawPolygon(X_Array, Y_Array, Num_Points) canvas.fillPolygon(X_Array, Y_Array, Num_Points) Example int[] xHouse = {150, 150, 200, 250, 250}; int[] yHouse = {100, 40, 20, 40, 100}; canvas.drawPolygon(xHouse, yHouse, xHouse.length);

COMP 110: Spring Creating Dialog Boxes Dialog boxes are special windows that can be used to take input from the user or to display output

COMP 110: Spring Dialog Boxes Dialog boxes can be created using the JOptionPane class import javax.swing.JOptionPane; public class JOptionPaneDemo { public static void main(String[] args) { String name = JOptionPane.showInputDialog("Enter your name:"); JOptionPane.showMessageDialog(null, "Hi " + name + "!"); System.exit(0); }

COMP 110: Spring Input Dialogs Input dialogs take input from the user in the form of a string We use the showInputDialog method of the JOptionPane class Example String name = JOptionPane.showInputDialog("Enter your name:")

COMP 110: Spring Output Dialogs Output dialogs are used to display a message to the user We use the showMessageDialog method of the JOptionPane class Example JOptionPane.showMessageDialog(null, "Hi " + name + "!");

COMP 110: Spring Methods of JOptionPane If showInputDialog and showMessage Dialog are called using the class name JOptionPane, what kind of methods must they be? Static, a JOptionPane object is never created String name = JOptionPane.showInputDialog("Enter your name:"); JOptionPane.showMessageDialog(null, "Hi " + name + "!");

COMP 110: Spring Exiting the Program public static void main(String[] args) { String name = JOptionPane.showInputDialog("Enter your name:"); JOptionPane.showMessageDialog(null, "Hi " + name + "!"); System.exit(0); } At the end of the program we called System.exit(0) This is necessary to get the program end when using things like windows/dialog boxes etc.

COMP 110: Spring Confirmation Windows We can also create dialog boxes that provide the user with the options “Yes” and “No” These are called confirmation windows

COMP 110: Spring Confirmation Windows import javax.swing.JOptionPane; public class JOptionPaneDemo2 { public static void main(String[] args) { while(true) { int answer = JOptionPane.showConfirmDialog(null, "End Program?", "Click Yes or No:", JOptionPane.YES_NO_OPTION); if(answer == JOptionPane.YES_OPTION) System.exit(0); else if(answer == JOptionPane.NO_OPTION) System.out.println("One more time"); else System.out.println("Error"); } } }

COMP 110: Spring Creating the Window int answer = JOptionPane.showConfirmDialog(null, "End Program?", "Click Yes or No:", JOptionPane.YES_NO_OPTION);

COMP 110: Spring Checking the Result if(answer == JOptionPane.YES_OPTION) System.exit(0); else if(answer == JOptionPane.NO_OPTION) System.out.println("One more time"); else System.out.println("Error");

COMP 110: Spring Programming Demo Expand the House Fill the house with a color Draw a door Draw a window Add Text

COMP 110: Spring Programming Demo (150, 100) (150, 40) (200, 20) (250, 40) (250, 100) (0,0) x-axis y-axis (175, 100) (175, 60) (200, 60) (200, 100) (220, 60) (220, 80)(240, 80) (240, 60) (150, 120)

COMP 110: Spring Programming Demo Programming

COMP 110: Spring Wednesday Quiz