我真的需要你們的幫助。我必須在3×3的圖像網格上進行動畫。繪圖和動畫的圖像網格
我的問題是:
1) 我如何構建3×3格與圖像?
這是我做的,但不是因爲工作,因爲我在這行得到NullPointerException異常: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) 如何使用此網格爲我的動畫背景?
3) 我必須旋轉網格[2] [2]中的圖像,我怎樣才能訪問這個圖像和旋轉它?我知道如何做旋轉,所以告訴我如何獲得元素[2] [2],以便我可以旋轉它。
感謝您的幫助
參見http://stackoverflow.com/questions/3420651 – trashgod 2010-08-13 16:43:41