I am making a game and have a class to print out everything on the screen. I get an error on this line: g.drawImage(chImage, imageP.x*scale, imageP.y*scale, null);
is it because i have null at the end of it? I have looked up this error and tried to figure it and i understand that something is null in that line but i can not figure out what is null or how to fix that. If you need me to post the other classes i can. From my google searches so far i think that it might have something to do with the way that my variables are initialized.
package Game;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.JPanel;
public class Printer extends JPanel{
private Map map;
private Character ch;
private int scale;
private Point imageP;
private BufferedImage chImage;
private ArrayList<Rectangle> part;
public Printer(int scale1){
scale = scale1;
map = new Map();
ch = new Character();
imageP = ch.getPoint();
chImage = ch.getImage();
part = map.setPart();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(chImage, imageP.x*scale, imageP.y*scale, null);
for(int i=0; i<part.size(); i++){
Rectangle temp = new Rectangle(part.get(i));
g.drawRect(temp.x, temp.y, temp.width, temp.height);
g.fillRect(temp.x, temp.y, temp.width, temp.height);
}
}
}
package Game;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
//Creates a image for a character and allows him to walk around the screen
public class Character extends JPanel{
private BufferedImage image;
private Point imageP;
private int speed;
private int scale;
private int width;
private int height;
public Character(){
}
public Character(int x, int y, int scale1, int w, int h){
super();
try {
image = ImageIO.read(new File("F:\\Programming\\Final Project\\Top_down\\narwhal.png"));
} catch (IOException ex) {
}
scale = scale1;
imageP = new Point(x,y);
speed = 10;
width = w;
height = h;
addKeyListener(new KeyAdapter(){
@Override
public void keyPressed(KeyEvent evt){
moveIt(evt);
}
});
}
public void moveIt(KeyEvent evt){
switch(evt.getKeyCode()){
case KeyEvent.VK_S:
if(imageP.y <= height-33)
imageP.y += 1*speed;
break;
case KeyEvent.VK_W:
if(imageP.y >=0+5)
imageP.y -= 1*speed;
break;
case KeyEvent.VK_A:
if(imageP.x >=0+5)
imageP.x -= 1*speed;
break;
case KeyEvent.VK_D:
if(imageP.x <= width-30)
imageP.x += 1*speed;
break;
}
repaint();
}
public Point getPoint(){
//g.drawImage(image, imageP.x*scale, imageP.y*scale, null); IGNORE THIS
return this.imageP;
}
public BufferedImage getImage(){
return this.image;
}
}