Basic Graphics Chapter 5 3/19/15 Thursday Section Only

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
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.
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.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
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.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
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.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
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.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
1 Graphical objects We will draw graphics in Java using 3 kinds of objects: DrawingPanel : A window on the screen. Not part of Java; provided by the authors.
Java Concepts Chapter 2 – Graphical Applications Mr. Smith AP Computer Science A.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
©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.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
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();
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
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 ________.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
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,
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Intro to Graphics from Chapter 2 of Java Software Solutions
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
GUI AND GRAPHICS.
Building Java Programs
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Topic 8 graphics "What makes the situation worse is that the highest level CS course I've ever taken is cs4, and quotes from the graphics group startup.
Building Java Programs
Building Java Programs
A+ Computer Science METHODS.
Building Java Programs
A+ Computer Science METHODS.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
Building Java Programs
Presentation transcript:

Basic Graphics Chapter 5 3/19/15 Thursday Section Only Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

This Week's Topics Creating a Window The Class Graphics Getting Ready to Draw A Sample Drawing Panel Displaying a Panel The Class Graphics The Coordinate System Drawing Lines, Shapes, and Text Painting Shapes Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Objectives After studying this chapter, you should be able to: Write a program that creates a window with graphics Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Objectives Display in a window Lines Rectangles Ovals Arcs Strings Use color in Graphics Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Creating a Window A rectangular area Figure 5-1 An empty frame Part of a user interface Called a frame Contains title bar Figure 5-1 An empty frame Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Creating a Window Construct object of class JFrame From package javax.swing JFrame aWindow = new JFrame(); Specify size aWindow.setSize(400, 600); Window must be made visible. aWindow.setVisible(true); Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Creating a Window setTitle() puts a title in the title bar: aWindow.setTitle(“Bronco Window”); View sample program, EmptyWindow Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Creating a Window If the user closes the window with the control button, the window closes. The program keeps running. aWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); Exits program when window closes. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Preparing to Draw Cannot draw directly on JFrame object This is a container Contains other objects Can create a JPanel object and add it to the JFrame, but it would be blank. The JPanel class is in javax.swing Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Preparing to Draw Create a class of panels upon which to draw. DrawingPanel inherits JPanel. Graphics g parameter allows it to draw. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Simple Drawing Panel Some basic shapes. Now we create a window and add an instance of class DrawingPanel to the window. Example in WindowWithDrawing Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Class Graphics Java runtime environment instantiates a Graphics object The graphics context Made available to method paintComponent Use methods of Graphics object to display shapes Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Coordinate System Figure 5-2 The axes and some points on the coordinate system used for graphics Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Lines, Shapes, Text Methods: g.drawline (x1, y1, x2, y2); g.drawRect(x, y, width, height); Note pixels on edges of 5 x 4 rectangle Figure 5-3 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Lines, Shapes, Text Ovals, Circles g.drawOval(x, y, width, height); ( for circle, height = width) Imaginary rectangle encloses oval Figure 5-4 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Example I will: Create a java class displays a JFrame. Create JPanel that has a drawing of a circle with a rectangle in it. Make it look like a DO NOT ENTER sign Add the JPanel to the JFrame to display the sign

Questions Study questions 1-4 in Chapter 5. Study exercises 1-6 on page 130.

Lines, Shapes, Text Arc – a portion of an oval Figure 5-5 Additional specification of beginning and ending angle g.drawArc(x, y, width, height, startAngle, extentAngle) Figure 5-5 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Lines, Shapes, Text Strings – text “drawn” onto the panel g.drawString ("Square", 50, 90); Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Lines, Shapes, Text Filled rectangles and squares Specify position, dimensions g.fillRect(x, y, width, height); Pixels in 5 x 4 rectangle painted by fillRect Figure 5-6 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Lines, Shapes, Text Filled ovals, circles, arcs Similar to use of drawOval, drawArc g.fillOval (250, 100, 40, 20); Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Participation #16 on page 131 Create a Jframe (window) Create a Panel and add it to the JFrame Use the paintComponent to draw basic shapes. Start with the largest rectangle of the house Add windows, doors and roof

import javax.swing.JFrame; public class WindowWithDrawing { public static void main(String[] args) { //set up a JFrame window JFrame bWindow = new JFrame(); bWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); bWindow.setSize(400,300); bWindow.setTitle("Shapes"); //Put a DrawingPanel in the Window //Make the window visible bWindow.setVisible(true); }

import javax.swing.JPanel; import java.awt.Graphics; public class DrawingPanel extends JPanel { //A panel to draw on. public void paintComponent(Graphics pen){ //Statements that draw go here }

Problem: Display Temperature* Program that draws a thermometer Display temp in analog, digital Note Figure 5-7 *See text for this example Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Problem: Display Temperature Discussion Rectangles Filled rectangle Text Locate in panel Figure 5-8 Schematic of Thermometer Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Problem: Display Temperature A panel that draws the thermometer Listing 5-4 Now we create a program that displays the panel specified above Listing 5-5 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Class Font-Skip Specify a Font object to set for the drawString method Font aFont = new Font(name, style, size); Physical name Name of an actual font Logical name Name of a font family Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Class Font Figure 5-9 Names of fonts and families written in the fonts they represent Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Class Color In java.awt.Color Create a color by specifying RGB values Each primary color given by integer 0 – 255 Predefined color constants from Color Figure 5-10 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Class Color A demonstration of the use of color Listing 5-6 A program that displays the panel defined in previous listing, Listing 5-7 Resulting output Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Problem Solved: Happy Face See text for this example We desire a program to display a happy face as shown in Figure 5-11 Must decide shape, size, color, positions of Face Eyes Mouth Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Problem Solved: Happy Face Positions to be specified Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Problem Solved: Happy Face View source code for happy-face panel Figure 5-8 The program which would display this panel is left as an exercise. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Imagine! Java: Programming Concepts in Context by Frank M. Carrano Basic Graphics Chapter 5 Imagine! Java: Programming Concepts in Context by Frank M. Carrano