Introduction
Key & Mouse
In this unit you will learn how to build programs that interact with your Canvas through the mouse and keyboard.
Responding to Mouse Actions
When you want a canvas to respond to mouse actions you will need to add an event handler for each mouse action, that needs a response. An event handler is a class that dictates how a specific type of event should be handled. Below is a list of common mouse actions and the canvas methods that are used to track those actions:
Mouse Event |
Method Name |
Mouse button is pressed down |
setOnMousePressed(EventHandler<MouseEvent> event) |
A pressed mouse button is released |
setOnMouseReleased(EventHandler<MouseEvent> event) |
A mouse button is pressed downand then released, without the mouse moving. |
setOnMouseClicked(EventHandler<MouseEvent> event) |
The mouse moves |
setOnMouseMoved(EventHandler<MouseEvent> event) |
The mouse moves while a button isin the down state |
setOnMouseDragged(EventHandler<MouseEvent> event) |
Adding a MouseEventHandler Format the Long Way:
canvasName.mouseEventMethodName(new EventHandler<MouseEvent>() { public void handle (MouseEvent mouseEvent) {
// code to respond the event
}
});
Adding a MouseEventHandler Format the Short Way:
canvasName. mouseEventMethodName(mouseEvent->
// code to respond the event
);
Which Button Caused the Event?
After an event has occurred, you will can retrieve the button that caused the event by calling getButton().name() from the mouse event. Below are the String values that correspond to each event:
Button |
Text |
Left |
PRIMARY |
Middle |
MIDDLE |
Right |
SECONDARY |
Where did the Event Occur?
After an event has occurred, you will can retrieve the x and y locations of where the event occurred with the following two methods:
Return Type |
Method Name |
Description |
double |
getX() |
Returns the x of where theevent occurred. |
double |
getY() |
Returns the y of where theevent occurred. |
Responding to Keys
When you want a canvas to respond to key actions you will need to add an event handler for each key action, that needs a response. Below is a list of key actions and the canvas methods that are used to track those actions:
Mouse Event |
Method Name |
A key is pressed down |
setOnKeyPressed(EventHandler<KeyEvent> event) |
A key pressed key goes up |
setOnKeyReleased (EventHandler< KeyEvent > event) |
A key goes down and they up |
setOnKeyTyped(EventHandler< KeyEvent > event) |
Adding a MouseEventHandler Format the Long Way:
canvasName. keyEventMethodName (new EventHandler<MouseEvent>() { public void handle (MouseEvent mouseEvent) {
// code to respond the event
}
});
Adding a MouseEventHandler Format the Short Way:
canvasName. keyEventMethodName (mouseEvent->
// code to respond the event
);
Which Key Caused the Event?
After an event has occurred, you will can retrieve the key that caused the event by calling getKeyCode().getName() from the key event. For non-displayable characters you will need to check the check the API Documentation to determine what those keys produce as an answer.
Loading & Drawing Images
For images we will be using the Image class.
We will load images using the below code:
image = new Image("file:fileName.fileType");
Example:
image = new Image("file:deadwumpus.gif");
Once an image has been loaded we will use the following GraphicsContext method to draw it to the
screen:
Return Type |
Method Name |
Description |
void |
drawImage(Image img,int x, int y) |
Draws the given image to thescreen at (x,y). |
DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma
Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t
Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th
1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of
1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of