JAVAFX - GRAPHIC

Post on 18-Dec-2021

20 views 1 download

Transcript of JAVAFX - GRAPHIC

JAVAFX - GRAPHIC

CONTENT

• Graphics• Canvas, GraphicsContext

• AnimationTimer

• Design• Shared Object, Drawing Part, Logic Part

• Handling User Input• Mouse, Keyboard

• Audio

• Export Jar

• Conclusion

2

Graphics

Drawing

• Where to draw ?

• How to draw ?

4

Canvas

• Empty component

• See it as Paper (Where to draw)

• Can draw text, shapes, lines and images using a set of graphics commands provided by a GraphicsContext.

• Canvas has 2 constructors• Canvas canvas = new Canvas();

• Create a Canvas of zero width and height• Can set width and height later

• Canvas canvas = new Canvas(double width, double height);

5

Canvas - Coordinate system

• Vertically flipped version of real world

6

(0,0) +X

+Y

Example : FxCanvasExample0.java 7

GraphicsContext

• Drawing class

• See it as Pen, Pencil, Brush (How to draw)

• Contains a wealth of powerful customization abilities

• GraphicsContext gc = canvas.getGraphicsContext2D()• Get the graphics context of the canvas

• Drawings that fall outside the bounds of the Canvas are clipped

8

GraphicsContext

• setLineWidth(Double lw)

• setFill(Paint p)

• setStroke(Paint p)

• restore()• Used to remove all properties from GraphicsContext

9

Example : FxCanvasExample1.java 10

Drawing Example 11

Drawing

• Basic shapes

• Text

• Paths

• Images

• Rotate

12

Drawing - Basic Shapes

• Basic Shapes• fillRect(), fillRoundRect(), fillOval(), fillArc()

• strokeLine(), strokeRect(), strokeRoundRect(), strokeOval(), strokeArc()

• clearRect()

• fillPolygon()

• strokePolygon(), strokePolyline()

13

• Basic drawing methods

• setStroke (the outline) // set property

• setFill (the shape) // set property

• FillRect // draw here

• gc.fillRect(int x, int y, int width, int height)

Drawing 2D shapes 14

(0,0)

(x,y)

height

width

Example : FxCanvasExample2.java 15

Example : FxCanvasExample2.java 16

Drawing - Text

• Text• setFont(Font f)• getFont()• fillText(Text t, int x, int y)• fillText(Text t, int x, int y, double maxWidth)• strokeText(Text t, int x, int y)• strokeText(Text t, int x, int y, double maxWidth)

• Font• Font.font(String family, FontWeight weight, FontPosture posture, int size)• More…

17

Hello World

(0,0)

(x,y)

Example : FxCanvasExample3.java 18

Example : FxCanvasExample3_2.java 19

Look both have

same position x,y

Drawing - Text

• How to draw Text in Rect?• We need to get text width and height

• -> use FontLoader

20

NO longer available!

Drawing - Text

• FontLoader fontLoader = Toolkit.getToolkit().getFontLoader();

• Width

• double font_width = fontLoader.computeStringWidth("This is a filled Text", gc.getFont());

• Height

• double font_height = fontLoader.getFontMetrics(gc.getFont()).getLineHeight();

• For every text that use the same font will have same height

21

NO longer available!

Example : FxCanvasExample3_3.java 22

NO longer available!

Drawing - Paths

• Paths• A path consists of multiple subpaths• beginPath()

• Resets the current path to empty

• closePath()• Closes the path

• moveTo() , lineTo(), quadraticCurveTo(), bezierCurveTo(), arc(), arcTo(), appendSVGPath(), rect()

• stroke(), fill()• draw an outline or fill the path

23

Example : FxCanvasExample4.java 24

Drawing - Images

• Can draw an Image on the Canvas using the drawImage() method

• Can draw the whole or part of the Image

• The drawn image can be stretched or shortened on the canvas

• void drawImage(Image img, double x, double y)

• void drawImage(Image img, double x, double y,

double w, double h)

25

(x,y)

h

w

Drawing - Images

• Image• new Image(InputStream is)

• new Image(String url)• url = image path in pc or web url

• new Image(String url, double requestedWidth, double requestedHeight, boolean preserveRatio, boolean smooth)

26

Use this to resize image

Example : FxCanvasExample5.java 27

Drawing - Images

• SubImage• WritableImage croppedImage = new

WritableImage(image.getPixelReader(), x, y, h, w);

• (x,y) is the rectangle’s top-left related to image’s top-left

28

w

h

(x,y)

(50,50)

(250,150)

100

200

Example : FxCanvasExample5_2.java 29

Other Topic - Rotate

• Rotate• gc.rotate(double degrees)

• gc.drawImage(image,100,50)

• It seem like rotate paper(Canvas) then draw something

30

+X

+Y

+X

+Y

img

Normal Rotate and Draw Show on Screen

Other Topic - Rotate 31

Canvas

GraphicsContext

img

Image

Other Topic - Rotate 32

Normal

+X

+Y

Rotate Draw

Example : FxCanvasExample8.java 33

Example : JAVA_FX_TankGame -Tank

• This code receive input from user

• W -> go forward

• A -> turn left x degree

• D -> turn right x degree

34

AnimationTimer

AnimationTimer

• make our programs dynamic, meaning that the game state changes over time

• implement a game loop: an infinite loop that updates the game objects and renders the scene to the screen

• AnimationTimer will be called at a rate of 60 times per second or as close to that rate as is possible

36

Example : FxCanvasExample7.java 37

Design

Design

• Normally, we want to draw many objects on our screen

• Objects with different shapes should have different drawing methods

• At the same time, aside from drawing, we might want to update those objects’ state

39

Design Component

• With requirements in the previous slide, the program should look like this

40

Application logic

Shared object

Drawing module

Update object’s stateAlso manage it

Observe object and draw according to its state

MainJAVAFX

Design Component

• Shared Object

• Drawing Part

• Logic Part

41

Design Component

• Shared Object

• Drawing Part

• Logic Part

42

Shared Object - Requirement

• Back to our requirements

• Objects with different shapes should have different drawing methods void draw(GraphicsContext gc)

• Objects may overlap• We need “ordering” int getZ()

• Z=9 -> Foregound• Z=-999 -> Background

• Add more capabilities to the object• Able to hide/show boolean isVisible()

43

Interface IRenderable

Shared Object - IRenderable

public interface IRenderable {

public int getZ();

public void draw(GraphicsContext gc);

public boolean isVisible();

}

44

Ordering

Draw

Hide/Show

Polymorphism

Shared Object -RenderableHolder 45

For example:

- ArrayList<IRenderable>

- LinkedList<IRenderable>

Anything to keep track of “all” IRenderable

• We want to draw many objects on our screen

• The application logic also access these objects

A shared Collection of generic interface (IRenderable)

Shared Object -RenderableHolder 46

Constructor

Collection

Shared Object – RenderableHolder -Sort

• RenderableHolder class• Any collection with <IRenderable>

ArrayList<IRenderable>

• Methods for accessing the collection

47

- The collection is used to keep track of objects that have to be

drawn on the screen

- IRenderable in the collection must be sorted by Z (So you can just loop through the collection and draw the deepest one first)

Shared Object - RenderableHolder -Sort

• RenderableHolder class• Any collection with <IRenderable>

ArrayList<IRenderable>

• Methods for accessing the collection -> add(IRenderable)

• Method for remove unused object from the entity list -> update()

48

-add(IRenderable)

add new IRenderable object to the list and sort the list

(according to Z)

Shared Object - RenderableHolder -Sort 49

Use to sort IRenderable order by Z

Add Irenderable to ArrayList

then sort using comparator

Shared Object –RenderableHolder - Singleton

• We can declare our shared collection as a field

• BUT:• According to our design, the collection does not belong

to either the logic part (application logic) or the drawing part (drawing module)

• We only need a single collection to store everything that must be drawn

• We encapsulate our collection in RenderableHolderclass and use a singleton pattern to ensure that there is only one instance of RenderableHolder

50

Shared Object -RenderableHolder - Singleton

• Singleton pattern• A design pattern for a class that can be instantiated

to only one object

• Good when the program only need a single object shared across every part

51

private static final RenderableHolder instance = new RenderableHolder();public static RenderableHolder getInstance(){

return instance;}

Shared Object -RenderableHolder - Singleton 52

Example : JAVA_FX_TankGame -GameScreen

• This is how to use RenderableHolder from other Classes.

53

This class wants to iterate all entities

Example : JAVA_FX_TankGame -GameLogic

• This is how to use RenderableHolderfrom other Classes.

54

This class wants to add entities

Shared Object - RenderableHolder -LoadResource

• Resource holder• Used to keep required resources in the memory

• Resources are either pre-loaded or loaded on use

• uses static initializer to load all resources on the first use

55

Shared Object - RenderableHolder -LoadResource 56

Load Resource once

Then

Use Everywhere

Example : JAVA_FX_TankGame -RenderableHolder 57

Singleton

Load resource only 1 time

How to sort

Remove dead entity

Add entity and then

sort

Design Component

• Shared Object

• Drawing Part

• Logic Part

58

Drawing Part

• Drawing Part• A simple subclass of Canvas that look at the shared

collection and draw all objects

59

Example : JAVA_FX_TankGame -GameScreen 60

Design Component

• Shared Object

• Drawing Part

• Logic Part

61

Logic Part

• Logic Part• This part is responsible for creating/removing

objects and updating their state

• When creating an object, if it must be drawn to the screen (that object is an IRenderable), add it to RenderableHolder

• When removing an object, if it was added to RenderableHolder, don’t forget to remove it from the holder

62

Design Component

• With requirements in the previous slide, the program should look like this

63

Application logic

Shared object

Drawing module

Update object’s stateAlso manage it

Observe object and draw according to its state

MainJAVAFX

Logic Part

• Drawing Module• Need to loop all Renderable.list to

draw all object

• Logic Module• Need to loop all Renderable.list to

update object

• Sometime, Logic Module access list while Drawing module have not finished draw all object

• This will cause Concurrent Modification Exception

64

Drawing Module

Logic Module

Time

Logic Part

• To update objects that are added to the holder, there are 2 approaches:

1. Loop through all objects in the holder and update any object needed (casting required)

2. Cache local references of those objects and update through these references

65

Logic Part – Update Object

• Approach 1 : Problem• Application logic access shared object at the same

time with Drawing module access shared object

66

Application logic

Shared object

Drawing module

Update object’s stateAlso manage it

Observe object and draw according to its state

MainJAVAFX

Logic Part – Update Object

• Approach 1 : • Loop through all objects in the holder and update

any object needed

• Problem : • Application logic access shared object at the same

time with Drawing module access shared object

• Solution :• using synchronize (Not in our scope!)

67

Logic Part – Update Object

• Approach 2 : • Cache local references of those objects and update

through these references

68

RenderableHolder

Application logic

Cached objects

Drawing module

Collection

Shared object

1. Create object

2. Add to both

3. Update

Look and draw

Example : JAVA_FC_TankGame -GameLogic

• Create object

• Add same object in both Lists.

69

Handling User Input

Handling User Input

• Detecting and processing user input in JavaFX is straightforward

• User actions that can be detected by the system, such as key presses and mouse clicks, are called events

• Any JavaFX class which implements the EventTarget class, such as a Scene, can "listen" for events and handle them

71

Handling User Input

• There are many methods that listen for handling different types of input from different sources

• setOnKey……() can assign an EventHandler that will activate when a key is ……

• setOnMouse…… () can assign an EventHandler that activates when a mouse button is ……

• The EventHandler class serves one purpose: to encapsulate a method (called handle()) that is called when the corresponding event occurs.

72

Handling User Input Example

• Example of Key Event

73

Handling User Input Example

• Example of Mouse Event

74

Input handling

Event-driven• A program handle an

event immediately

• Listener & Event

Polling• Periodically check if

there is an input from a user

75

Button

Program

1. Event occur

2. Notify

Program

Button

Button

Event occur?

No

Event occur?

YES!!

Input handling

• JVM provides user’s input to JavaFX application via callback method in a listener which is an event-driven method

• This means we don’t not know when the input is coming

• In contrast, the input checking code requires the exact time to operate!

• Therefore, it is required to poll the incoming event which is polling method

76

Input handling - Event-driven

• Event-driven• JVM provides user’s input to JavaFX application via

callback method in a listener

• This means we don’t not know when the input is coming

77

Event listener

Our logic

Example : JAVA_FX_InputHanding_Event_Driven 78

Trigger -> draw

Input handling - Polling

• Polling• the input checking code requires the exact

placement in the application code. Therefore, it is required to poll the incoming event

• The idea• When an event takes place, notes it down

somewhere

• Our logic polls the noted event

79

Event listener

Input flag

Our logic

Note

Input handling - Polling

• Polling example

80

While(true){Sleep for 20 msDraw screenUpdate logic (poll the event here)

}

If (users press ENTER){Do action 1

}If(users press SPACE){

Do action 2}

Example : JAVA_FX_InputHanding_Polling 81

Trigger -> store value

Loop to redraw

Get code value

Input handling – Pressing VS Triggering

• Pressing VS Triggering• When user press and hold a button

• If we poll for pressing event : it must remain TRUE until user releases the button

• If we poll for triggering event : it must be TRUE on the FIRST TICK of button pressing ONLY

82

Tick 1 Tick 2 Tick 3 Tick 4 Tick 5 Tick 6

Press and hold Release

Pressing event

Triggering event

Example : JAVA_FX_Key 83

Example : JAVA_FX_Key

• This happens when we press “S” and hold

(This should be 1)

84

Start Program Press S Hold S

Input handling – Pressing VS Triggering

• Implementation to poll for pressing and triggering event• Take note of pressing and triggering event

separately when “pressed” event happens

• Pressing flag changes to FALSE on button releasing

• Triggering flag changes to FALSE at the end of every tick

85

Example : JAVA_FX_KeyUpgrade 86

Example : JAVA_FX_KeyUpgrade

Action Code Counter Pressed Triggered

Start Program 0 False False

Press S S 1 True True

CodeUtility.postUpdate() S 1 True False

Hold S S 1 True False

CodeUtility.postUpdate() S 1 True False

Release S S 1 False False

CodeUtility.postUpdate() S 1 False False

Press S S 2 True True

CodeUtility.postUpdate() S 2 True False

87

Example : JAVA_FX_KeyUpgrade

• This happens when we press “S” continuously

88

Start Program Press S Hold S Release S Press S

Input handling – Pressing VS Triggering

• When we have multiple input at the same time

• One way to accomplish this is by creating an ArrayList of String objects

• When a key is initially pressed, we add the String representation of the KeyEvent's KeyCode to the list

• When the key is released, we remove it from the list.

89

Input handling – Pressing VS Triggering

• Implement method • Getter and setter of keyPressed, KeyTriggered and

KeyTriggerFlag yourself

(><)// wish u can do it by yourself

90

Input handling – Pressing VS Triggering

• Triggering event: Keyboard VS Mouse

• MouseListener fires “mousePressed” event only once when mouse button is pressed and held

• KeyListener fires “keyPressed” event continuously (about every tick) as long as a button is pressed and held

• Hold key? we only note down triggering event on the first tick of button holding : The tick that “pressed” flag change from FALSE to TRUE

91

Example : JAVA_FX_Mouse

• Details of the program:• Click to increase the counter

• Click & hold considers as “1 click”

• The program should differentiate between “click” vs. “click & hold”

92

Input handling – Mouse

• This happens when we hold “left click”

93

Start Program Left Click Hold Left ClickRelease

Left ClickLeft Click

Graphics + Input

RenderableHolder

Put everything together 95

Application logic

Cached objects

Drawing module

CollectionShared object

1. Create object

2. Add to both

3. Update

Look and drawInput

tracker

Button

A. Event occur

B. Take note(set flag)

4.1 Poll

Put everything together

• Put everything together : JAVA_FX_TankGame• Controllable tank

• W: Forward• A: Turn left• D: Turn right• Shift: Hide• Click: Warp

• The code• Drawing the battlefield• A moving tank

96

Input Handling –JAVA_FX_TankGame

• GameScreen Class

97

Key Event

Mouse Event

Audio

Java sound

• New JAVA sound API in JavaFX• javafx.scene.media.AudioClip

• Constructor• AudioClip sound = new AudioClip(String source)

• Very easy to use• sound.play• sound.stop() • sound.setCycle() • sound.setVolume()

99

Example : JAVA_FX_Sound 100

But It can’t be

exported!! You

need to use

GetSystemResource

Example : JAVA_FX_Sound_Fix -Main 101

Put everything together

• Put everything together : JAVA_FX_TankGame• Move tank to the same position of mine

• It will cause Explosion sound

• The code• Drawing the battlefield

• A moving tank

102

Java sound – JAVA_FX_TankGame 103

Conclusion

What you’ve learned

• Drawing on Canvas

• Input polling based on listener & event

• (Very simple) audio playback

• Application design pattern example

105

Last Suggestion 106

Google is your

best friend

Credit

• https://openjfx.io/javadoc/15/javafx.graphics/javafx/scene/canvas/Canvas.html

• https://examples.javacodegeeks.com/desktop-java/javafx/javafx-canvas-example/

• https://openjfx.io/javadoc/15/javafx.graphics/javafx/scene/canvas/GraphicsContext.html

• http://zetcode.com/gui/javafx/canvas/

• https://gamedevelopment.tutsplus.com/tutorials/introduction-to-javafx-for-game-development--cms-23835

• https://jaxenter.com/tutorial-a-glimpse-at-javafxs-canvas-api-105696.html

107