I'm making one of those puzzle games that has the empty tile which you need to slide them around to finish the picture. When I refer to the "Active Tile" I'm referring to the tile that's empty on the board.
I'm changing the position of the active tile and the tile that's one position in the opposite direction of the directional keys that were pressed (doing this to simulate sliding), however, when I press a key to move them, the active tile is still in the same position as before (it will not move).
Here's all the relavent code:
package dev.geodox.shuffle.gfx;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.List;
import javax.swing.JPanel;
import dev.geodox.shuffle.helper.Difficulty;
@SuppressWarnings("serial")
public class ShuffleScreen extends JPanel
{
private Dimension windowSize;
private Random shuffler;
private BufferedImage image;
private Difficulty diff;
private Tile[][] tiles;
private Tile activeTile;
public int diffWidth;
public int diffHeight;
private int tileWidth;
private int tileHeight;
private int tileOffsetX;
private int tileOffsetY;
public ShuffleScreen(BufferedImage image, final Difficulty diff)
{
this.image = image;
this.diff = diff;
diffWidth = diff.getWidth();
diffHeight = diff.getHeight();
tiles = new Tile[diffWidth][diffHeight];
tileWidth = image.getWidth() / diffWidth;
tileHeight = image.getHeight() / diffHeight;
addKeyListener(new KeyListener()
{
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_LEFT)
{
if(activeTile.getPosX() > 0)
{
swap(tiles[activeTile.getPosX() + 1][activeTile.getPosY()], activeTile);
repaint();
}
}
else if(e.getKeyCode() == KeyEvent.VK_RIGHT)
{
if(activeTile.getPosX() < diffWidth)
{
swap(tiles[activeTile.getPosX() - 1][activeTile.getPosY()], activeTile);
repaint();
}
}
else if(e.getKeyCode() == KeyEvent.VK_UP)
{
if(activeTile.getPosY() > 0)
{
swap(tiles[activeTile.getPosX()][activeTile.getPosY() + 1], activeTile);
repaint();
}
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN)
{
if(activeTile.getPosY() < diffHeight)
{
swap(tiles[activeTile.getPosX()][activeTile.getPosY() - 1], activeTile);
repaint();
}
}
}
});
splitImage();
}
private void splitImage()
{
for(int i = 0; i < diff.getWidth(); i++)
{
for(int j = 0; j < diff.getHeight(); j++)
{
tiles[i][j] = new Tile(image.getSubimage(i * tileWidth, j * tileHeight, tileWidth, tileHeight), diff, tileWidth, tileHeight);
tiles[i][j].setPos(i, j);
}
}
}
public void shuffle()
{
int toRemoveX = 0, toRemoveY = 0;
shuffler = new Random();
Collections.shuffle(Arrays.asList(tiles));
for(int i = 0; i < tiles.length; i++)
Collections.shuffle(Arrays.asList(tiles[i]));
for(int i = 0; i < tiles.length; i++)
{
for(int j = 0; j < tiles[0].length; j++)
{
tiles[i][j].setPos(i, j);
}
}
toRemoveX = shuffler.nextInt(tiles.length);
toRemoveY = shuffler.nextInt(tiles[0].length);
activeTile = tiles[toRemoveX][toRemoveY];
revalidate();
}
private void swap(Tile from, Tile to)
{
tiles[from.getPosX()][from.getPosY()].setPos(to.getPosX(), to.getPosY());
tiles[from.getPosX()][from.getPosY()] = to;
tiles[to.getPosX()][to.getPosY()].setPos(from.getPosX(), from.getPosY());
tiles[to.getPosX()][to.getPosY()] = from;
activeTile = from;
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
setBackground(Color.BLACK);
for (int i = 0; i < tiles.length; i++)
{
for(int j = 0; j < tiles[0].length; j++)
{
tiles[i][j].render(g, tileOffsetX, tileOffsetY);
}
}
g.setColor(Color.RED);
g.drawRect(activeTile.getPosX() * tileWidth + tileOffsetX, activeTile.getPosY() * tileHeight + tileOffsetY, tileWidth, tileHeight);
}
public void setWindowSize(Dimension windowSize)
{
this.windowSize = windowSize;
tileOffsetX = (int) (windowSize.getWidth() - image.getWidth()) / 2;
tileOffsetY = (int) (windowSize.getHeight() - image.getHeight()) / 2;
}
}
Also, I'll throw in the render method from the Tile Class:
public void render(Graphics g, int offsetX, int offsetY)
{
g.drawImage(image, posX * tileWidth + offsetX, posY * tileHeight + offsetY, null);
}
As you can see, I store the positions in the tile objects.