Si estas creando un juego podrias utilizar la libreria que estoy creando que te serviria muy bien para lo que necesitas, te dejo un ejemplo de un programa que crea una cuadricula de 8x8 y mueve una pieza de ajedrez en el tablero:
Codigo Fuente: https://github.com/luischavez/LGE Video: http://www.youtube.com/watch?v=Wu2EDvwjqsQ Ejecutable del ejemplo: http://www.mediafire.com/?d2x0ln4amgnkyzb
Código Javascript
:
Ver originalpackage test;
import java.awt.Color;
import javax.swing.JFrame;
import mx.com.leviathan.game.engine.event.CollisionEvent;
import mx.com.leviathan.game.engine.event.SpriteAdapter;
import mx.com.leviathan.game.engine.event.TouchEvent;
import mx.com.leviathan.game.engine.object.Sprite;
import mx.com.leviathan.game.engine.surface.DrawableSurface;
import mx.com.leviathan.game.engine.surface.Surface;
import mx.com.leviathan.game.engine.worker.WorkerHandler;
import mx.com.leviathan.game.engine.worker.event.TouchWorker;
import mx.com.leviathan.game.engine.worker.sprite.CollisionWorker;
import mx.com.leviathan.game.engine.worker.surface.SurfaceWorker;
import mx.com.leviathan.game.engine.worker.surface.TimeRepaintWorker;
/**
*
* @author Leviathan
*/
public class TEST {
public static final int ROWS = 8, COLUMNS = 8;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame ventana = new JFrame();
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.setSize(ROWS * 20, COLUMNS * 20);
ventana.setResizable(false);
DrawableSurface drawableSurface = new DrawableSurface();
drawableSurface.setBounds(0, 0, ROWS * 20, COLUMNS * 20);
ventana.add(drawableSurface);
Color color = Color.BLACK;
int id = 1;
for (int i = 0; i < ROWS; i++) {
color = color.equals(Color.BLACK) ? Color.WHITE : Color.BLACK;
for (int j = 0; j < COLUMNS; j++) {
color = color.equals(Color.BLACK) ? Color.WHITE : Color.BLACK;
Grid grid = new Grid(color, id++, i * 20, j * 20, 20, 20);
drawableSurface.addSprite(grid);
}
}
Peon peon = new Peon(0, 0, 0, 20, 20);
peon.getSpriteListeners().add(new SpriteAdapter() {
private Sprite sprite = null;
@Override
public void onTouch(Sprite source, TouchEvent event) {
if (event.getType() == TouchEvent.PRESSED) {
if (sprite != null || !source.isTouched()) {
sprite = null;
} else {
sprite = source;
}
}
}
@Override
public void onTouchSurface(Surface surface, TouchEvent event) {
if (event.getType() == TouchEvent.MOVED) {
if (sprite != null) {
sprite.update(event.getX(), event.getY());
}
}
}
@Override
public void onCollision(Sprite source, Sprite target, CollisionEvent event) {
if (event.getType() == CollisionEvent.CONTAINS) {
source.setTouched(false);
source.update(target.getX(), target.getY());
}
}
});
drawableSurface.addSprite(peon);
WorkerHandler workerHandler = new WorkerHandler();
TouchWorker touchWorker = new TouchWorker(drawableSurface);
touchWorker.setAutomatic(true);
workerHandler.getWorkers().add(touchWorker);
workerHandler.getWorkers().add(new CollisionWorker(drawableSurface));
workerHandler.getWorkers().add(new SurfaceWorker(drawableSurface));
workerHandler.getWorkers().add(new TimeRepaintWorker(30));
workerHandler.init();
ventana.pack();
ventana.setLocationRelativeTo(null);
ventana.setVisible(true);
}
}
Y este es el codigo para dibujar la pieza de ajedrez y las grillas:
Código Javascript
:
Ver originalpackage test;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import mx.com.leviathan.game.engine.object.MovableSprite;
import mx.com.leviathan.game.engine.util.ImageUtils;
/**
*
* @author Leviathan
*/
public class Peon extends MovableSprite {
private Image image = ImageUtils.loadImage("peon_blanco", "png");
public Peon(int id, int x, int y, int width, int height) {
super(id, x, y, width, height);
}
@Override
public void draw(Graphics graphics) {
Graphics2D graphics2D = (Graphics2D) graphics;
graphics2D.drawImage(image, getX(), getY(), getWidth(), getHeight(), null);
}
}
Código Javascript
:
Ver originalpackage test;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import mx.com.leviathan.game.engine.object.MovableSprite;
/**
*
* @author Leviathan
*/
public class Grid extends MovableSprite {
private Color color;
public Grid(Color color, int id, int x, int y, int width, int height) {
super(id, x, y, width, height);
this.color = color;
}
@Override
public void draw(Graphics graphics) {
Graphics2D graphics2D = (Graphics2D) graphics;
graphics2D.setColor(color);
graphics2D.fill3DRect(getX(), getY(), getWidth(), getHeight(), true);
}
}
Puedes agregar mas funcionalidades facilmente, la libreria no es perfecta pero pienso seguir mejorandola.