views:

29

answers:

3

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

A: 

This line is wrong with Swing:

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

As I remember, we should apply layouts to panels, i.e.

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.

Roman
A: 

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.

P.S. Please work on your accept rate.

npinti
@npinti: he's already using GridLayout but in a wrong way.
Roman
Thanks for your reply. What do you mean by working on my accept rate? I mean what should i do concerning the accept rate.
Edy Moore
@Roman, yeah my bad... I misinterpreted myself, that is why I posted a link to the GridLayout tutorial.@Kap: When you ask a question, and people answer, you can chose what question was the correct answer. In doing so, who answered the question correctly will be awarded points. If you do not mark answers to your questions, users will be more reluctant to answer your questions.
npinti
OK thanks for that. i will do so from now. How do i rate the answers?
Edy Moore
On the top left, under the rating of the answer, you should be able to see the outline of a "very good" mark. Just click on that and you should be set.
npinti
A: 

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.

Tormod