2

My problem is this:

(Game: display a tic-tac-toe board) Display a frame that contains nine labels. A label may display an image icon for X or and image icon for O. What to display is randomly decided. Use the Math.random() method to generate an integer 0 or 1, which corresponds to displaying an X or O image icon. These images are in the files x.gif and o.gif

The problem I'm having is the fact that the imgaes wont show in the program. When i run it the program generates an empty frame. My guess is that there must be a problem with the location of the image files.

I have downloaded a .zip from the publisher of the book im using (Introduction to Java programming by Liang) which it seems the images should contain. But it didnt. So i found the identical .gif's online and downloaded them. Now im wondering how i set the right location.

I think the code itself should be okay. Here it is anyways.

import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class TicTacToe extends JFrame {
private ImageIcon cross = new ImageIcon("image/x.gif");
private ImageIcon not = new ImageIcon("image/o.gif");

public TicTacToe() {
    setLayout(new GridLayout(3, 3));

    for (int i = 0; i < 9; i++) {
        int mode = (int) (Math.random() * 3.0);
        if (mode == 0)
            add(new JLabel(this.cross));
        else if (mode == 1)
            add(new JLabel(this.not));
        else
            add(new JLabel());
    }
}

public static void main(String[] args) {
    TicTacToe frame = new TicTacToe();
    frame.setTitle("TicTacToe");
    frame.setSize(200, 200);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(3);
    frame.setVisible(true);
  }
}

Another small question, would using this:

import java.awt.*;
import javax.swing.*;

instead of this:

import java.awt.GridLayout;
import javax.swing.ImageIcon;

Be a bad thing? I'm thinking if you think my lecturer would find it lazy to always import all instead of the separate I need.

In advance - thanks.

Khilmarsen
  • 107
  • 4
  • 5
  • 10

2 Answers2

1

These statements will compile and execute

private ImageIcon cross = new ImageIcon("image/x.gif");
private ImageIcon not = new ImageIcon("image/o.gif");

if you have an image subfolder off the main project folder and the image subfolder is on the build path. These ImageIcons should be built in their own method, so you can handle the generated error(s) if the gifs are missing.

I read image files like this.

    try {
        img = ImageIO.read(new File("graphics/close_0.jpg"));
        remoteController = ImageIO.read(new File("graphics/pilot.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }

That way, your code doesn't have to wait for the images to load.

If you use an integrated development environment (IDE) like Eclipse, Eclipse will generate the following statements from the code.

import java.awt.GridLayout;
import javax.swing.ImageIcon;  

If you're not using an IDE, the following statements are OK. You don't want to spend a lot of time manually typing import statements.

import java.awt.*;
import javax.swing.*;
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Okay, so i managed to get the program running with the .gif's the way i wanted with the help of you guys so thanks for that. I am using Eclipse, but what i do is that i start with every program with importing the libraries im going to use. Would _you_ say that its more efficient to write the program and when its done, let Eclipse suggest what you need to import? – Khilmarsen Nov 10 '13 at 15:56
  • 1
    You'll have compile errors unless you let Eclipse generate the import statements as you add external classes. You can press Cntl-Shift-O as many times as you need, or right click and select Source -> Organize Imports. – Gilbert Le Blanc Nov 11 '13 at 00:42
0

Your icon files must be in a folder names 'images', and that folder must be in the default directory from which you run your program.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • Does this mean that i have to make a new folder inside the source folder for that specific java project? or can i put it the workspacefolder/"book" folder? – Khilmarsen Nov 10 '13 at 15:10
  • 1
    Where you put the images for development is another issue, though it is convenient to be able to run them with an IDE such as eclipse or IntelliJ. I was assuming you are running this as a jar file with a command such as "java -jar TicTacToe.jar"; in that case, you are in a particular 'default' folder when you do that. Images must be within that folder for your code to pick up the GIF files the way you have it written. There will be an equivalent in your IDE, possibly an images folder off the 'project' or 'project folder' or however yours works. – arcy Nov 10 '13 at 15:14
  • Thank you for the help, I am in fact using Eclipse, sorry for not informing you in the question. What i did was: I copy/pasted the .gif's inside the project folder, works like a charm. – Khilmarsen Nov 10 '13 at 15:50