עקרונות תכנות מונחה עצמים תרגול 7: אנימציה

Slides:



Advertisements
Similar presentations
Web Design & Development Lecture 19. Java Graphics 2.
Advertisements

Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
JFrame JComponent JFrame JComponent JFrame JComponent.
Events CSC 171 FALL 2001 LECTURE 9. History: the ABC John Vincent Atanasoff, with John Berry, developed the machine we now call the ABC -- the.
Mouse Events and Keyboard Events
1 Lecturte 14Lecture 7 Applications of Graphics Overview  Conversions Between Applications and Applets  Handling Mouse Events  Handling Keyboard Events.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
Applet Class Hierarchy. Applet Methods called by the system public void init() When the applet is first loaded. Do initialization here. public void start()
Graphical User Interfaces
Animation To animate our programs, that is, to have real time interactions where objects move on the screen, we want to call repaint( ) every few milliseconds.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
CS12420 – Swing and threads Lynda Thomas
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
© A+ Computer Science - Chicken yeller = new Chicken();
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
 Definition: An event is an object thrown in response to a user of programmatic action  Definition: A listener is a series of methods that executes in.
Concurrent Programming and Threads Threads Blocking a User Interface.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Animation To animate our programs, that is, to have real time interactions where objects move on the screen, we want to call repaint( ) every few milliseconds.
Java Dynamic Graphics.
עקרונות תכנות מונחה עצמים תרגול 6 - GUI. Outline  Game of Life  Painting.
Practical Session 10: Animation. Animation kinds There are three basic kinds of animation: 1. Moving object 2. Changing pictures 3. Combination of 1 and.
1 Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Computer Science 209 GUIs Model/View/Controller Layouts.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
A cannon game ?. Simple version angle from command line, one shot only Coordinate system is “upside-down”: Use dy(int) method to transform y coordinate:
עקרונות תכנות מונחה עצמים תרגול 7: אנימציה. בשבוע שעבר  paint  Graphics  repaint.
Nested Classes and Event Handling Chapter 10. Overview We explain how to write Java code that responds to events. The Timer class can be used to respond.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
GUI Tutorial 2. What did we do last time?  Basic flow  instance variables, set up in ctor, close operation, size, visible  JFrame  Event-driven programming.
KeyListener and Keyboard Events Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) –to implement.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Chapter 4 Interface Types and Polymorphism Part 2.
Modular Event Handling
Chapter 14 Event-Driven Programming
Lecture 10 Graphics D&D 3.14, 4:10, 5.13, 6.15, 8.16, Date.
Lecture 9.5 Event Delegation.
Multimedia in Java Multimedia combines graphics, animation and sound
Chapter 4 Interface Types and Polymorphism Part 2
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Project Snake. Project Snake Snake For this project, we’re going to make Snake This includes Making the overall game logic (using a 2D array) Handling.
עקרנות תכנות מונחה עצמים
עקרונות תכנות מונחה עצמים
Drawing in Java.
Chapter 16 Event-Driven Programming
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
עקרונות תכנות מונחה עצמים תרגול 7: אנימציה
Presentation transcript:

עקרונות תכנות מונחה עצמים תרגול 7: אנימציה

בשבוע שעבר paint Graphics repaint

בשבוע שעבר

בשבוע שעבר

Outline Animation The flickering problem Assignment 3

Animation kinds There are three basic kinds of animation: 1. Moving object 2. Changing pictures 3. Combination of 1 and 2.

Timer In order to get animation effect we need to update an image several times per second. We use javax.swing.Timer. Constructor syntax: Timer t = new Timer (delay , actionListener) A Timer periodically creates events of type ActionPerformed. For each event, the image can be changed or moved.

Example 1 - Ball public class Ball extends JFrame implements ActionListener{ private Timer timer; private int x, y; private final int delay = 200; public Ball(){ x = 0; y = 0; timer = new Timer(delay,this); timer.start(); } public void paint(Graphics g){ super.paint(g); g.fillOval(x,y, 10, 10); public void actionPerformed(ActionEvent e){ if (e.getSource() == timer){ x = x + 5; y = y + 5; repaint();

Example 2 – Moving Car

Changing Pictures 1. Create an array of pictures. im = new ImageIcon[3]; im[0] = new ImageIcon("car1.gif"); im[1] = new ImageIcon("car2.gif"); im[2] = new ImageIcon("car3.gif"); 2. Define a field that represents the current image number. private int frame = 0; 3. The paint method draws the appropriate image. im[frame].paintIcon(this, g, x, y); 4. ActionPerformed, which is invoked by a timer, changes the current image. frame = (frame + 1) % 3;

public class Car extends JPanel implements ActionListener{ private int frame; private ImageIcon[] im; private Timer timer; private int x, y, dx, dy; public Car(){ frame = 0; x = 30; y = 30; dx = 15; dy = 15; im = new ImageIcon[3]; // . . . (create three images and assign to the array) timer = new Timer(200,this); timer.start(); } public void actionPerformed(ActionEvent e){ if (e.getSource() == timer){ frame = (frame + 1) % 3; x = (x + dx) % getSize().width; y = (y + dy) % getSize().height; repaint(); public void paint(Graphics g){ super.paint(g); im[frame].paintIcon(this, g, x, y); }

Responding to key press When a key is pressed a KeyEvent is initiated. Listening to KeyEvents is performed with KeyListener. public class Frame extends JFrame implements KeyListener{ . . . public void keyPressed(KeyEvent e) { int code; code = e.getKeyCode()); } public static void main(String args []){ Frame frame = new Frame(); frame.addKeyListener(frame);

Changing the Car example to respond to key press public class Car extends JPanel implements ActionListener, KeyListener{ . . . public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT){ dx = -30; dy = 0; } if (e.getKeyCode() == KeyEvent.VK_RIGHT){ dx = 30; dy = 0; if (e.getKeyCode() == KeyEvent.VK_UP){ dx = 0; dy = -30; if (e.getKeyCode() == KeyEvent.VK_DOWN){ dy = 30; dx = 0;

Double Buffering Technique

The double buffering technique allows to overcome the flickering problem. Flickering appears because a drawing is deleted before it is repainted, and repaint takes time. Solution: Maintain an additional panel. Draw a new image on the additional panel. Replace the old image only once the new one is ready.

Fixing the flickering problem in the Drawing-Canvas example Reminder: The original paint method of DrawingCanvas public void paint(Graphics g){ super.paint(g); for (int i = 0; i < matrix.size(); i++) for (int j =0; j < matrix.get(i).size(); j++) if (matrix.get(i).get(j).intValue() == 1) g.fillOval(i,j,5,5); g.fillOval(x,y,20,20); }

Fixing the flickering problem in the Drawing-Canvas example The corrected paint method public void paint(Graphics g){ //super.paint(g); Image offIm = createImage(getSize().width, getSize().height); Graphics offGr = offIm.getGraphics(); for (int i = 0; i < matrix.size(); i++) for (int j =0; j < matrix.get(i).size(); j++) if (matrix.get(i).get(j).intValue() == 1) offGr.fillOval(i, j, 5, 5 ); offGr.fillOval(x,y, 20, 20); g.drawImage(offIm, 0,0, this); }

סיכום אנימציה javax.swing.Timer paint and repaint KeyListener Double buffering