COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

25
COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics

Transcript of COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

Page 1: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Images, Buffering and Animation

controlling full screen graphics

Page 2: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

A screen manager class

manages the full screen display sets display parameters initializes, terminates full screen display provides access to full screen window take over display of screen to improve

quality

Page 3: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Images

transparency file formats file input

Page 4: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Transparency

three types opaque transparent translucent

Page 5: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Opaque

every pixel displayed

image

Page 6: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Transparent

every pixel displayed or not

image

Page 7: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Translucent

every pixel can be partly transparent

image

Page 8: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

File formats GIF – don’t use it – PNG always better PNG

any colour bit depth any transparency compression

JPEG 24 bit colour only opaque only efficient but lossy compression good for photos for screen use

Page 9: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Reading images - applets

methods in Applet class Image getImage (URL url) Image getImage (URL url, String fName)

image does not start loading till drawing of image begins

Page 10: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Reading images - appletse.g.import java.applet.*; import java.awt.*;public class ImageEG extends Applet{ private Image im; public void init() { im = getImage(getDocumentBase(), ”photo.png”); } public void paint(Graphics g) { g.drawImage(im, 0, 0, this); }}

Page 11: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Reading images - applications

image reader is in default toolkit: this method caches the image

Image im = Toolkit.getDefaultToolkit().getImage(“photo.png”);

to reload file for every displayImage im =

Toolkit.getDefaultToolkit().createImage(“photo.png”);

these methods work like the applet method (load with display)

Page 12: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Reading images - applications

to read an image completely: method does not return till image is

loadedimport javax.swing.*;

...

Image im = new ImageIcon(“photo.png”).getImage();

Brackeen files: ImageTest.java, ImageSpeedTest.java

Page 13: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Animation

animation = shape change + motion set of shapes (frames) are alternated in a

timed sequence each shape is an Image

200 ms 325 ms 625 ms 675 ms 200 ms

im1 im2 im1 im3 im1

one cycle, repeated

Page 14: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Animation Animation class

frames and times total time for one animation cycle current frame current time

200 ms 325 ms 625 ms 675 ms 200 ms

im1 im2 im1 im3 im1

total time of cycle

Page 15: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Animation

store frames in a list (ArrayList) frame (AnimFrame)

image plus duration

ArrayList

0 im1 200

1 im2 325

2 im1 625

3 im3 675

200 ms 325 ms 625 ms 675 ms 200 ms

im1 im2 im1 im3 im1

total time duration

Page 16: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Animation

updating the animation update current time (mod total duration) update current frame

ArrayList

0 im1 200

1 im2 325

2 im1 625

3 im3 675

200 ms 325 ms 625 ms 675 ms 200 ms

im1 im2 im1 im3 im1

total time duration

Page 17: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Animation

animation loop initialize current time from clock while current time < end time

get clock time determine elapsed time, new current time update animation(s) with elapsed time draw animations

Brackeen example AnimationTest1.java

Page 18: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Animation

quality active rendering – control painting

directly (not through event queue) double buffering (remove flicker) synchronizing with monitor refresh

rate (remove tearing) BufferStrategy class

Page 19: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Active rendering full screen window ignores event-queue

paint events fullScreen.ignoreRepaint();

then.. get graphics object for the window paint the window dispose the graphics object

Graphics g = fullScreen.getGraphics(); draw(g); g.dispose();

Page 20: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Double buffering (eliminate flicker) 2 display buffers

first is displayedwhile

second is painted

first, secondswapped

Page 21: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Synchronizing with refresh (eliminate tearing)

switch buffers between refreshes, not during

tear:

Brackeen example AnimationTest2.java

Page 22: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Brackeen’s ScreenManager class(1)

public ScreenManager() public DisplayMode[]

getCompatibleDisplayModes() public DisplayMode

findFirstCompatibleMode(DisplayMode modes[]) public DisplayMode getCurrentDisplayMode() public boolean displayModesMatch(DisplayMode

mode1, DisplayMode mode2) public void setFullScreen(DisplayMode

displayMode)

Page 23: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Brackeen’s ScreenManager class(2)

public JFrame getFullScreenWindow() public int getWidth() public int getHeight() public Graphics2D getGraphics() public void update() public void restoreScreen() public BufferedImage

createCompatibleImage(int w, int h, int transparancy)

Page 24: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Sprites: Shape change and Motion

object that moves on graphic display may change shape also

Sprite object Animation position on screen velocity get and set methods update method computes new position

and Animation image

Page 25: COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.

COSC 4126 images - D Goforth

Spritemovement plus shape change

elapsed time et since last update distance moved

x = x + et * dx y = y + et * dy

Animation determines new image

Brackeen code:Sprite.java

SpriteTest1.java, SpriteTest2.java