R
rob bell
Hello im having a problem drawing over existing JLables. is there anyway to
do it, its hard to explain what im trying to do, but basically my problem is
that i cant get my program to draw a certain tile in a certain place, or
atleast it does but it doesnt show up, maybe its drawing behind the old
tile. here is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class MyGame implements KeyListener{
JFrame gameFrame;
JPanel gamePanel;
int playerX, playerY, newPlayerX, newPlayerY;
int[][] mapArray = { {1,1,1,1,1,1,1,1,2,3},
{1,1,1,1,1,1,1,2,2,2},
{1,1,1,1,1,1,1,1,1,1},
{2,2,2,2,2,1,1,1,1,1} };
JLabel labelArray[][] = new JLabel[4][10];
public MyGame() {
gameFrame = new JFrame("Quest");
gamePanel = new JPanel();
gameFrame.addKeyListener(this);
fillMap();
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setDefaultLookAndFeelDecorated(true);
gameFrame.getContentPane().add(gamePanel, BorderLayout.CENTER);
gameFrame.pack();
gameFrame.setSize(600,450);
gameFrame.setResizable(false);
gameFrame.setVisible(true);
placeMap();
runGame();
}//MyGame Constructor
private void fillMap(){
ImageIcon grassIcon = new ImageIcon("img/grass.gif");
ImageIcon playerIcon = new ImageIcon("img/playerDown.gif");
ImageIcon waterIcon = new ImageIcon("img/water.gif");
for(int row = 0; row < 4; row++){
for(int col = 0; col < 10; col++){
if(mapArray[row][col] == 1){
labelArray[row][col] = new JLabel(grassIcon);
gamePanel.add(labelArray[row][col], new Integer(1));
}
if(mapArray[row][col] == 2){
labelArray[row][col] = new JLabel(waterIcon);
gamePanel.add(labelArray[row][col], new Integer(1));
}
if(mapArray[row][col] == 3){
labelArray[row][col] = new JLabel(playerIcon);
gamePanel.add(labelArray[row][col], new Integer(1));
playerX = col;
playerY = row;
}
}
}
}//fillMap
private void placeMap(){
for(int row = 0; row < 4; row++){
for(int col = 0; col < 10; col++){
labelArray[row][col].setLocation((col*20),(row*20));
}
}
}//placeMap
private void runGame(){
while(true){
placeMap();
}
}//runGame
public void keyTyped(KeyEvent e){
}
public void keyPressed(KeyEvent e){
if((e.getKeyCode()) == 37){//left
mapArray[playerY][playerX] = 1;//sets old player pos to grass
playerX--;
mapArray[playerY][playerX] = 3;//sets new player pos
fillMap(); // attempts to redraw the map
}
if((e.getKeyCode()) == 38){
System.out.println("go up");
}
if((e.getKeyCode()) == 39){
System.out.println("go right");
}
if((e.getKeyCode()) == 40){
System.out.println("go down");
}
}
public void keyReleased(KeyEvent e){
}
public static void main (String[] args){
MyGame newGame = new MyGame();
}//main
}//MyGame
Is there maybe a better way to do what im trying to do, ive tryed using
drawImage but found it confusing, could you point me to any good tutorials
on drawing images, or on games programming. I believe the above is working
but its drawing it behind the old tile, because if u press left ten times
you get a null out of bounds exception, where it goes off the screen.
thanks in advance
rob
do it, its hard to explain what im trying to do, but basically my problem is
that i cant get my program to draw a certain tile in a certain place, or
atleast it does but it doesnt show up, maybe its drawing behind the old
tile. here is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class MyGame implements KeyListener{
JFrame gameFrame;
JPanel gamePanel;
int playerX, playerY, newPlayerX, newPlayerY;
int[][] mapArray = { {1,1,1,1,1,1,1,1,2,3},
{1,1,1,1,1,1,1,2,2,2},
{1,1,1,1,1,1,1,1,1,1},
{2,2,2,2,2,1,1,1,1,1} };
JLabel labelArray[][] = new JLabel[4][10];
public MyGame() {
gameFrame = new JFrame("Quest");
gamePanel = new JPanel();
gameFrame.addKeyListener(this);
fillMap();
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setDefaultLookAndFeelDecorated(true);
gameFrame.getContentPane().add(gamePanel, BorderLayout.CENTER);
gameFrame.pack();
gameFrame.setSize(600,450);
gameFrame.setResizable(false);
gameFrame.setVisible(true);
placeMap();
runGame();
}//MyGame Constructor
private void fillMap(){
ImageIcon grassIcon = new ImageIcon("img/grass.gif");
ImageIcon playerIcon = new ImageIcon("img/playerDown.gif");
ImageIcon waterIcon = new ImageIcon("img/water.gif");
for(int row = 0; row < 4; row++){
for(int col = 0; col < 10; col++){
if(mapArray[row][col] == 1){
labelArray[row][col] = new JLabel(grassIcon);
gamePanel.add(labelArray[row][col], new Integer(1));
}
if(mapArray[row][col] == 2){
labelArray[row][col] = new JLabel(waterIcon);
gamePanel.add(labelArray[row][col], new Integer(1));
}
if(mapArray[row][col] == 3){
labelArray[row][col] = new JLabel(playerIcon);
gamePanel.add(labelArray[row][col], new Integer(1));
playerX = col;
playerY = row;
}
}
}
}//fillMap
private void placeMap(){
for(int row = 0; row < 4; row++){
for(int col = 0; col < 10; col++){
labelArray[row][col].setLocation((col*20),(row*20));
}
}
}//placeMap
private void runGame(){
while(true){
placeMap();
}
}//runGame
public void keyTyped(KeyEvent e){
}
public void keyPressed(KeyEvent e){
if((e.getKeyCode()) == 37){//left
mapArray[playerY][playerX] = 1;//sets old player pos to grass
playerX--;
mapArray[playerY][playerX] = 3;//sets new player pos
fillMap(); // attempts to redraw the map
}
if((e.getKeyCode()) == 38){
System.out.println("go up");
}
if((e.getKeyCode()) == 39){
System.out.println("go right");
}
if((e.getKeyCode()) == 40){
System.out.println("go down");
}
}
public void keyReleased(KeyEvent e){
}
public static void main (String[] args){
MyGame newGame = new MyGame();
}//main
}//MyGame
Is there maybe a better way to do what im trying to do, ive tryed using
drawImage but found it confusing, could you point me to any good tutorials
on drawing images, or on games programming. I believe the above is working
but its drawing it behind the old tile, because if u press left ten times
you get a null out of bounds exception, where it goes off the screen.
thanks in advance
rob