Showing posts with label Animation. Show all posts
Showing posts with label Animation. Show all posts

Tuesday, 1 September 2015

Designing the Animations- Java 7(Part 2)-The Code

Here is the code snippet for the Ball_Animate.java:
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class Animate_Ball {

    public static JFrame fr;
    public static JLabel ball1;
    public static Timer balltimer;
    public static Container c;
    public static ImageIcon i;
   
   
    public Animate_Ball() {
       
        fr=new JFrame();
        ball1=new JLabel();
        c=fr.getContentPane();
        i=new ImageIcon("ball.png");
       
        c.setLayout(null);
       
        fr.setBounds(0, 0, 800, 500);
       
        ball1.setBounds(0, 9, 179, 168);
       
        ball1.setIcon(i);
        c.setBackground(Color.black);
        c.add(ball1);
        fr.setVisible(true);
       
balltimer=new Timer(6, new ActionListener() {            //ball animator
           
            @Override
            public void actionPerformed(ActionEvent arg0) {
       
                if(ball1.getX()<fr.getWidth()){
                    ball1.setLocation(ball1.getX()+5,ball1.getY());
                }
                else{
                    ball1.setLocation(0,9);
                }
                   
            }
        });

    }
   
    public static void main(String arr[]){
        Animate_Ball n=new Animate_Ball();
        n.balltimer.start();
    }

}

And the output will be moving ball. You can experiment it with the moving ball vertically by changing the ball1.getY()+5.


Happy designing with java.

Monday, 31 August 2015

Designing the Animations- Java 7(Part 2)

Welcome back guys, I will now make you entered into the coding now. We will move the ball now on the Frame.

So, first of all open the eclipse Luna and create new project in it with any name you prefer to give.
I will assuming that you know all these basic stuff of opening and creating project in Eclipse and creating a new class. I will now directly skipping to code:

Step 1.
The stuffs you will need in the code::
    public static JFrame fr;
    public static JLabel ball1;
    public static Timer balltimer;
    public static Container c;
    public static ImageIcon i;


Frame is necessary to residing or a base for your ball and container will contains all your components, in our case it will contain our ball. Timer is used for continuously moving ball, our actual code logic resides here.

So let's start the further initialization.

Step 3.
public Animate_Ball() {
       
        fr=new JFrame();
        ball1=new JLabel();
        c=fr.getContentPane();
        i=new ImageIcon("ball.png");

        .
        .
 This is the constructor in which we will initialize all stuffs. Frame is initialized and ball object is initialized here and note that I have used Label for that purpose. Container is initialized with the Frame's contents. Now the primary thing is the ImageIcon which includes path of your image file or you can put it in your eclipse project files where your code resides or outside src folder.

Image stored here


This is the way you can create a simple animation through small code. Now next is the main code:

Step 3.
        c.setLayout(null);
        fr.setBounds(0, 0, 800, 500);
        ball1.setBounds(0, 9, 179, 168);
        ball1.setIcon(i);
        c.setBackground(Color.black);
        c.add(ball1);
        fr.setVisible(true);

I have just set layout as null because I want to move my label freely anywhere on the frame. Then set bounds according to the image size, my image size is 179*168. Then set the icon to the label that you have initialized earlier in Step 2. Then I want my background black hence I have changed the background of container but not the frame because frame contains the container and container contains components.
Lastly add the components to container and set visible the frame.

Step 4.
balltimer=new Timer(6, new ActionListener() {            //ball animator
           
            @Override
            public void actionPerformed(ActionEvent arg0) {
       
                if(ball1.getX()<fr.getWidth()){
                    ball1.setLocation(ball1.getX()+5,ball1.getY());
                }
                else{
                    ball1.setLocation(0,9);
                }
                   
            }
        });


Now you have to give your logic here that will execute continuously after the 6 sec interval as I mentioned, you can add more time. Initialize the timer as shown above and make use of CTRL+ SPACEBAR while typing to ease your typing. Then you need to call your timer by using balltimer.start();. Either call it from the constructor but below the initialization of the timer otherwise you will get exceptions or you can call it from main().

If you understood the concept then just create complete code from it then and only then you will get it clearly. Otherwise I will upload the code anyway in next tutorial.

Happy Designing with Java.

Sunday, 30 August 2015

Designing the Animations- Java 7(Part 1)


I am back again with the thing that will make you feel joyful and exciting. I was already gave you a basic knowledge about the designing in Photoshop. Today I will demonstrate you all about the animation in java with the help of obviously Adobe Photoshop.

Here are the things you will need while going through out the tutorial:
  1. Adobe Photoshop
  1. Java 7
  1. Eclipse Luna

Step 1.
First thing you need to do is that just create a ball in Adobe Photoshop. Don't worry,  I will help you out with this as usual.

Just select the shape tool but it doesn't appear as you move your mouse upon it. It's depends upon your previous selection in project. It may be named Line tool, Polygon tool, etc. You need to select Ellipse tool.  I already explained about Toolbox tools and selection. 

 
Initial Stage

Now draw it on the workspace like I have done. Get a reference of the image given above.

You will have a kind of scenario as shown above. It may differ as you are doing it in your own way. I am assuming that you have done with it and colored your ball. But still there's something you that don't make you feel it as a ball so here is the trick for giving it an illusion of 3d ball.

Step 2.
Remember guys everything you see in the real life is completely a part of illusion but that doesn't mean that everything you see in real world is falsified. It's just contains some illusions. So here is our illusion in virtual world, first of all make sure you have colored ball with something dark as I have done. 

Create ball


As I have selected blue color because it seems really shining color. Now the next thing you need to do is that select brush tool. But it will not allow you to color it directly so first Rasterize by right clicking on the Layer(Shape 1 if you didn't named it) and select Rasterize option. Now you have the rights to color it through any color.

Step 3.

Just now select Brush tool to make the ball shine. Change the foreground color to white and just click once on the area where you want the shine on ball like I have done. Note: Select entire ball by CTRL+ Left Click on the Shape 1 layer on layer Bar, it will select entire ball at once and then put the brush tool containing white and size may vary based on your scenario.
Final touch on ball
 Here is the sample ball I have created in png format:
In the Next tutorial I will describe the further steps.

Happy Designing.