views:

45

answers:

2

I am trying to add an image to a JPanel class, which is a chess board. Then I want to be able to "cut" it to define each space as a part of an array list and then be able to place pieces on top of each according to the part of the board it is supposed to start in. Here is part of what I have:

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.io.*;
import java.awt.image.*;
import java.applet.*;
import javax.imageio.*;

public class JVChess extends JFrame implements Runnable {

  BufferedImage img;
 public JVChess()
 {
  super("JVChess");
  //../../../../../Pictures/My\ Projects/JVChess/Default\ Board.png
  Applet app = new Applet();
  Container contentPane = getContentPane();
  //contentPane.add(BG);
  try
   {
     img = ImageIO.read(new File("JVChess/DefaultBoard.png"));
   }
   catch(IOException ioe){}
  //BufferedImage img = getImage(app.getCodebase(), "C:/Users/Veronica// Sigler/Pictures/My// Projects/JVChess/Default// Board.png");
  //try{
  //  img = ImageIO.read(new File("C:/Users/Veronica// Sigler/Pictures/My// Projects/JVChess/Default// Board.png"));
  //}
  {
  }
 }
 public void run() {
  // TODO Make window to work and import the JPanel include Menu (New Game, Player #, Quit, ...)
  setSize(1000,1000);
  getContentPane().add(new BG(img));
  JMenuBar mbar = new JMenuBar();
  setJMenuBar(mbar);
  JMenu file = new JMenu("File");
  mbar.add(file);
  JMenuItem newGame = new JMenuItem("New Game");
  file.add(newGame);
  newGame.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e)
   {

   }
  });
  JMenuItem quit = new JMenuItem("Quit");
  file.add(quit);
  quit.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e)
   {
    System.exit(0);
   }
  });
  JMenu theme = new JMenu("Theme");
  mbar.add(theme);
  JMenuItem defaultTheme = new JMenuItem("Default");
  theme.add(defaultTheme);
  defaultTheme.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e)
   {

   }
  });
  JMenuItem wood = new JMenuItem("Wood");
  theme.add(wood);
  wood.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e)
   {

   }
  });
  JMenuItem dungeon = new JMenuItem("Dungeon");
  theme.add(dungeon);
  dungeon.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e)
   {

   }
  });
  JMenuItem piggie = new JMenuItem("Piggie");
  theme.add(piggie);
  piggie.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e)
   {

   }
  });


  setVisible(true);
 }
 /*****Main Method*****/
 public static void main(String[] args) {
  // TODO 
  JVChess jvc = new JVChess();
  javax.swing.SwingUtilities.invokeLater(jvc);
  jvc.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e)
    {System.exit(0);
    }
  });
  jvc.add(new LoadImageApp());
  jvc.pack();
  jvc.setVisible(true);
 }


}

Another class

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class ChessPanel extends JPanel implements MouseListener, MouseMotionListener{
  BufferedImage img;
 public ChessPanel(){
  super();
 }

 public void mouseEntered(MouseEvent e){}
 public void mouseExited(MouseEvent e){}
 public void mouseClicked(MouseEvent e){}
 public void mousePressed(MouseEvent e){}
 public void mouseReleased(MouseEvent e){}
 public void mouseDragged(MouseEvent e){}
 public void mouseMoved(MouseEvent e){}

}

and a just Test class

import javax.swing.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.imageio.*;

class BG extends JPanel
{
  BufferedImage img;

  BG(BufferedImage img)
  {
    this.img = img;
    try
   {
     img = ImageIO.read(new File("JVChess/DefaultBoard.png"));
   }
   catch(IOException ioe){}
   setVisible(true);
  }

  protected void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    int x = 1000;
    int y = 1000;
    g.drawImage(img, x, y, this);
  }
}
A: 

It sounds like this might be what you're looking for.

javamonkey79
This is helpful, but I feel that I might need to also know how to lay an image on top of that.
Salazar
A: 

Have you considered breaking your JPanel into an 8x8 grid of JLabels, then using JLabel.setIcon() to set the piece in each space?

Andy Thomas-Cramer
Nope I haven't. I know this is some what an ambitious project for a high school student. I am not sure how to add the pieces on the chessboard on top. How would the overlay of an image work?
Salazar
The JLabel class provides a method that sets the image presented by the control. The method accepts an object that implements the Icon interface -- such as ImageIcon, which accepts an Image or the name of a file containing an image. You could arrange 64 such JLabels in your JPanel in an absolute or GridLayout. This is simpler than splicing together your own images, or trying to overlay an image onto your JPanel.
Andy Thomas-Cramer