Java Concepts Chapter 2 – Graphical Applications Mr. Smith AP Computer Science A.

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

Chapter 2: Using Objects Part 2. Assume you wish to test the behaviour of some method. This is accomplished by providing a tester class: Supply a main.
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!!!
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 2 – Using Objects.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Graphical User Interfaces Lecture 25, Thu Apr
Java Applets A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia.
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.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Web Design & Development Lecture 18. Java Graphics.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 5 - 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.
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 Two: Using Objects.
Chapter Two: Using Objects. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about parameters.
Chapter 2 – Using Objects Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Chapter 12 Event Handling. Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons,
ICOM 4015: Advanced Programming Lecture 2 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Two: Using.
Chapter 4: Applets and Graphics 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 4 Applets and Graphics.
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
© A+ Computer Science - Chicken yeller = new Chicken();
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
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.
Fall 2006Adapded from Java Concepts Companion Slides1 Event Handling Advanced Programming ICOM 4015 Lecture 13 Reading: Java Concepts Chapter 12.
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.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
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.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Graphical User Interfaces Lecture 24, Tue Apr
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 2 – Using Objects.
Clicker quick questions 10/17/13 CSE 1102 Fall 2013.
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.
Chapter 4 Interface Types and Polymorphism: Graphics, Timer, Animation.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 2 – Using Objects.
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 Goals To learn about variables
Chapter Goals To learn about variables
Chapter 2 Not Glasses, Classes!!! Using Classes
Chapter 5 Programming Graphics
Chapter 8 Graphics.
Chapter 2 – Using Objects
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
GUI AND GRAPHICS.
Using Objects.
Chapter 10 Graphics.
Chapter 2 – Using Objects
Chapter 12 Event Handling
A+ Computer Science METHODS.
Chapter 8 Graphics.
A+ Computer Science METHODS.
Handout-14 Applets and Graphics
Presentation transcript:

Java Concepts Chapter 2 – Graphical Applications Mr. Smith AP Computer Science A

Graphical Applications and Frame Windows Objective Overview of drawing graphical applications You will be able to draw basic graphics in a frame window (more attractive than a console window)  Construct a frame of a certain size  Construct a component (such as a rectangles, circles, or a combination)  Add the component to the frame  Make the frame visible

Graphical Applications and Frame Windows Constructing a Frame Construct an object of the JFrame class JFrame frame = new JFrame(); Set the size of the frame frame.setSize(300, 400); //Frame will be 300 pixels wide and 400 pixels tall //Omitting this step will make the frame 0x0 Set the title of the frame (optional) frame.setTitle("Graphical Application"); Set the "default close operation". frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //When the user closes the frame, the program will end Make the frame visible frame.setVisible(true);

Graphical Applications and Frame Windows Creating a Rectangle Component Object In order to draw something in the frame, you must first create a component object and then add it to the frame A component can consist of multiple graphics objects Here are some of the classes that need to be imported in order to create a component object that draws a rectangle: import java.awt.Graphics; //primitive class – stores graphics state //including the current color, font, etc. import java.awt.Graphics2D; //Class used to draw shapes import java.awt.Rectangle; //Rectangles class import javax.swing.JComponent; //Used to draw in a frame

Graphical Applications and Frame Windows Example of a Rectangle Component Object Class (used for creating, drawing, and positioning Rectangle objects) import java.awt.Graphics;//Allows you to manipulate the graphics //state (color, etc.) import java.awt.Graphics2D; //Contains methods to draw shapes import java.awt.Rectangle; import javax.swing.JComponent; public class RectangleComponent extends JComponent { //Place all the drawing instructions inside paintComponent method public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; //Cast to recover Graphics2D Rectangle box = new Rectangle(5, 10, 20, 30); //Construct rectangle g2.draw(box); //Draw rectangle box.translate(15, 25); //Move rectangle 15 units right and 25 down g2.draw(box); //Draw rectangle again }

Graphical Applications and Frame Windows Drawing a Rectangle Component Construct a frame (see previous slide) Construct an object of your component class RectangleComponent component = new RectangleComponent(); Add the component to the frame frame.add(component); Make the frame visible (see previous slide)

Graphical Applications and Frame Windows Write a Client Program to View the Rectangles (used for instantiating the RectangleComponent object and adding it to the frame) import javax.swing.JFrame; public class RectangleViewer { public static void main(String [] args) { JFrame frame = new JFrame(); frame.setSize(300, 400); frame.setTitle("Graphics Application"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); RectangleComponent component = new RectangleComponent(); frame.add(component); frame.setVisible(true); }

Graphical Applications and Frame Windows Drawing a Face using Basic Shapes You must first construct a "face" component and then add it to the frame The face component will consist of different types of graphical objects for the head, eyes, and mouth Here are some of the classes that need to be imported in order to use graphics: import java.awt.Color; //color class import java.awt.Graphics; //primitive graphics class import java.awt.Graphics2D; //Contains methods to draw shapes import java.awt.Rectangle; //Rectangles class Import java.awt.geom.Ellipse2D; Import java.awt.geom.Line2D; Import javax.swing.JPanel; import javax.swing.JComponent;

Graphical Applications and Frame Windows Write a Face Component Class (used for creating, drawing, and positioning a face) import java.awt.Color;//color class import java.awt.Graphics;//primitive graphics class import java.awt.Graphics2D; //Extends Graphics class import java.awt.Rectangle;//Rectangles class import java.awt.geom.Ellipse2D; //Ellipse and circle class import java.awt.geom.Line2D; //Line class import javax.swing.JPanel; import javax.swing.JComponent; public class FaceComponent extends JComponent { //Place drawing instructions inside this method public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; //Make graphics 2D Ellipse2D.Double head = new Ellipse2D.Double(5, 10, 100, 150); //Construct head (x, y, width, height) g2.draw(head); //Draw head Line2D.Double eye1 = new Line2D.Double(25, 70, 45, 90); //Construct left eye (x1, y1, x2, y2) g2.draw(eye1); Line2D.Double eye2 = new Line2D.Double(85, 70, 65, 90); //Construct right eye (x1, y1, x2, y2) g2.draw(eye2); Rectangle mouth = new Rectangle(30, 130, 50, 5); //Construct mouth (x, y, width, height) g2.setColor(Color.RED); //Color of mouth g2.fill(mouth); //Fill mouth with red g2.setColor(Color.BLUE); g2.drawString("Hello, Class!!!", 5, 175); //Draw greeting (“message”, x, y) }

Graphical Applications and Frame Windows Write a Client Program to View the Face (used for creating the FaceComponent object and adding it to the frame) import javax.swing.JFrame; public class FaceViewer { public static void main(String [] args) { JFrame frame = new JFrame();//Instantiate new frame frame.setSize(300, 400);//Set frame size frame.setTitle("Alien Face");//Set frame title frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FaceComponent component = new FaceComponent(); //Instantiate component frame.add(component);//Add component to frame frame.setVisible(true);//Set frame visible }

Graphical Applications and Frame Windows Classwork/Homework Assignment Download the following classes from my website:  FaceComponent – Face drawing object  FaceViewer – View the face Use these classes as a starting point for your component class and client program to draw it in a frame Refer to Java Concepts for drawing other graphical objects (ellipse, lines, rectangles, etc.) You can also refer to the link on my website named Drawing Geometric Shapes Use your imagination and draw an object of your choice Remember to include your name (JohnDoe) at the end of all classes you create, so I know who created them Once these are finished, we will show them to the class