Upload
ronald-robinson
View
217
Download
1
Tags:
Embed Size (px)
Citation preview
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
COSC 4126 images - D Goforth
Images
transparency file formats file input
COSC 4126 images - D Goforth
Transparency
three types opaque transparent translucent
COSC 4126 images - D Goforth
Opaque
every pixel displayed
image
COSC 4126 images - D Goforth
Transparent
every pixel displayed or not
image
COSC 4126 images - D Goforth
Translucent
every pixel can be partly transparent
image
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
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
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); }}
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)
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
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
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
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
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
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
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
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();
COSC 4126 images - D Goforth
Double buffering (eliminate flicker) 2 display buffers
first is displayedwhile
second is painted
first, secondswapped
COSC 4126 images - D Goforth
Synchronizing with refresh (eliminate tearing)
switch buffers between refreshes, not during
tear:
Brackeen example AnimationTest2.java
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)
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)
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
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