Object Layer for a Java RPG
<p>I'm trying to make a basic RPG in java.  I think I have a good hold of the basics of java, but when it comes to this stuff I'm definitely a beginner.  Anyway, I thought that it would be best to create a map with two layers: a terrain layer with water, rocks, grass, etc... and an object layer with trees, houses, items- things that the player can interact with. So far I have a terrain layer, produced using a 2-Dim array.  My question is: how can I make an object layer, more specifically, how can I make a layer that contains objects whose coordinates can be interacted with? </p>

<p>Below is what I have so far. I apologize, my code must be organized strangely- like I said, I'm a beginner with this. Thanks for your time. </p>

<p>-Marcus</p>

<pre><code>import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.*;
import javax.imageio.ImageIO.*;
import java.awt.image.*;
import java.awt.image.BufferedImage.*;
import java.io.File.*;
import java.io.*;
import java.awt.Graphics2D.*;

public class mapStoreII extends JFrame implements KeyListener{


BufferedImage pic;
int width = 32;
int height = 32;
int px = 656;
int py = 656;

int[][] map_1 = {
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,10,5,11,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,3,14,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,13,6,12,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},        
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,16,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1}
        };

public mapStoreII(){
    setSize(875,750);
    setLocation(800,150);
    setTitle("Test_Organics_v.1");
    setResizable(true);
    setBackground(Color.black);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    addKeyListener(this);
    setVisible(true);   
}

public void paint(Graphics g){          
    Graphics2D graphics = (Graphics2D) g;
    for(int y = 0; y&lt;20; y++){
        for(int x = 0; x&lt;24; x++){

            try {
            pic = ImageIO.read(new File(getImage(getMap(y,x))));            
            } catch (IOException e) {}

            graphics.drawImage(pic, (x*width)+50, (y*height)+50, null);                         
            graphics.setPaint(Color.red);
            graphics.fill(new Ellipse2D.Double(px,py,32,32));       //Currently the player.
        }
    }
}

public int getMap(int y, int x){
    return map_1[y][x];
}

public String getImage(int a){
    if(a==2){
        return "shoreline_1.png";                           
    }
    else if(a==1){                      
        return "water_1.png";           
    }
    else if(a==9){
        return "player_on_dirt_1.png";
    }
    else if(a==3){
        return "island_left.png";
    }
    else if(a==4){
        return "island_right.png";
    }
    else if(a==5){
        return "island_up.png";
    }
    else if(a==6){
        return "island_down.png";
    }
    else if(a==7){
        return "chemical_1.png";
    }
    else if(a==10){
        return "island_topLC.png";
    }
    else if(a==11){
        return "island_topRC.png";
    }
    else if(a==12){
        return "island_botRC.png";
    }
    else if(a==13){
        return "island_botLC.png";
    }
    else if(a==14){
        return "island_center.png";
    }
    else if(a==15){
        return "shoreline_2.png";
    }
    else if(a==16){
        return "shoreline_3.png";
    }
    else{                                   
        return "grassydirt_1.png";          
    }
}

public void keyPressed(KeyEvent event){ 
    int code = event.getKeyCode();
        if(code == KeyEvent.VK_LEFT || code == KeyEvent.VK_A){
            px-=8;
            repaint();                                                                          
        }
        else if(code == KeyEvent.VK_RIGHT || code == KeyEvent.VK_D){
            px+=8;
            repaint();
        }
        else if(code == KeyEvent.VK_UP || code == KeyEvent.VK_W){
            py-=8;
            repaint();
        }
        else if(code == KeyEvent.VK_DOWN || code == KeyEvent.VK_S){
            py+=8;
            repaint();
        }
        else if(code == KeyEvent.VK_I){         
            lineDraw inventory = new lineDraw();    //Opens an inventory.
        }
    }

public void keyReleased(KeyEvent event){
}

public void keyTyped(KeyEvent event){
}

public static void main(String[] args){
    JFrame frame = new mapStoreII();
}

 }
</code></pre>
