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.
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.
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.
No comments:
Post a Comment