Hi Guys,
i really need you guys your help. I have to do animation on a 3 X 3 grid of images.
My questions are :
1) How do i construct the 3 X 3 grid with the images.?
This is what i did but is not working because because i get nullpointerException in this line : rail[x][y] = new JLabel(icon);
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ButtonGrid {
JFrame frame=new JFrame(); //creates frame
JButton[][] grid; //names the grid of buttons
JLabel[][] rail = null;
public ButtonGrid(int width, int length){ //constructor with 2 parameters
frame.setLayout(new GridLayout(width,length)); //set layout of frame
grid=new JButton[width][length]; //allocate the size of grid
for(int y=0; y<length; y++){
for(int x=0; x<width; x++){
//grid[x][y]=new JButton("("+x+","+y+")");
//frame.add(grid[x][y]); //adds button to grid
ImageIcon icon = createImageIcon("images/crossingsHorizontal.JPG", "");
//JLabel lab = new JLabel(icon);
rail[x][y] = new JLabel(icon);
frame. add(rail[x][y]);
}
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static ImageIcon createImageIcon(String path,String description) {
java.net.URL imgURL = ButtonGrid.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
return null;
}
}
public static void main(String[] args) {
new ButtonGrid(3,3);//makes new ButtonGrid with 2 parameters
}
}
2) How can i use this grid as a background for my animation?
3) I have to rotate the the image in grid [2][2], how can i access this image alone and rotate it? I know how to do the rotation so tell me how to get the element [2][2] so that i can rotate it.
thanks for your help