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

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

22 15 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 22
Dung lượng 235,71 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 18 - Mouse, keyboard, sounds, and images. This is essentially a “learning by doing” chapter. Not much theory here. This chapter also introduces bit-wise logical operators in the context of identifying the status of keyboard modifier keys (Alt, Shift, Ctrl).

Trang 1

Mouse, Keyboard, Sounds,

and Data Structures

Maria Litvin ● Gary Litvin 2nd AP edition with GridWorld

 

Chapter

Trang 2

• Learn the basics of playing sounds and

displaying images in applets and

applications

Trang 3

addMouseListener(this);

addMouseMotionListener(this); // optional

Trang 4

Mouse Events (cont’d)

• The MouseListener interface defines five

methods:

void mousePressed (MouseEvent e)

void mouseReleased (MouseEvent e)

void mouseClicked (MouseEvent e)

void mouseEntered (MouseEvent e)

void mouseExited (MouseEvent e)

• One click and release causes several calls Using only mouseReleased is usually a safe bet

Called when the mouse cursor

enters/exits component’s visible area

Trang 5

Mouse Events (cont’d)

• Mouse listener methods receive a

MouseEvent object as a parameter

• A mouse event can provide the coordinates of the event and other information:

public void mousePressed (MouseEvent e)

Trang 6

Mouse Events (cont’d)

• The MouseMotionListener interface adds two methods:

void mouseMoved (MouseEvent e)

void mouseDragged (MouseEvent e)

• These methods are often used together with

MouseListener methods (the same class

implements both interfaces)

Called when the mouse has moved with a button held down

Trang 7

requestFocus method.

• A component (for example, a JPanel) can

serve as its own key listener:

addKeyListener(this);

Trang 8

Keyboard Events (cont’d)

• The KeyListener interface defines three methods:

void keyPressed (KeyEvent e)

void keyReleased (KeyEvent e)

void keyTyped (KeyEvent e)

• One key pressed and released causes several events

Trang 9

Keyboard Events (cont’d)

• Use keyTyped to capture character keys (that

is, keys that correspond to printable

}

Trang 10

Keyboard Events (cont’d)

• Use keyPressed or keyReleased to handle

“action” keys, such as cursor keys, <Enter>, function keys, and so on

• e.getKeyCode() returns and int, the key’s

“virtual code.”

• The KeyEvent class defines constants for various virtual keys For example:

VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN

VK_HOME, VK_END, VK_PAGE_UP, etc.

Cursor keys

Home, etc.

Trang 11

Keyboard Events (cont’d)

• e.isShiftDown(), e.isControlDown(),

e.isAltDown() return the status of the

respective modifier keys

• e.getModifiers() returns a bit pattern that represents the status of all modifier keys

• KeyEvent defines “mask” constants

CTRL_MASK, ALT_MASK, SHIFT_MASK, and so on

Trang 12

Playing Audio Clips

• The JApplet class has a method getAudioClip

that returns an AudioClip object

tune.play(); There may be a slight

delay The audio clip is actually loaded only when

you call its play method

for the first time.

Trang 13

Playing Audio Clips (cont’d)

• URLPath is the path part of the URL where the audio clip file is located

getDocumentBase() is often used for this parameter

• getDocumentBase() returns the path part of the absolute URL of this applet’s HTML file:

AudioClip bells = getAudioClip ( getDocumentBase(), “sounds/Bells.wav");

Trang 14

Images

• The JApplet class has a method getImage

that returns an Image object

Trang 15

Images (cont’d)

• In applications, it may be easier to work with

Swing’s ImageIcon objects:

(usually this)

Trang 17

The Drawing Editor Program

Trang 18

Drawing Editor (cont’d)

• Allows to move and shape objects on

“canvas” using the mouse and the cursor keys

• Here the shapes are circles (“balloons”)

• Uses JColorChooser to pick a color

Trang 19

Drawing Editor (cont’d)

is a detailed step-by-step plan in

the book (Section 18.4).

ControlPanel

DrawingPanel

Balloon BalloonDraw

Trang 20

Review:

• Name the five methods of the MouseListener

interface

• Can a class implement MouseMotionListener

but not MouseListener?

• What are the units and the origin for the

coordinates returned by the MouseEvent’s

getX and getY methods?

• How many methods does the KeyListener

interface specify?

Trang 21

Review (cont’d):

• Which KeyListener’s method is used to

capture an action key event?

• Which KeyEvent’s method returns the actual character typed?

• Which KeyEvent’s methods return the status

of modifier keys?

• When do we need a requestFocus() call?

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

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

TÀI LIỆU LIÊN QUAN