1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Java methods: Object-oriented programming and data structures (2nd AP edition): Chapter 16 - Maria Litvin, Gary Litvin

21 22 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 21
Dung lượng 333,72 KB

Nội dung

Chapter 16 - Graphics. This chapter introduces basic general computer graphics concepts as well as a few specifics for Java. This chapter’s objectives are to: 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.

Java Methods Object-Oriented Programming and Data Structures 2nd AP edition with GridWorld Maria Litvin ● Gary Litvin   Height  Chapter 16  Ascent  Baseline  Descent  Graphics Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing All rights reserved 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­2 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­3 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­4 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­5 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­6 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­7 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­8 paint and paintComponent (cont’d) • 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) { Defines the graphics super.paint(g); “context” (location, size, coordinates, etc.) } } 16­9 paint and paintComponent (cont’d) • The same for paintComponent: import java.awt.*; import javax.swing.*; Or another Swing GUI component public class MyCanvas extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); } } 16­10 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­11 Coordinates (0, 0) Origin: the upper-left corner of the component x Units: pixels y y-axis points down, as in many other graphics packages 16­12 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­13 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­14 Coordinates (cont’d) g.drawOval(x - r, y - r, 2*r, 2*r); (x-r, y-r) r (x, y) 2r 2r 16­15 Colors • The color attribute is set by calling g.setColor and stays in effect until changed: g.setColor(Color.BLUE); g.draw g.draw g.setColor(Color.LIGHT_GRAY); • You can form a color with specified red, green, and blue (RGB) values: int rVal = 5, gVal = 255, bVal = 40; Color yourEyes = new Color (rVal, gVal, bVal); 16­16 Colors (cont’d) • javax.swing.JColorChooser lets you choose a color in a GUI application: 16­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); 16­18 Basic Shapes (cont’d) g.drawPolygon (xCoords, yCoords, nPoints); g.fillPolygon (xCoords, yCoords, nPoints); g.drawPolyline (xCoords, yCoords, nPoints); x, y abc g.drawString (str, x, y);  g.drawImage (img, x, y, this); ImageObserver, often this 16­19 Fonts Font font = new Font (name, style, size); abc                  "Serif" abc int (pixels) "SansSerif" abc "Monospaced" Font.PLAIN g.setFont (font); Font.BOLD Font.ITALIC 16­20 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 you set a drawing color? • Name a few drawing methods 16­21 ... shapes • In other words, a graphics package turns a raster device into a “logical” vector device 16? ?5 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? ?6... 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

Ngày đăng: 04/11/2020, 23:17

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN