简体   繁体   中英

JLabel Icon not showing after change

I tried looking up this question, but most of the answers are that the file path is wrong, but that's most likely not the case. The file works the 1st time I use it.

I am making a battleships game, and use JLabels to show ships on map. I want to make a button to rotate ship from horizontal to vertical but it's icon disappears when I try to change it.

When I run this constructor code:

public Ship(int size, String direction, boolean active, Client c,
        ClientGUI cg) {
    this.c = c;
    this.cg = cg;
    health = size;
    this.active = active;
    this.direction = direction;

    file = "img/" + Integer.toString(health) + direction + ".png";   // String
    try {
        System.out.println(file);
        tx = ImageIO.read(new File(file));    // BufferedImage
    } catch (IOException e) {

        e.printStackTrace();
    }
    texture = new JLabel(new ImageIcon(tx));
    if (direction.equals("v"))
        texture.setBounds(0, 0, 40, 40 * size);
    else
        texture.setBounds(0, 0, 40 * size, 40);
    texture.setVisible(true);

}

everything works and I can see the image.

But then I try to rotate it, using pretty much the same code:

void rotate() {
    if (direction.equals("h")) {
        direction = "v";
        file = "img/" + Integer.toString(health) + direction + ".png";
        try {
            System.out.println(file);
            tx = ImageIO.read(new File(file));
        } catch (IOException e) {

            e.printStackTrace();
        }
        texture.setBounds(0,0,40, 40 * size);
        texture.setIcon(new ImageIcon(tx));

    } else {
        direction = "h";
        file = "img/" + Integer.toString(health) + direction + ".png";
        try {
            System.out.println(file);
            tx = ImageIO.read(new File(file));
        } catch (IOException e) {

            e.printStackTrace();
        }
        texture.setIcon(new ImageIcon(tx));
        texture.setBounds(0,0,40 * size, 40);
    }

    cg.repaint();    // not sure if I need to do this
}

and it disappears...

I tried placing two ships, one is rotated, it's just missing the JLabel or the icon on JLabel. 在此处输入图片说明

If you update the JLabel texture by calling a method which changes it's state, it may or may not be updated immediately unless you call texture.repaint() or texture.paintAll(texture.getGraphics()) , or some similar method.

Also, I would look into using a LayoutManager for whatever upper level component you are using to hold your grid of JLabels. If you use a GridLayout of your game board and either:

  1. set the JLabel's preferred size with texture.setPreferredSize(Dimension) and call frame.pack() once when setting up your game; or

  2. set the JLabel's size with label.setSize(Dimension) once and don't pack your JFrame

You will only need to set the size of the JLabel once, not every time you set a new ImageIcon to the label. Because, Ideally your game shouldn't be doing any extra work that it doesn't have to so it performs faster.

I would also recommend maintaining every possible ImageIcon as static fields in your class, rather than accessing them from the file every time. That way, you read them once from a static initializing method, which then reach ship can directly access when changing the direction.

I want to make a button to rotate ship from horizontal to vertical

You can use the Rotated Icon class to do the rotation for you.

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