140 likes | 262 Views
Computer Science A 11: 24/3. CS A. Animation. - Dubble buffering - Transformations - Sprites and textures - Sound. Dubble buffering. Basic pinciple of animation: use two display areas: one to draw in and one to display while(true){ canvas.startBuffer(); canvas.clear();
E N D
Animation - Dubble buffering - Transformations - Sprites and textures - Sound
Dubble buffering Basic pinciple of animation: use two display areas: one to draw in and one to display while(true){ canvas.startBuffer(); canvas.clear(); … draw image canvas.endBuffer(); canvas.sleep(20); // display image }
Bouncing ball while(true){ int mx=canvas.getWidth(); int my=canvas.getHeight(); canvas.startBuffer(); canvas.clear(); canvas.setPaint(Color.green); x+=vx; y+=vy; if(x<0){x=0;vx=-vx;} if(y<0){y=0;vy=-vy;} if(x+d>mx){x=mx-d;vx=-vx;} if(y+d>my){y=my-d;vy=-vy;} canvas.fillOval(x,y,d,d); canvas.endBuffer(); canvas.sleep(20); }
Coordinate transformations You want to draw some model (chair, box, house,..) This will often involve several transformations: • Coordinates of parts of model in own system • Coordinates of model in world coordinates • Transform coordinates so view point is on z-axis • Make 3d projection: multiply size with the inverse of distance to view point • Transform to window coordinates • Draw model: furthest away first, nearest last
Textures, sprites A texture is a tileable image A sprites is a small (often partially transparant) image drawn on top of a background
JEventQueue JEventQueue events = new JEventQueue(); events.listenTo(button1,"button1"); events.listenTo(button2,"button2"); ... while(true){ EventObject event = events.waitEvent(); String name=events.getName(event); if(name.equals("button1")){ ... }else if(name.equals("button2")){ ... }else ... }
Selections: RadioButton: isSelected() CheckBox: isSelected() Spinner: getValue() Slider. getValue() ComboBox: getSelectedItem() List: getSelectedValue()
Mouse events Draw circles where you click and lines where you drag. int x0=0,y0=0; while(true){ EventObject event=events.waitEvent(); if(events.isMouseEvent(event)){ int x=events.getMouseX(event); int y=events.getMouseY(event); if(events.isMousePressed(event)){x0=x;y0=y;} if(events.isMouseClicked(event)) canvas.drawOval(x0-5,y0-5,10,10); if(events.isMouseReleased(event)) canvas.drawLine(x0,y0,x,y); } }
Timers startTimer(miliseconds,name) stopTimer(name)
Window events Events when closing or resizing a window Bring up a dialog when you close a window so that the user can confirm whether the user wants to exit the program.
More on graphics: • Transparent shapes • Blending paints • Outline of fonts, font metrics • Gradient paint • Curves • Coordinate transfomations (when writing text) • Timers
Sound import javax.sound.sampled.Clip; public class PlaySound{ public static void main(String args[]){ Clip crash = JCanvas.loadClip("crash.au"); JCanvas.playClip(crash); JCanvas.sleep(4000); } }
Sound playClip(Clip clip) starts the sound, and returns loadClip(String filename) format: .au, .wav, .aiff stopClip(Clip clip) loopClip(Clip clip,int n)