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);
}
}