ece 462 object-oriented progggramming using … 462 object-oriented progggramming using c++ and java...

90
ECE 462 Object-Oriented Programming using C++ and Java Key Inputs in Java Games Yung-Hsiang Lu l@ d d yunglu@purdue.edu YHL Java Key Input 1

Upload: vannhan

Post on 13-Apr-2018

242 views

Category:

Documents


2 download

TRANSCRIPT

ECE 462Object-Oriented Programmingj g g

using C++ and Java

Key Inputs in Java Games

Yung-Hsiang Lul @ d [email protected]

YHL Java Key Input 1

Handle Key EventsHandle Key Events

• have the focus of the keyboard inputs by calling y p y gsetFocusable(true)

• two types of events:– pressing and releasing– what key (Unicode) is pressed (such as 'A' or '<')

implement three functions• implement three functions– keyPressed(KeyEvent e) – keyReleased(KeyEvent e)keyReleased(KeyEvent e) – keyTyped(KeyEvent e)

YHL Java Key Input 2

YHL Java Key Input 3

YHL Java Key Input 4

YHL Java Key Input 5

YHL Java Key Input 6

YHL Java Key Input 7

YHL Java Key Input 8

YHL Java Key Input 9

YHL Java Key Input 10

YHL Java Key Input 11

YHL Java Key Input 12

YHL Java Key Input 13

Handle Key Events in a Full Screen Game

YHL Java Key Input 14

YHL Java Key Input 15

YHL Java Key Input 16

YHL Java Key Input 17

YHL Java Key Input 18

YHL Java Key Input 19

must be overriddenby derived classes

YHL Java Key Input 20

by derived classes

YHL Java Key Input 21

YHL Java Key Input 22

YHL Java Key Input 23

ECE 462Object-Oriented Programmingj g g

using C++ and Java

Mouse Inputs in Java Games

Yung-Hsiang Lul @ d [email protected]

YHL Java Mouse Input 1

Java Mouse ListenersJava Mouse Listeners

• MouseListener: handle clicks– mouseClicked(MouseEvent e) – mouseEntered(MouseEvent e) – mouseExited(MouseEvent e) – mousePressed(MouseEvent e)

( )– mouseReleased(MouseEvent e) • MouseMotionListener: handle motion

mouseDragged(MouseEvent e)– mouseDragged(MouseEvent e) – mouseMoved(MouseEvent e)

YHL Java Mouse Input 2

YHL Java Mouse Input 3

YHL Java Mouse Input 4

YHL Java Mouse Input 5

YHL Java Mouse Input 6

YHL Java Mouse Input 7

YHL Java Mouse Input 8

YHL Java Mouse Input 9

YHL Java Mouse Input 10

YHL Java Mouse Input 11

YHL Java Mouse Input 12

YHL Java Mouse Input 13

YHL Java Mouse Input 14

YHL Java Mouse Input 15

ECE 462Object-Oriented Programmingj g g

using C++ and Java

Multiple Inheritance in C++

Yung-Hsiang Lul @ d [email protected]

YHL C++ Multiple Inheritance 1

Multiple InheritanceMultiple Inheritance

• One of the most (if not the most) controversial features ( )in C++.

• Not supported in Java. Java uses interfaces.• Most C++ books do not explain the concept and the

problems.• Understand the advantages and the problems and you• Understand the advantages and the problems and you

can decide whether to use it.

YHL C++ Multiple Inheritance 2

Multiple InheritanceMultiple Inheritance

Student Teacher Car Truck

TeachingAssistant

Car Truck

SportUtilityVehicleSportUtilityVehicle

Person

EmployeeWoman Employee

FemaleEmployee

Woman

YHL C++ Multiple Inheritance 3

FemaleEmployee

Design IssuesDesign Issues

• A class provides interface and pimplementation.

• Code reuse is good but a class, d l d i h d t h

Person

W P tonce declared, is hard to change because of the code depending on this class. Employee

Woman Parent

M th• If you have any doubt, do not

create a class.A id th lif ti f l

Mother

• Avoid the proliferation of classes because they make code reuse harder.

MomEmployee

YHL C++ Multiple Inheritance 4

Too Many ClassesToo Many Classes

• In many payroll systems, a person's status (for example y p y y , p ( ptax withholding) depends on the person's role. If a person has children, the person's tax withholding is less.If l i l th d• If an employee is also a mother, a company may send a gift to the children on their birthdays.

• Should you create one class for each possible status ofShould you create one class for each possible status of employee?

• Or, use attributes to distinguish their status?

YHL C++ Multiple Inheritance 5

What Does a Class Give You?What Does a Class Give You?

• interface (public inheritance) and implementation(p ) p• polymorphism• object creation

• If you are not using polymorphism, think twice (or more) before creating a classbefore creating a class.

• It is usually easier to change the behavior of an attribute (encapsulation) than changing the interface ( p ) g g(code reuse).

YHL C++ Multiple Inheritance 6

Repeated InheritanceRepeated Inheritance

class Vehicle{

int v_engineSize; Vehicle

int v_numberWheel;}; Car Truck

Does SportUtilityVehicle have onev engineSize or two?

SportUtilityVehicle

v_engineSize or two?⇒ two, unless you use virtual inheritance⇒ ambiguous, compilation error

YHL C++ Multiple Inheritance 7

⇒ ambiguous, compilation error

YHL C++ Multiple Inheritance 8

YHL C++ Multiple Inheritance 9

Virtual InheritanceVirtual Inheritance

YHL C++ Multiple Inheritance 10

YHL C++ Multiple Inheritance 11

YHL C++ Multiple Inheritance 12

Virtual Inheritance and Copy Constructor

YHL C++ Multiple Inheritance 13

X

Y Tvirtual virtual

Z

U

YHL C++ Multiple Inheritance 14

YHL C++ Multiple Inheritance 15

YHL C++ Multiple Inheritance 16

YHL C++ Multiple Inheritance 17

YHL C++ Multiple Inheritance 18

Virtual Inheritance and Assignment Operator

YHL C++ Multiple Inheritance 19

YHL C++ Multiple Inheritance 20

YHL C++ Multiple Inheritance 21

YHL C++ Multiple Inheritance 22

YHL C++ Multiple Inheritance 23

YHL C++ Multiple Inheritance 24

YHL C++ Multiple Inheritance 25

ECE 462Object-Oriented Programmingj g g

using C++ and Java

Qt Layout

Yung-Hsiang Lul @ d [email protected]

YHL Qt Layout 1

YHL Qt Layout 2

YHL Qt Layout 3

YHL Qt Layout 4

YHL Qt Layout 5

YHL Qt Layout 6

YHL Qt Layout 7

YHL Qt Layout 8

Menubar and Vertical LayoutMenubar and Vertical Layout

YHL Qt Layout 9

YHL Qt Layout 10

YHL Qt Layout 11

YHL Qt Layout 12

YHL Qt Layout 13

YHL Qt Layout 14

Horizontal LayoutHorizontal Layout

YHL Qt Layout 15

YHL Qt Layout 16

YHL Qt Layout 17

Grid LayoutGrid Layout

YHL Qt Layout 18

YHL Qt Layout 19

YHL Qt Layout 20

YHL Qt Layout 21

YHL Qt Layout 22

Combination of LayoutsCombination of Layouts

YHL Qt Layout 23

YHL Qt Layout 24

YHL Qt Layout 25

YHL Qt Layout 26

YHL Qt Layout 27