JAVA Animation with PAUSE,RESUME and EXIT functionality

TUTORIAL NO 5

Animation with PAUSE, RESUME and EXIT functionality

In this tutorial you will learn: 1.Swing Animations 2.Event handling 3.JAVA awt 4.JAVA swing 5.Adapters Whenever I am coding a game I faced a problem in making the pause, resume and exit functionality. So today I am going to teach you how to make a simple animation with pause, resume and exit functionality. Which will later help you when you are coding a game. In this tutorial we will be working in JAVA SWING. The first thing that we are going to do is setting up a JPanel and adding the required Components after that adding that JPanel in the JFrame. Basic step: Download and install ECLIPSE and set up a JAVA PROJECT. Then create a new class and name it Animation. Then follow the steps 1.IMPORT STATEMENTS First of all write these import statements in your .java file
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
We require event import for mouse click, awt and swing imports for visual design and animation. We will be using separate classes for JPanel and a simple class in which we will make a JFrame and add the mouse listeners. First thing is setting up the panel and after that we will setup a class in which we will setup the frame. Then we will do composition i.e make and object of the panel class and add it to frame. 2. SETTING UP THE PANEL CLASS
  1. class AnimationPanel extends JPanel {
  2.  
  3. int x = 0, y = 200, flag = 1; // screen coordinates
  4. static boolean pause = false; // pause is false so that animation starts first time
  5. Image img = new ImageIcon("image//logo.png").getImage(); // picking the image icon from folder
We require a variable for x,y for the coordinates and flag for moving the animation vertically or horizontally. Then a Boolean to check whether the animation is paused or not. After that we need a variable to get our image from the image folder.
  1. public void paintComponent(Graphics g) {
  2. super.paintComponent(g); //calling paint function of super class
  3. Graphics2D g2d = (Graphics2D) g; //typecasting to graphics2D for javax.swing animation
  4. moveLogo(g2d); //calling the move function
  5. try {
  6. Thread.sleep(5); //sleeping the thread for 5ms
  7. } catch (Exception e) {} //catching the expeption if any
  8.  
  9. g2d.drawImage(img, x, y, null);// for drawing the image
  10. repaint(10); // repainting the entire screen for movement
  11.  
  12. }// paintComponent ends
In the JPanel class we write the paintComponent function which is used to draw the animation. First of all we need to call the base class paintComponent in the function that we are overriding, after that type cast the graphics object to Graphics2D because we want the animation to be of java.swing. Then we call our moveLogo function by passing the g2d reference to it which will move our animation by changing certain coordinates. After that we call the Thread.sleep function to slow down the speed of our animation and then draw the image by passing the image object, x, y coordinates and the fourth argument as null which is the image observer. In the end we called the repaint function with a slight delay to redraw the animation at the new position.
  1. void moveLogo(Graphics g2d) {
  2. if (pause == false) { //when pause is false animation can play
  3.  
  4. if (x < 1000 & y == 200 & flag == 1) { //flag is 1 then inc x
  5. x++;
  6. if (x == 1000) {
  7. x = 400; //when x reaches max width set x to 400
  8. y = 10; // y to 10
  9. flag = 2; //for vertical movement
  10. }
  11. }
  12. if (x == 400 & y < 550 & flag == 2) {
  13. y++; //now inc y for horizontal
  14. if (y == 550) { //when y reaches max width
  15. x = 0;
  16. y = 200;
  17. flag = 1; //set flag to 1 for horizontal movement aain
  18. }
  19. }
  20. }// end if
  21. }// end movelogo
  22. }// class AnimationPanel ends
Now we will write the move logo function. We will only move forward in this function if pause the Boolean variable pause is equal to false. After that we write two if statement one for the horizontal movement and the other one for the vertical movement. When our x coordinate is less than the max width of the frame i.e 1000 and y coordinate is 200 and flag = 1 then we increment x coordinate to move the logo horizontally. But when the logo reaches 1000 x coordinate then we change the values of x and y coordinates as well as the flag to the half of the frame. So that we can start the logo movement from the top middle to bottom middle vertically. After reaching the bottom we changed are values back for horizontal movement again. 3. WRITING THE ANIMATION CLASS We will do everything in a constructor so that the jframe gets setup whenever we create the object of the Animation class. Now writing the constructor
  1. public class Animation extends MouseAdapter {
  2.  
  3. static JButton pause = new JButton("Pause");
  4. static JButton resume = new JButton("Resume");
  5. static JButton exit = new JButton("Exit");
  6. static JFrame frame = new JFrame("ANIMATION APPLICATION");//frame with title
  7.  
  8. AnimationPanel ap = new AnimationPanel();
  9.  
  10. Animation() { // setting the frame listeners and panel in constructor
  11.  
  12. frame.add(ap); //add animation panel in frame
  13. ap.add(pause); //add pause button
  14. ap.add(resume);//add resume button
  15. ap.add(exit); //add exit button
  16.  
  17. frame.setSize(1000, 600);
  18. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. ap.setBackground(Color.WHITE);
  20. frame.setVisible(true);
  21.  
  22. pause.addMouseListener(this);// mouselistener for pause
  23. resume.addMouseListener(this);// mouselistener for resume
  24. exit.addMouseListener(this);// mouselistener for exit
  25. }
In this animation class what we need to do is to make a JFrame as well as three buttons for pause resume and exit after that we need to add mouselisteners to them. SO inorder to do that we created the 3 JButton objects, the frame object and then the panel object to add in the JFrame. After that we write the animation class constructor. First of all we added the panel in the frame and then we added all the 3 buttons in that panel. After that we set the frame size, background color and visibility. Then we add the mouse listeners for the three buttons.
  1. @Override
  2. public void mouseClicked(MouseEvent ae) {
  3. if (ae.getSource() == pause) // checking which button is pressed
  4. {
  5. AnimationPanel.pause = true; //set pause to true and it will pause the animation
  6. }
  7. if (ae.getSource() == resume) {
  8. AnimationPanel.pause = false; //set pause to false and it will resume the animation
  9. }
  10. if (ae.getSource() == exit) {
  11. System.exit(0); // exit the app
  12. }
  13. }//end mouseclicked
Now we override the mouseClicked function of the mouseAdapter class to provide the functionality to the three buttons. First of all we check for the button name in the if statement. If the button pressed is pause then we change the value of our static pause variable of the animationPanel to true otherwise we change it to false on pressing resume to continue the animation. In the end we check for the exit button and call the function system.exit if it is pressed which will close our application instantly. 4. DEFINING THE MAIN FUNCTION
  1. public static void main(String[] args){
  2. Animation a = new Animation();
  3. }//end main
  4. }// end class Animation
In the main function we only created the object of the JFrame class and our application is complete. NOTE: Code file includes the image file and source code OUPUT: output

Add new comment