Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.

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?
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.
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.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 16 : Swing VI Graphics King Fahd University of Petroleum & Minerals College of Computer.
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,
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
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.
Copyright 2010 by Pearson Education Building Java Programs Graphics reading: Supplement 3G.
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 ________.
Clicker quick questions 10/17/13 CSE 1102 Fall 2013.
Review_6 AWT, Swing, ActionListener, and Graphics.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
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.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Building Java Programs
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
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
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
Building Java Programs
Presentation transcript:

Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

This Week's Topics Creating a Window  Getting Ready to Draw  A Sample Drawing Panel  Displaying a Panel The Class Graphics  The Coordinate System  Drawing Rectangles  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 Display in a window  Rectangles, Ovals and Arcs 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  Part of a user interface  Called a frame  Contains title bar 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); setTitle() puts a title in the title bar: aWindow.setTitle(“Bronco Window”); Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Creating a Window Window must be made visible. aWindow.setVisible(true); 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.

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

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 that Graphics object to display shapes.

Coordinate System Figure 5-2 The axes and some points on the coordinate system used for graphics

Lines, Shapes, Text Methods:  g.drawline (x1, y1, x2, y2);  g.drawRect(x, y, width, height);  g.drawOval(x, y, width, height); Note pixels on edges of 5 x 4 rectangle Figure 5-3

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 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 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Draw Arc g.drawArc(35, 45, 75, 95, 0, 90); drawArc(int x, int y, int width, int length, int startAngle, int arcAngle) Used to draw an arc inside an imaginary rectangle whose upper left corner is at (x,y). The arc is drawn from the startAngle to startAngle + arcAngle and is measured in degrees. A startAngle of 0º points horizontally to the right (like the unit circle in math). Positive is a counterclockwise rotation starting at 0º.

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 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Change Colors Assume g is a Graphic object. g.setColor(Color.RED); //Makes the “pen” // red.

Example Make a project that uses Graphics to make a smiley face. Make a Frame Put a Jpanel on the Frame Make a Drawing on the panel

Smiley Face Draw a yellow circle Draw two solid arcs for the eyes Draw a smile

Participation Create a Jframe (window) Create a Panel and add it to the JFrame Use the paintComponent to make a Japanese flag.  Draw a solid white rectangle.  Draw a solid red circle centered inside the white rectangle.

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 }

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