need help with JPanel Images
I am working on creating a pacman game but i am having trouble putting an Image into JPanel....
any help is appreciated
Code:
import java.awt.*;
import java.awt.geom.Line2D;
import javax.swing.*;
public class gameBoard extends JPanel
{
SquareRx[][] squares;
final int PAD = 20;
public gameBoard()
{
int ROWS = 10;
int COLS = 10;
squares = new SquareRx[ROWS][COLS];
for(int i = 0; i < ROWS; i++) {
for(int j = 0; j < COLS; j++) {
squares[i][j] = new SquareRx(i, j);
}
}
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
double xInc = (double)(w - 2*PAD)/squares[0].length;
double yInc = (double)(h - 2*PAD)/squares.length;
g2.setPaint(Color.black);
for(int i = 0; i <= squares[0].length; i++)
{
double x = PAD + i*xInc;
//g2.draw(new Line2D.Double(x, PAD, x, h-PAD));
}
for(int i = 0; i <= squares.length; i++)
{
double y = PAD + i*yInc;
//g2.draw(new Line2D.Double(PAD, y, w-PAD, y));
//left column
g.fillRect(20,20 ,35,35);
g.fillRect(20,55 ,35,35);
g.fillRect(20,90 ,35,35);
g.fillRect(20,120 ,35,35);
g.fillRect(20,155 ,35,35);
g.fillRect(20,190 ,35,35);
g.fillRect(20,220 ,35,35);
g.fillRect(20,255 ,35,35);
g.fillRect(20,290 ,35,35);
g.fillRect(20,320 ,35,35);
g.fillRect(20,355 ,35,35);
g.fillRect(20,390 ,35,35);
g.fillRect(20,420 ,35,35);
g.fillRect(20,455 ,35,35);
g.fillRect(20,490 ,35,35);
//top row
g.fillRect(55,20 ,35,35);
g.fillRect(90,20 ,35,35);
g.fillRect(120,20 ,35,35);
g.fillRect(155,20 ,35,35);
g.fillRect(190,20 ,35,35);
g.fillRect(220,20 ,35,35);
g.fillRect(255,20 ,35,35);
g.fillRect(290,20 ,35,35);
g.fillRect(320,20 ,35,35);
g.fillRect(355,20 ,35,35);
g.fillRect(390,20 ,35,35);
g.fillRect(420,20 ,35,35);
g.fillRect(455,20 ,35,35);
g.fillRect(490,20 ,35,35);
//bottom row
g.fillRect(55,490 ,35,35);
g.fillRect(90,490 ,35,35);
g.fillRect(120,490 ,35,35);
g.fillRect(155,490 ,35,35);
g.fillRect(190,490 ,35,35);
g.fillRect(220,490 ,35,35);
g.fillRect(255,490 ,35,35);
g.fillRect(290,490 ,35,35);
g.fillRect(320,490 ,35,35);
g.fillRect(355,490 ,35,35);
g.fillRect(390,490 ,35,35);
g.fillRect(420,490 ,35,35);
g.fillRect(455,490 ,35,35);
g.fillRect(490,490 ,35,35);
//right column
g.fillRect(490,55 ,35,35);
g.fillRect(490,90 ,35,35);
g.fillRect(490,120 ,35,35);
g.fillRect(490,155 ,35,35);
g.fillRect(490,190 ,35,35);
g.fillRect(490,220 ,35,35);
g.fillRect(490,255 ,35,35);
g.fillRect(490,290 ,35,35);
g.fillRect(490,320 ,35,35);
g.fillRect(490,355 ,35,35);
g.fillRect(490,390 ,35,35);
g.fillRect(490,420 ,35,35);
g.fillRect(490,455 ,35,35);
g.fillRect(490,490 ,35,35);
//top right
g.fillRect(90,90 ,35,35);
g.fillRect(90,120 ,35,35);
g.fillRect(90,155 ,35,35);
g.fillRect(120,90 ,35,35);
g.fillRect(155,90 ,35,35);
g.fillRect(90, 420, 35, 35);
g.fillRect(90, 390, 35, 35);
g.fillRect(90, 355, 35, 35);
g.fillRect(120, 420, 35, 35);
g.fillRect(155, 420, 35, 35);
g.fillRect(420, 90, 35, 35);
g.fillRect(390, 90, 35, 35);
g.fillRect(355, 90, 35, 35);
g.fillRect(420, 120, 35, 35);
g.fillRect(420, 155, 35, 35);
g.fillRect(390, 420, 35, 35);
g.fillRect(420, 390, 35, 35);
g.fillRect(420, 355, 35, 35);
g.fillRect(420, 420, 35, 35);
g.fillRect(355, 420, 35, 35);
}
}
public static void main(String[] args)
{
gameBoard test = new gameBoard();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(test);
f.setSize(560,575);
f.setLocation(200,200);
f.setVisible(true);
}
}
class SquareRx
{
private final int row;
private final int col;
private boolean occupied = false;
public SquareRx(int r, int c)
{
row = r;
col = c;
}
public void setOccupied(boolean occupied)
{
this.occupied = occupied;
}
public boolean isOccupied()
{
return occupied;
}
}