Painting to JPanel

Hi all,

Why does the following code only paint black to my JPanel. I am making a RPG and this is where i am up to, only the tile map.

//Engine for game

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

public class Engine extends JPanel{

FRW connecton;
Fight gui;
Tile[] tiles;
Image grass;
Image water;
BufferedImage screenImage;
Graphics screenGC;
int curX;
int curY;
int cutX;
int cutY;
boolean first = true;
int[][] mapdata = {

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0}};

public Engine( Fight gui ){
screenImage = null;
setSize(400,400);
System.out.println(“Starting Tracker”);
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(water, 0);
tracker.addImage(grass, 1);
try{
tracker.waitForAll();
}
catch(Exception trackerx){ System.out.println(“Tracker Failed”); }
System.out.println(“Tracker Done”);
this.gui = gui;
tiles = new Tile[2];
System.out.println(“Tiles GUI setup”);
curX = 0;
curY = 0;
cutY = 0;
cutX = 0;

System.out.println(“Creating Image”);
grass = Toolkit.getDefaultToolkit().createImage(“Grass.bmp”);
water = Toolkit.getDefaultToolkit().createImage(“Water.bmp”);

System.out.println(“Setting up KeyListener”);

this.addKeyListener( new KeyAdapter(){

public void keyPressed( KeyEvent e){

switch(e.getKeyCode()){ 
  
case KeyEvent.VK_UP: 
 scroll(1); 
 break; 

case KeyEvent.VK_DOWN: 
 scroll(3); 
 break; 
  
case KeyEvent.VK_RIGHT: 
 scroll(2); 
 break; 
  
case KeyEvent.VK_LEFT: 
 scroll(4); 
 break; 
} 

repaint();

}
}
);

tiles[0] = new Tile(grass, 40, 40);
tiles[1] = new Tile(water, 40, 40);
System.out.println(“About to printMap”);
System.out.println(“In paint setting up Graphics”);
screenImage = new BufferedImage(480, 480, BufferedImage.TYPE_INT_RGB);
System.out.println(“Double Buffering”);
System.out.println(“Setting up graphics”);
screenGC = screenImage.createGraphics();
first = false;
drawMap();

}

public void drawMap(){

for(int y = 0; y < 10; y++){
for(int x = 0; x < 10; x++){
tiles[mapdata[cutY + y][cutX + x]].paint(screenGC, curX, curY);
curX += 40;
}
curY += 40;
curX = 0;
}
System.out.println(“About to repaint map”);
repaint();
}
public void scroll(int dir){

}

public void paintComponent(Graphics g){

super.paintComponent(g);

System.out.println(“In Paint”);
g.drawImage(screenImage, 0, 0, this);
System.out.println(“Painted”);

}

public void distroy(){

screenGC.dispose();

}

}

//Tile Class

import java.awt.;
import java.awt.image.
;

public class Tile{

Image tileImage; //Hold tile image
int tileHieght; //Tile hieght
int tileWidth; //Tile width

public Tile(Image tile, int width, int hieght){

tileImage = tile;
tileHieght = hieght;
tileWidth = width;

}

public void paint(Graphics g, int x, int y){
System.out.println(“In Tile Paint”);
Graphics g2 = g.create(x, y, 40, 40);
g2.drawImage(tileImage, 0, 0,null);
g2.dispose();
System.out.println(“Painted Image in Tile”);

}
}

\JFrame class - main

import javax.swing.;
import java.awt.
;
import java.awt.event.;
import javax.swing.event.
;

public class Fight extends JFrame{

Engine bgmap;

public Fight(){

super(“MiniWars”);

System.out.println(“In Constructor”);
bgmap = new Engine(this);
System.out.println(“Created Engine”);
Container hold;
hold = getContentPane();
hold.add(bgmap);

setSize(400, 400);
show();

}

public static void main( String args[] ){

Fight app = new Fight();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e ){
System.exit(0);
}
}
);

}
}

Any help would really be great…

tip #1: use the [ code ] tags to format code posts
tip #2: I don’t see the setColor(Color c) method of a graphics object being invoked anywhere. I just did a quick scan, but that would probably be part of it.

The problem appears to be the way you are loading the grass and water images. I use png but I suppose bmp should work. At any rate I got it to draw something by changing the toolkit.creatImage calls to the following.


    URL url = Engine.class.getResource("Grass.png");
    ImageIcon xi = new ImageIcon(url);   
    grass = xi.getImage();

    url = Engine.class.getResource("Water.png");
    xi = new ImageIcon(url);   
    grass = xi.getImage();

BTW please don’t cross post in these forums.