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). |
CS 340 Milestone One Guidelines and Rubric Overview: For this assignment, you will implement the fundamental operations of create, read, update,
Retail Transaction Programming Project Project Requirements: Develop a program to emulate a purchase transaction at a retail store. This
7COM1028 Secure Systems Programming Referral Coursework: Secure
Create a GUI program that:Accepts the following from a user:Item NameItem QuantityItem PriceAllows the user to create a file to store the sales receip
CS 340 Final Project Guidelines and Rubric Overview The final project will encompass developing a web service using a software stack and impleme