Nebula
Nebula

Reputation: 47

How to set an ImageIcon to a JLabel Array?

I have created a program to add an ImageIcon to the next empty JLabel, the compiler shows no errors so why isn't this working? The setEmptySpace contains a loop that returns an empty space and the getCar method is supposed to set the icon.

package CarPark;
//add imports
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Park extends JPanel
{

    //declare a JLabel array for parking space
    JLabel[] parkingSpace;
    //declare ImageIcon class for cars
    ImageIcon car;

    public Park()
    {
        //create a new instance of the parking space array with 12 spaces
        parkingSpace = new JLabel[12];
        //populate array
        for (int i = 0; i < parkingSpace.length; i++)
        {
            //create parking space JLabels
            //parkingSpace[i] = new JLabel(String.valueOf(i + 1));
            parkingSpace[i] = new JLabel();
            //set parking space colour
            parkingSpace[i].setBackground(new Color(85, 55, 170));
            //make JLabels opaque
            parkingSpace[i].setOpaque(true);

        }

        //create grid for parking space setting rows, columns, horizontal gap and vertical gap
        this.setLayout(new GridLayout(4, 3, 4, 4));
        //set background colour to white
        this.setBackground(new Color(255, 255, 255));

        //add 12 parking spaces to panel
        for (int i = 0; i < 12; i++)
        {
            this.add(parkingSpace[i]);
            //create a red border with a thickness of 4 pixels
            parkingSpace[i].setBorder(BorderFactory.createLineBorder(Color.RED, 4));
        }
    }
    //find an empty parking space
    int setEmptySpace()
    {
        for(int i = 0; i < parkingSpace.length; i++)
        {
            if(parkingSpace[i] == null)
            {
                return i;
            }
        }
        return -1;
    }
    //add car image to empty parking space
    void getCar(int getEmptySpace)
    {
         car = new ImageIcon("car.png");

         parkingSpace[getEmptySpace].setIcon(car);
    }
}

Upvotes: 1

Views: 437

Answers (2)

Nebula
Nebula

Reputation: 47

I've tried simplifying my code into more methods that only do one thing each instead of trying to do multiple tasks per method. What I want to know is does the setCarIconToLabel() actually set the icon to a JLabel and if it doesn't how could it be altered. Also is this method the correct type since I don't think it should be void but I don't know what else to set it to?

//find an empty parking space and return it's number
int emptySpaceNo()
{

    for(int i = 0; i < parkingSpace.length; i++)
    {
        System.out.println("Test method 1 Park P 1");
        if(parkingSpace[i].getIcon() == null)
        {
            System.out.println("Test method 1 P 2");
            return i;

        }
    }
    return -1;
}

//return JLabel that is null
JLabel findEmptySpace()
{
    return parkingSpace[emptySpaceNo()];
}

//create new car image icon
ImageIcon setCarIcon()
{
    ImageIcon carIcon = new ImageIcon("car.png");

    return carIcon;
}

//set car icon to JLabel parking space
void setCarIconToLabel()
{
  findEmptySpace().setIcon(setCarIcon());
}

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347314

Based on this from your constructor...

for (int i = 0; i < parkingSpace.length; i++) {
    //create parking space JLabels
    //parkingSpace[i] = new JLabel(String.valueOf(i + 1));
    parkingSpace[i] = new JLabel();
    //set parking space colour
    parkingSpace[i].setBackground(new Color(85, 55, 170));
    //make JLabels opaque
    parkingSpace[i].setOpaque(true);
}

It's impossible for this...

int setEmptySpace() {
    for (int i = 0; i < parkingSpace.length; i++) {
        if (parkingSpace[i] == null) {
            return i;
        }
    }
    return -1;
}

to return anything other than -1, because every parkingSpace contains a JLabel

What I think you want to do, is check to see if the JLabel's icon property is null, for example

int setEmptySpace() {
    for (int i = 0; i < parkingSpace.length; i++) {
        if (parkingSpace[i].getIcon() == null) {
            return i;
        }
    }
    return -1;
}

Upvotes: 1

Related Questions