Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.

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

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?
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.
User Interface Programming in C#: Graphics
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.
©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.
Web Design & Development Lecture 18. Java Graphics.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
Java Applets. Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java.
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 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction.
tiled Map Case Study: Rendering with JPanel © Allan C. Milne v
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
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.
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.
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.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
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.
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.
-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.
Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
(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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Java Dynamic 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.
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,
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.
Creating Your Own Widgets
Chapter 8 Graphics.
Java Graphics.
Java Graphics CS 2511.
Chapter 10 Graphics.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Chapter 8 Graphics.
Presentation transcript:

Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. TM

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

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

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

14-5 Basics (cont’d) l 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. l In other words, a graphics package turns a raster device into a “logical” vector device.

14-6 Graphics in Java l Java libraries offer two graphics packages: Graphics and Graphics2D. l Graphics is really not a package but a class in the java.awt package, which provides only most basic capabilities. l Graphics2D and related classes in java.awt support better graphics with color gradients, line styles, etc. l Here we only deal with Graphics.

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

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

14-9 paint and paintComponent (cont’d) l paint 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.)

14-10 paint and paintComponent (cont’d) l 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

14-11 paint and paintComponent (cont’d) l A programmer never calls paint or paintComponent directly. repaint is called instead whenever you need to refresh the picture (e.g. after adding, moving, or changing some elements): MyCanvas canvas = new MyCanvas();... balloon.move(dx, dy); repaint();... repaint takes no arguments: the graphics context is restored and sent to paintComponent automatically

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

14-13 Coordinates (cont’d) l A GUI component provides getWidth and getHeight methods that return its respective dimensions. l They can be used to produce scalable graphics. l getWidth, getHeight only work after the component has been placed (e.g., don’t call them from a component’s constructor).

14-14 Coordinates (cont’d) l 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

14-15 Colors l The color attribute is set by calling g.setColor and stays in effect until changed again: l You can form a color with any RGB values: g.setColor(Color.blue); g.draw... g.setColor(Color.lightGray);... int rVal = 5, gVal = 255, bVal = 40; Color yourEyes = new Color (rVal, gVal, bVal);

14-16 Colors (cont’d) l javax.swing.JColorChooser let’s you choose a color in a GUI application:

14-17 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);

14-18 x, y 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, usually this

14-19 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)

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