简体   繁体   中英

Drawing and animating a grid of images

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

This line is wrong with Swing:

frame.setLayout(new GridLayout(width,length))

As I remember, we should apply layouts to panels, ie

frame.getContentPane().setLayout (new GridLayout(width,length));

This line is wrong as well:

frame.add(rail[x][y]);

the solution is the same: use contentPane .

Some basics could be found at JFrame javadocs page .

Since you are trying to build a grid, then, I suggest you take a look at the GridLAyout . This will take care of your components, since it will split the given area into a grid.

With ragards to rotating the image, please take a look at the rotate method.

Answer to 1)

You get the NPE because you do not initialize the array named ´rail´ like you do with ´grid´:

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
-->     rail=new JLabel[width][length]; //allocate the size of rail
        for(int y=0; y<length; y++){ 

It is generally better to work in a JPanel, like suggested, but ´add(...)´ and ´setLayout(...)´ on JFrame are convenience methods that delegates to the content pane, so this will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM