1 / 14

Computer Science A 11: 24/3

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();

emery-pena
Télécharger la présentation

Computer Science A 11: 24/3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science A 11: 24/3 CS A

  2. Animation - Dubble buffering - Transformations - Sprites and textures - Sound

  3. 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 }

  4. 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); }

  5. 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

  6. Textures, sprites A texture is a tileable image A sprites is a small (often partially transparant) image drawn on top of a background

  7. 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 ... }

  8. Selections: RadioButton: isSelected() CheckBox: isSelected() Spinner: getValue() Slider. getValue() ComboBox: getSelectedItem() List: getSelectedValue()

  9. 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); } }

  10. Timers startTimer(miliseconds,name) stopTimer(name)

  11. 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.

  12. More on graphics: • Transparent shapes • Blending paints • Outline of fonts, font metrics • Gradient paint • Curves • Coordinate transfomations (when writing text) • Timers

  13. 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); } }

  14. Sound playClip(Clip clip) starts the sound, and returns loadClip(String filename) format: .au, .wav, .aiff stopClip(Clip clip) loopClip(Clip clip,int n)

More Related