Every time i run the panel is filled with a white line in the corner of it and the rest is black. I would like to add KeyListener to the program so when i push the "W" key it will animate the sprite as it seems to be going forward.
IF the sprite is not centered all the time than how would i code a collision detect to repaint the map when the sprite image block reaches the edge of the panel.
I know im asking more than one question yet im tring to figure out one problem.
How would i spawn a simple NPC at a specific location on the tiled map that has not yet been painted. Have this NPC to be intractable.
What I would like to do is have a sprite in the center of the screen. Have a tiled map in the background with collision detect depended on color squares. IF tile layer is colored red not allow to pass through IF tile layer is colored red allow to pass through If tile color yellow apply action for it is a treasure chest.
public class gamePanel extends JPanel {
private BufferedImage image;
public gamePanel() throws IOException{
this.setBackground(Color.red);
}
public static void loadMap(Graphics g, int frame){
try {
BufferedImage bigImg = ImageIO.read(new File("C:\\Users\\czar\\Documents\\NetBeansProjects\\Inter\\src\\inter\\menu.jpg"));
} catch (IOException ex) {
Logger.getLogger(gamePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void loadSprite(Graphics g, int move_int) throws IOException{
BufferedImage bigImg = ImageIO.read(new File("C:\\Users\\czar\\Documents\\NetBeansProjects\\Inter\\src\\inter\\sprite.jpg"));
// The above line throws an checked IOException which must be caught.
final int width = 25;
final int height = 40;
final int cols = 2;
BufferedImage[] left_move = new BufferedImage[3];
BufferedImage[] right_move = new BufferedImage[3];
BufferedImage[] up_move = new BufferedImage[3];
BufferedImage[] down_move = new BufferedImage[3];
for(int i = 0; i < 4; i++){
if(move_int == 0){
left_move[i] = bigImg.getSubimage(
0 * width, 0 * height, width, height );
}
if(move_int == 1){
right_move[i] = bigImg.getSubimage(
i * width, 1 * height, width, height );
}
if(move_int == 2){
up_move[i] = bigImg.getSubimage(
i * width, 2 * height, width, height );
}
if(move_int == 3){
down_move[i] = bigImg.getSubimage(
i * width, 3 * height, width, height );
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
try {
loadMap(g, 0);
loadSprite(g, 0);
} catch (IOException ex) {
// handle exception...
}
}
}