Docsity

Animation - Web Design and Development - Lecture Handouts, Lecture notes of Web Design and Development

Toggle sidebar
bg1
Web Design & Development CS-506
- 225 -
Lecture 19
How to Animate?
If we want to animate something like ball, moving from one place to another, we
constantly need to call paintComponent( ) method and to draw the shape (ball etc.) at new
place means at new coordinates.
Painting is managed by system, so calling paintComponent() directly is not recommended
at all. Similarly calling paint( ) method is also not recommended. Why? Because such
code may be invoked at times when it is not appropriate to paint -- for instance, before
the component is visible or has access to a valid Graphics object.
Java gives us a solution in the from of repaint( ) method. Whenever we need to repaint,
we call this method that in fact makes a call to paint( ) method at appropriate time.
Problem & Solution
๎šƒ What to do to move the shapes present in example code 18.1 (last example) when a
mouse is dragged
๎šƒ First time painting is what we already have done
๎šƒ When a mouse is clicked find the co-ordinates of that place and paint Rectangle at
that place by requesting, using repaint() call
๎šƒ Here instead of Hard-coding the position of co-ordinates uses some variables. For
example mx , my
โ€“ In the last example code, we draw a rectangle by passing hard-coded values
like 20
g.drawRect(20,20,20,20);
โ€“ Now, weโ€™ll use variables so that change in a variable value causes to display a
rectangle at a new location
g.drawRect(mx,my,20,20;
๎šƒ Similarly, you have seen a tennis game (during lecture). Now, what to do code the
paddle movement.
๎šƒ In the coming up example. We are doing it using mouse, try it using mouse.
pf3
pf4
pf5

Partial preview of the text

Download Animation - Web Design and Development - Lecture Handouts and more Lecture notes Web Design and Development in PDF only on Docsity!

Lecture 19

How to Animate?

If we want to animate something like ball, moving from one place to another, we

constantly need to call paintComponent( ) method and to draw the shape (ball etc.) at new

place means at new coordinates.

Painting is managed by system, so calling paintComponent() directly is not recommended

at all. Similarly calling paint( ) method is also not recommended. Why? Because such

code may be invoked at times when it is not appropriate to paint -- for instance, before

the component is visible or has access to a valid Graphics object.

Java gives us a solution in the from of repaint( ) method. Whenever we need to repaint,

we call this method that in fact makes a call to paint( ) method at appropriate time.

Problem & Solution

ยƒ What to do to move the shapes present in example code 18.1 (last example) when a

mouse is dragged

ยƒ First time painting is what we already have done

ยƒ When a mouse is clicked find the co-ordinates of that place and paint Rectangle at

that place by requesting, using repaint() call

ยƒ Here instead of Hard-coding the position of co-ordinates uses some variables. For

example mx , my

โ€“ In the last example code, we draw a rectangle by passing hard-coded values

like 20

g.drawRect(20,20,20,20);

โ€“ Now, weโ€™ll use variables so that change in a variable value causes to display a

rectangle at a new location

g.drawRect( mx , my ,20,20;

ยƒ Similarly, you have seen a tennis game (during lecture). Now, what to do code the

paddle movement.

ยƒ In the coming up example. We are doing it using mouse, try it using mouse.

Example Code 19.

The following outputs were produced when mouse is dragged from one location to anther

First we examine the MyPanel.java class that is drawing a filled rectangle.

import javax.swing.; import java.awt.;

// extending class from JPanel public class MyPanel extends JPanel {

// variables used to draw rectangles at different //locations int mX = 20; int mY = 20;

// overriding paintComponent method public void paintComponent(Graphics g){

// erasing behaviour โ€“ this will clear all the // previous painting super.paintComponent(g);

// Down casting Graphics object to Graphics2D Graphics2D g2 = (Graphics2D)g;

// changing the color to blue g2.setColor(Color.blue);

// drawing filled oval with color i.e. blue // using instance variables g2.fillRect( mX , mY ,20,20);

} // end paintComponent

} // end MyPanel class

// call to repaint causes rectangle to be drawn on // new location p.repaint() ;

} // end mouseDragged

} // end Handler class

// main method public static void main(String args[ ]){ Test t = new Test();

}

} // end MyPanel class

On executing this program, when you drag mouse from one location to another, rectangle

is also in sink with the movement of mouse. Notice that previously drawn rectangle is

erased first.

If we exclude or comment out the following line from MyPanel class

super.paintComponent(g);

Dragging a mouse will produce a similar kind of output shown next

Example Code 19.2: Ball Animation

The ball is continuously moving freely inside the corner of the frames. The sample

outputs are shown below:

First we examine the MyPanel.java class that is drawing a filled oval.

import javax.swing.; import java.awt.;

// extending class from JPanel public class MyPanel extends JPanel {

// variables used to draw oval at different locations int mX = 200; int mY = 0;

// overriding paintComponent method public void paintComponent(Graphics g){

// erasing behaviour โ€“ this will clear all the // previous painting super.paintComponent(g);

// Down casting Graphics object to Graphics2D Graphics2D g2 = (Graphics2D)g;

// changing the color to blue g2.setColor(Color.blue);

// drawing filled oval with blue color // using instance variables g2.fillOval( mX,mY ,20,20);

}// end paintComponent

} // end MyPanel class

// if ball reached to maximum height of frame // minus 40 then change the Y-direction of ball if (f.getHeight()-40 == p.mY)

y = -3;

// if ball reached to min. of width of frame, // change the X-direction of ball if (p.mX == 0 )

x = 5;

// if ball reached to min. of height of frame, // change the Y-direction of ball

if (p.mY == 0 )

y = 3;

// Assign x,y direction to MyPanelโ€™s mX & mY p.mX += x; p.mY += y;

// call to repaint() method so that ball is drawn on // new locations p.repaint();

} // end actionPerformed() method

// main method public static void main(String args[ ]){ AnimTest at = new AnimTest(); }

} // end AnimTest class