Im totally new to game developing so be nice ;)
Before i get to the point i have to say that i have difficulties understanding on how to use threads and im not sure if my "basic structure" is right..
Ok..lets get to it. This is my Board Class:
package mortifera;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
public class Board extends JPanel implements Runnable, MouseListener {
private int posX, posY;
private Thread thread;
private Hero hero;
private Map map;
public Board(Hero hero, Map map) {
this.hero = hero;
this.map = map;
addMouseListener(this);
setBackground(Color.BLACK);
setDoubleBuffered(true);
}
public void addNotify(){
super.addNotify();
thread = new Thread(this);
thread.start();
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
// DRAW MAP. index -> index of map[] array
int index = 0;
while(index<(map.getMapWidthInTiles()*map.getMapHeightInTiles())){
for(int i=0; i<map.getMapWidthInTiles(); i++){
for(int j=0; j<map.getMapHeightInTiles(); j++){
g2d.drawImage(map.getTileImage(index), posX, posY, this);
posX += map.getTileWidth();
index++;
}
posX = 0;
posY += map.getTileHeight();
}
posY = 0;
}
// DRAW HERO
g2d.drawImage(hero.getImage(), hero.getX(), hero.getY(), this);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void run() {
while(true){
repaint();
}
}
@Override
public void mouseClicked(MouseEvent e) {
if(e.getButton() == 1){
hero.moveTo(e.getX(), e.getY());
}
}
Followed by my Map Class:
package mortifera;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Map {
private int mapWidth, mapHeight, tileWidth, tileHeight;
private int[] map =
{ 0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0
};
private Image[] tileImage;
public Map(int mapWidth, int mapHeight){
this.mapWidth = mapWidth;
this.mapHeight = mapHeight;
this.tileImage = new Image[2];
this.tileImage[1] = new ImageIcon(this.getClass().getResource("tile-grass.png")).getImage();
this.tileImage[0] = new ImageIcon(this.getClass().getResource("tile-mountain.png")).getImage();
this.tileWidth = this.tileImage[0].getWidth(null);
this.tileHeight = this.tileImage[0].getHeight(null);
}
public int getMapWidthInTiles(){
return this.mapWidth;
}
public int getMapHeightInTiles(){
return this.mapHeight;
}
public int getTileWidth(){
return this.tileWidth;
}
public int getTileHeight(){
return this.tileHeight;
}
public Image getTileImage(int index){
return this.tileImage[map[index]];
}
}
my Hero Class:
package mortifera;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Hero{
private int[] position;
private Image image;
public Hero(){
position = new int[2];
position[0] = 0; // X
position[1] = 0; // Y
image = new ImageIcon(getClass().getResource("held.gif")).getImage();
}
public void setPosition(int x, int y){
this.position[0] = x;
this.position[1] = y;
}
public int[] getPosition(){
return this.position;
}
public int getX(){
return this.position[0];
}
public int getY(){
return this.position[1];
}
public Image getImage(){
return this.image;
}
public void moveTo(int x, int y){
while(this.position[0]!=x && this.position[1]!=y){
if(this.position[0]<x){
this.position[0]++;
}
if(this.position[1]<y){
this.position[1]++;
}
}
}
}
and my Main Class:
package mortifera;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
Hero hero = new Hero();
Map map = new Map(10, 10);
Board board = new Board(hero, map);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(board);
frame.setSize(800, 600);
frame.setVisible(true);
}
}
At the moment a a little map is drawn and my hero...
- After clicking some time on my screen the game crashes..
- At the moment the Hero is "teleported" to the point i clicked. I want it to "go" there and know how to do it with a Swing Timer, but not how it works with Threads.
- Apart from those 2 problems, im very thankful for any hints or critics to my whole concept. Is this, what im trying to do, done in this way?
Thanks in advance and pardon my english