0

I'm just trying to load in 9 identical .gif's of doges. That's all I wanna do. Please help!

It's for a project of a modified version of tic-tac-toe, and for some reason I can't get the images to appear in my window. Help!

package jerryTacToe;
import javax.swing.*;

import java.awt.*; 
import java.awt.event.*;

import java.io.IOException;

class picture extends JPanel
{
  /**
 * 
 */
private static final long serialVersionUID = 1L;
ImageIcon doge;

  public picture() throws IOException
  {
    doge = new ImageIcon("dogeRotates.gif", null);
    JPanel panel = new JPanel();
    JLabel label = new JLabel("", doge, JLabel.CENTER);
    panel.add(label, BorderLayout.CENTER);
}



public void paintComponent(Graphics g) 
{
    ((Icon) g).paintIcon(this, g, 10, 10);
/*        Graphics2D g2 = (Graphics2D) g;
    Line2D line = new Line2D.Float(100, 100, 250, 260);
    g2.draw(line);*/
}

public void changeDoge(boolean human) throws IOException
{
    if(human)
        doge = new ImageIcon("./dogeIntensifies.gif", null);
    else
        doge = new ImageIcon("./ferociousDoge.gif", null);
}
}

class Closer extends WindowAdapter
{
  public void windowClosing(WindowEvent e)
  {
System.out.println("much derp");
System.exit(0);
  }
}

public class Gui extends JFrame
  implements ActionListener, MouseListener
 {
/**
 * 
 */
private static final long serialVersionUID = 1L;

boolean human;

  JButton goSecond;
  picture centerCenter; picture topLeft; picture topCenter; 
  picture topRight; picture centerLeft; picture centerRight; 
  picture bottomLeft; picture bottomCenter; picture bottomRight;

  public void mouseClicked(MouseEvent e){
  if(e.getSource()==topLeft || e.getSource()==topCenter      
  ||e.getSource()==topRight ||
            e.getSource()==centerLeft ||e.getSource()==centerCenter   
  ||e.getSource()==centerRight || 
            e.getSource()==bottomLeft ||e.getSource()==bottomCenter 
   ||e.getSource()==bottomRight)
  {
      /*
       * Call the AI here. 
       * 
       * 
       * hello?
       * yes, this is doge. 
       * */
  }
  }
 public void mouseEntered(MouseEvent e){}
  public void mouseExited(MouseEvent e){}
   public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
     public void actionPerformed(ActionEvent e)
   {

  }

   public Gui() throws IOException
   {
addWindowListener( new Closer() );
setTitle("DOGE-TAC-TOE (very0.0.4)");
setVisible(true);
setSize(1600,900);

goSecond=new JButton("wow second");
topLeft=new picture();
topCenter=new picture();
topRight=new picture();
centerLeft=new picture();
centerCenter=new picture();
centerRight=new picture();
bottomLeft=new picture();
bottomCenter=new picture();
bottomRight=new picture();



centerCenter.addMouseListener(this);
topLeft.addMouseListener(this);
topCenter.addMouseListener(this);
topRight.addMouseListener(this);
centerLeft.addMouseListener(this);
centerRight.addMouseListener(this);
bottomRight.addMouseListener(this);
bottomCenter.addMouseListener(this);
bottomLeft.addMouseListener(this);
goSecond.addActionListener(this);

Container stuff=getContentPane();

stuff.setLayout( new BoxLayout(stuff, BoxLayout.PAGE_AXIS) );

JPanel panelTop=new JPanel();
panelTop.setLayout(new GridLayout(1, 3, 300, 100));
panelTop.add(topLeft);
panelTop.add(topCenter);
panelTop.add(topRight);

JPanel panelCenter=new JPanel();
panelCenter.setLayout(new GridLayout(1, 3, 150, 100));
panelCenter.add(centerLeft);
panelCenter.add(centerCenter);
panelCenter.add(centerRight);

JPanel panelBottom=new JPanel();
panelBottom.setLayout(new GridLayout(1, 3, 300, 100));
panelBottom.add(bottomLeft);
panelBottom.add(bottomCenter);
panelBottom.add(bottomRight);

stuff.add(goSecond);
stuff.add(panelTop);
stuff.add(panelCenter);
stuff.add(panelBottom);
  }

  public static void main(String [] args)throws IOException
  {
    JFrame it=new Gui();
   }

}
Captain
  • 1
  • 2
  • Please, in the future, post clean code. I'd help out, but I spent 5 minutes just trying to understand where everything is, and I got lost. I can't tell what vars are local and what are field; I cant tell when you create a new class, and the spacing is completely random – Vince Apr 30 '14 at 19:09
  • Where is `dogeRotates.gif` stored? Is it within the projects `src` or does it reside with the execution context of the application on the file system? Also, why not just use a `JLabel`... – MadProgrammer May 01 '14 at 00:51

1 Answers1

1

You are accessing images in different ways as shown below:

doge = new ImageIcon("dogeRotates.gif", null);

doge = new ImageIcon("./dogeIntensifies.gif", null);

Please check the image placement again.

If you are facing problem in accessing the images then have a look at my post

How to retrieve image from project folder?


Why don't you use simply GridLayout in this game of tic-tac-toe?

Find sample codes below

enter image description here

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76