I realized this method was exactly what I needed, so I used the code provided in it. I modified the code according to Guest_Terrey*'s post so that it would be easier to handle.
How would I add a KeyListener that would use KeyEvents from the arrow keys to move around the screen?
import javax.swing.JFrame;
public class Main
{
private JFrame frame;
public Main()
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setTitle("RPG");
frame.setResizable(false);
//JScrollPane jsp = new JScrollPane(new Board());
//jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//frame.add(jsp);
frame.add(new Board());
frame.setVisible(true);
}
public static void main(String[] args)
{
new Main();
}
}
import java.awt.Graphics;
import javax.swing.JPanel;
public class Board extends JPanel
{
TileMap map = new TileMap();
public Board()
{
}
public void paintComponent(Graphics g)
{
int count = 0;
for(int i = 0; i < map.getWidth(); i++)
{
for (int k = 0; k < map.getWidth(); k++)
{
//g.drawImage(map.getTileSet().getTileTypeAtIndex(map.getTiles()[count]).getImage(), gpft(k), gpft(i), this);
//this just randomly generates some either water or grass tiles
double rand = Math.random();
if (rand < 0.25)
{
g.drawImage(map.getTileSet().getTileTypes()[0].getImage(), gpft(k), gpft(i), this);
}
else if (rand < 0.5)
{
g.drawImage(map.getTileSet().getTileTypes()[3].getImage(), gpft(k), gpft(i), this);
}
else if (rand < 0.75)
{
g.drawImage(map.getTileSet().getTileTypes()[2].getImage(), gpft(k), gpft(i), this);
}
else
{
g.drawImage(map.getTileSet().getTileTypes()[1].getImage(), gpft(k), gpft(i), this);
}
count++;
}
}
}
public int gpft(int t)//calculates the location to draw each tile image
{
return t * 32;//the tile images are 32 pixels by 32 pixels
}
}
public class TileMap
{
private TileSet tileSet;
private int[] tiles = new int[]{0, 1, 2, 3, 0, 1, 3, 2, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, 1, 0, 0, 2, 2, 0};
private int width;
public TileMap()
{
tileSet = new TileSet();
//width = (int)Math.sqrt(tiles.length);
width = 25;
}
public TileSet getTileSet()
{
return tileSet;
}
public int[] getTiles()
{
return tiles;
}
public int getWidth()
{
return width;
}
}
import java.awt.Toolkit;
public class TileSet
{
private Tile[] tileTypes = new Tile[4];
public TileSet()
{
init();
}
public void init()
{
tileTypes[0] = new Tile(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("grass.png")), "Grass");
tileTypes[1] = new Tile(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("monster_grass.png")), "Monster Grass");
tileTypes[2] = new Tile(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("sand_brick_path.png")), "Brick Path");
tileTypes[3] = new Tile(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("water.png")), "Water");
}
public Tile getTileTypeAtIndex(int index)
{
return tileTypes[index];
}
public Tile[] getTileTypes()
{
return tileTypes;
}
}
import java.awt.Image;
public class Tile
{
private Image image;
private String name;
public Tile(Image i, String n)
{
image = i;
name = n;
}
public void setImage(Image i)
{
image = i;
}
public void setName(String n)
{
name = n;
}
public Image getImage()
{
return image;
}
public String getName()
{
return name;
}
}

New Topic/Question
Reply



MultiQuote




|