Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.

Slides:



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

1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
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.
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.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
1 L36 Graphics and Java 2D™ (1). 2 OBJECTIVES  To understand graphics contexts and graphics objects.  To understand and be able to manipulate colors.
©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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
©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.
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.
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.
1 Drawing Shapes and Text With Applets. 2 Drawing in the paint method import java.awt.*;// access the Graphics object import javax.swing.*;// access to.
Graphics and Multimedia. Introduction The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other.
Web Design & Development Lecture 18. Java Graphics.
Java Applets. Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java.
Chapter 16: Graphics. Objectives Learn about the paint() and repaint() methods Use the drawString() method to draw String s using various fonts and colors.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Introducing Graphics There are generally two types of graphics facilities in Java –Drawing –GUIs We concentrate on drawing here We will draw on a Graphics.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction.
Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects 28.3Color Control 28.4Font Control 28.5Drawing.
 Pearson Education, Inc. All rights reserved. 1 Ch 12 Graphics and Java 2D In this chapter you will learn:  To understand graphics contexts.
tiled Map Case Study: Rendering with JPanel © Allan C. Milne v
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors.
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.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
1CS 680: Developing User Interfaces. Dario Salvucci, Drexel University. Introduction to JFC Swing.
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
-Sai Phalgun Tatavarthy. Outline What is a “Graphing Calculator”? Graphics, Graphics2D classes Methods of Graphics object Custom Painting The paintComponent()
Creating Your Own Widgets CompSci 230 S Software Construction.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Merete S COLLEGEFACULTY OF ENGINEERING & SCIENCE Graphics ikt403 – Object-Oriented Software Development.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
(c)2006 E.S.Boese All Rights Reserved.1 Drawing Shapes and Text Chapter 2 – Lecture Slides.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
1 Graphics, Fonts and Color Chapter 9. 2 What is in this chapter: l Graphics class and coordinates l graphics primitives (lines,rectangles,ovals and arcs)
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.
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.
6.1 Coordinates The screen of a computer is a grid of little squares called pixels. The color of each pixel can be set individually, and drawing on the.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Break Time! Graphical User Interface (GUI) NO EXAM….ONLY PROJECT!
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Chapter 8 Graphics.
Java Graphics.
Building Java Programs
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Chapter 10 Graphics.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Chapter 8 Graphics.
Presentation transcript:

Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin 3rd AP edition

16-2 Objectives: Understand computer graphics basics Learn about paint, paintComponent, and repaint methods Learn about coordinates and colors Review shape drawing methods Learn to use fonts and draw graphics text

16-3 Computer Graphics Basics There are two types of graphics devices: vector and raster. A vector device has some kind of pen that can move and draw lines directly on the surface. A raster device creates a picture by setting colors to individual pixels (picture elements) on a rectangular “raster.” 

16-4 Basics (cont’d) A graphics adapter in your computer is a raster device. VRAM (video RAM) contains the information about the colors of all pixels. The screen displays the contents of VRAM. To draw a shape, you need to set the exactly right set of pixels to the required colors. The number of pixels in the raster vertically and horizontally is called the device resolution.

16-5 Basics (cont’d) A graphics software package provides functions for:  setting drawing attributes: color, line width and style, fill texture and pattern, font  drawing lines, circles, rectangles, polygons, and other basic shapes. In other words, a graphics package turns a raster device into a “logical” vector device.

16-6 Graphics in Java Java library offers a graphics class Graphics and a graphics package Graphics2D. Graphics provides only most basic capabilities. Graphics2D and related classes in java.awt support better graphics with color gradients, line styles, etc. Here we only deal with Graphics.

16-7 Graphics in Windows In a windowing environment, a picture must be repainted every time we move, reopen or reshape the window. The program must have one “central” place or method where all the drawing happens. The operating system sends a “message” to the program to repaint its window when necessary.

16-8 paint and paintComponent The javax.swing.JFrame class (which represents application windows) has one method, called paint, where all the drawing takes place. In Swing, paint calls paintComponent for each of the GUI components in the window. A programmer creates a picture by overriding the default paintComponent method (or the paint method).

16-9 paint and paintComponent (cont’d) The paint method takes one argument of the type Graphics: import java.awt.*; import javax.swing.*; public class MyWindow extends JFrame {... public void paint (Graphics g) { super.paint (g);... } Defines the graphics “context” (location, size, coordinates, etc.)

16-10 paint and paintComponent (cont’d) The same for paintComponent: import java.awt.*; import javax.swing.*; public class MyCanvas extends JPanel {... public void paintComponent (Graphics g) { super.paintComponent (g);... } Or another Swing GUI component

16-11 paint and paintComponent (cont’d) A programmer never calls paint or paintComponent directly. repaint is called instead whenever you need to refresh the picture (after adding, moving, or changing some elements, etc.): public class MyCanvas extends JPanel {... balloon.move(dx, dy); repaint ( );... repaint takes no parameters: the graphics context is restored and sent to paintComponent automatically

16-12 Coordinates x y y-axis points down, as in many other graphics packages Units: pixels (0, 0) Origin: the upper-left corner of the component

16-13 Coordinates (cont’d) A GUI component provides getWidth and getHeight methods that return its respective dimension. These methods can be used to produce scalable graphics. getWidth and getHeight only work after the component has been placed (do not call them from a component’s constructor).

16-14 Coordinates (cont’d) The position of a rectangle, oval, and even an arc is defined by using its “bounding rectangle,” described by x, y, width, height: x, y

16-15 Coordinates (cont’d) (x, y) g.drawOval(x - r, y - r, 2 * r, 2 * r); r (x - r, y - r) 2r2r 2r2r

16-16 Colors The color attribute is set by calling g.setColor and stays in effect until changed: You can form a color with specified red, green, and blue (RGB) values: g.setColor(Color.BLUE); g.draw... g.setColor(Color.LIGHT_GRAY);... int rVal = 5, gVal = 255, bVal = 40; Color yourEyes = new Color (rVal, gVal, bVal);

16-17 Colors (cont’d) javax.swing.JColorChooser lets you choose a color in a GUI application:

16-18 Drawing Basic Shapes g.drawLine (x1, y1, x2, y2); g.clearRect (x, y, w, h); g.drawRect (x, y, w, h); g.fillRect (x, y, w, h); g.drawRoundRect (x, y, w, h, horzD, vertD); g.fillRoundRect (x, y, w, h, horzD, vertD); g.drawOval (x, y, w, h); g.fillOval (x, y, w, h); g.drawArc (x, y, w, h, fromDegr, measureDegrs);

16-19 Basic Shapes (cont’d) g.drawPolygon (xCoords, yCoords, nPoints); g.fillPolygon (xCoords, yCoords, nPoints); g.drawPolyline (xCoords, yCoords, nPoints); g.drawString (str, x, y); g.drawImage (img, x, y, this); abc ImageObserver, often this x, y

16-20 Fonts Font font = new Font (name, style, size); g.setFont (font); abc "Serif" abc "SansSerif" abc "Monospaced" Font.PLAIN Font.BOLD Font.ITALIC int (pixels)

16-21 Review: Explain the difference between vector and raster graphics. In what units are the coordinates measured in Graphics? Where is the origin of the coordinate system? How is the position and size of a rectangle or an oval is defined? How do you set a drawing color? Name a few drawing methods.