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

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

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.

Trang 1

Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing All

rights reserved.

Java Methods

Object-Oriented Programming

and Data Structures

Maria Litvin ● Gary Litvin

2nd AP edition with GridWorld

Trang 2

Objectives:

• Understand computer graphics basics

• Learn about paint, paintComponent, and

repaint methods

• Learn about coordinates and colors

• Learn to use fonts and draw graphics text

Trang 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.”

Trang 4

• 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.

Trang 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

Trang 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

Trang 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)

Trang 9

• paint method takes one argument of the

Defines the graphics

“context” (location, size, coordinates, etc.)

Trang 10

• The same for paintComponent:

Trang 11

• 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

is restored and sent to

paintComponent

automatically

Trang 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

Trang 13

Coordinates (cont’d)

• A GUI component provides getWidth and

getHeight methods that return its respective dimension

• These methods can be used to produce

Trang 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:

int rVal = 5, gVal = 255, bVal = 40;

Color yourEyes = new Color (rVal, gVal, bVal);

Trang 17

Colors (cont’d)

• javax.swing.JColorChooser lets you choose a color in a GUI application:

Trang 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);

Trang 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);

Trang 20

int (pixels)

Trang 21

• 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?

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