Sign in to follow this  

Java - Basic Animation/Threading Woes

This topic is 1917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

Been stuck on this for a few days. Would really appreciate a little help.

I'm trying to learn the basics of animating movement on a 2D Tileset. Characters can move in 8 directions on the board. If I click several tiles distant, the intermediate moves are not shown - the character just moves instantly to the end point

I used println to feed back information. It seems the thread is being created, but that it is not calling the paint method.




This method gets the tiles the character must pass through to get to his destination, moves the character, starts a thread to call repaint, then sleeps to allow the animation (which doesn't happen) to occur.
[source lang="java"]public void moveActor(int destinationIn)
{

LinkedList <Integer> actorMoves = calculateMoves(destinationIn); // get the int values representing the co-ordinates the actor must move through to get to destination

// Iterates through the actorMoves
for(int i = 0 ; i < actorMoves.size() ; i++)
{
System.out.println("MoveActor: Move " + i + ": " + actorMoves.get(i));

// Resets the actor Id of the current position to -1 to denote empty
int currentPos = actorList.get(selectedActorId).getActorPosition();
board.setActorIdInTile(currentPos, -1);

// Sets the actor position in the actor object and on board
actorList.get(selectedActorId).setActorPosition(actorMoves.get(i));
board.setActorIdInTile(actorMoves.get(i), selectedActorId);

// Starts a thread to update the board
new Thread()
{
public void run()
{
System.out.println("Paint thread started");
repaint();
}
}.start();

// Puts main thread to sleep
try
{
System.out.println("Main thread sleeping");
Thread.sleep(500);
}
catch (InterruptedException ex)
{
Logger.getLogger(ImageLoader6.class.getName()).log(Level.SEVERE, null, ex);
}

System.out.println("Main thread active");
}

}[/source]
This is the paint method. First it draws the tiles, then the characters on top.
[source lang="java"]// Paints the map
public void paint(Graphics g)
{
System.out.println("Paint Method started");
for(int i = 0 ; i < 100 ; i++)
{
String type = board.getTileType(i);

int x = (i % 10) * 64;
int y = (i /10) * 64;

// Checks the tile object to determine whether it is grass or sand
if(type.equalsIgnoreCase("grass"))
{
g.drawImage(grass, x, y, null);
}

else
{
g.drawImage(sand, x, y, null);
}
}

// Works through the list of actors, draws them
for(int j = 0 ; j < actorList.size() ; j++)
{
// Get Actor's position & convert to X,Y coordinates
int actorPosition = actorList.get(j).getActorPosition();

int actorX = (actorPosition % 10) * tileSize;
int actorY = (actorPosition / 10) * tileSize;

g.drawImage(actorList.get(j).getActorImage(), actorX, actorY, null);
}

try
{
Thread.sleep(20);
}
catch (InterruptedException ex)
{
Logger.getLogger(ImageLoader6.class.getName()).log(Level.SEVERE, null, ex);
}

}// End of paint()[/source]

Here's a sample of the output. It seems the paint method is not being called by the thread.

MoveActor: Move 0: 22
Main thread sleeping
Paint thread started
Main thread active
MoveActor: Move 1: 33
Main thread sleeping
Paint thread started
Main thread active
MoveActor: Move 2: 44
Main thread sleeping
Paint thread started
Main thread active
Paint Method started

If you need me to post the whole program, no problems. Let me know. Any help hugely appreciated!

Share this post


Link to post
Share on other sites
This is just the tester I've been using to try to come to grips with animation. Hope to integrate it with the actual game later. Just click on the black spot, then any other square. You'll notice the black spot (representing a character) jumps to the final tile, without displaying on any of the intermediaries. The plan is to get the character to move to each square (first) , then break the move from each square down into a series of smaller moves with individual animations.


Here goes:

This launches the program

[CODE]
/*
* Launches Image loader
*/

import javax.swing.JFrame;

public class ImageLauncher
{

public static void main(String[] args)
{
ImageLoader6 loader = new ImageLoader6();
loader.setTitle("Basic Image Loader");
loader.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}[/CODE]
This contains game logic

[CODE]/*
* Game logic is in this class
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
public class ImageLoader6 extends JFrame
{

/* INSTANCE FIELDS */

private Image sand;
private Image grass;
private Image character;
private final int tileSize = 64;
private final int WIDTH = 640;
private final int HEIGHT = 640;
private JPanel gamePanel;
private ActionListener MouseListener;
private GameBoard board;
private ArrayList <Actor> actorList;
private final int BOARD_WIDTH = WIDTH / tileSize;
private final int BOARD_HEIGHT = HEIGHT / tileSize;
java.util.Timer timer;


private int selectedActorId;
private boolean isActorSelected;

/* CONSTRUCTOR */
public ImageLoader6()
{
// Initialise Instance fields
board = new GameBoard();
actorList = new ArrayList <Actor> ();
selectedActorId = 0;
isActorSelected = false;

// Add an Actor to the list
actorList.add(new Actor(actorList.size(), 11, "C:\\My Folder\\Game Development\\Testing\\Graphics\\char.png"));
board.setActorIdInTile(11, actorList.size()-1);

add(createGamePanel());
setSize(WIDTH, HEIGHT);
loadImages();
setVisible(true);
repaint();

}
/* METHODS */

// Loads Images
public void loadImages()
{
sand = new ImageIcon("C:\\My Folder\\Game Development\\Testing\\Graphics\\sandTile.jpg").getImage();
grass = new ImageIcon("C:\\My Folder\\Game Development\\Testing\\Graphics\\grassTile.jpg").getImage();
}


// Creates a panel to display Images
public JPanel createGamePanel()
{
gamePanel = new JPanel();
addMouseListener(new MouseListener());
return gamePanel;
}

// Paints the map
public void paint(Graphics g)
{
System.out.println("Paint Method started");
for(int i = 0 ; i < 100 ; i++)
{
String type = board.getTileType(i);

int x = (i % 10) * 64;
int y = (i /10) * 64;

// Checks the tile object to determine whether it is grass or sand
if(type.equalsIgnoreCase("grass"))
{
g.drawImage(grass, x, y, null);
}

else
{
g.drawImage(sand, x, y, null);
}
}

// Works through the list of actors, draws them
for(int j = 0 ; j < actorList.size() ; j++)
{
// Get Actor's position & convert to X,Y coordinates
int actorPosition = actorList.get(j).getActorPosition();

int actorX = (actorPosition % 10) * tileSize;
int actorY = (actorPosition / 10) * tileSize;

g.drawImage(actorList.get(j).getActorImage(), actorX, actorY, null);
}

try
{
Thread.sleep(20);
}
catch (InterruptedException ex)
{
Logger.getLogger(ImageLoader6.class.getName()).log(Level.SEVERE, null, ex);
}

}// End of paint()


// Mouse Listener
private class MouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
Point b = e.getPoint();

int relX = e.getX();
int relY = e.getY();
System.out.println("Mouse x: " + relX);
System.out.println("Mouse y: " + relY);

// Determines which tile the click occured on
int xTile = relX/tileSize;
int yTile = relY/tileSize;

System.out.println("X Tile: " + xTile);
System.out.println("Y Tile: " + yTile);

// Get the position of the tile in ArrayList format
int arrayLPos = (xTile * 1) + (yTile * 10);
System.out.println("MouseListener: ArrayListPosition: " + arrayLPos);

if(isActorSelected == false)
{
System.out.println("MouseListener: Actor selected: false");
// Check if that tile has an Actor
int tempSelectedActorId = board.getTileActorId(arrayLPos);
System.out.println("MouseListener: Selected actor Id: " + tempSelectedActorId);

// If tempSelectedActor Id isn't -1,
if(tempSelectedActorId != -1)
{
selectedActorId = tempSelectedActorId;
isActorSelected = true;
System.out.println("MouseListener: Selected Actor is: " + selectedActorId);
}
}

else
{
System.out.println("MouseListener: Actor selected: true");
moveActor(arrayLPos);
}
}
}

// Moves the actor (should update graphics, but doesn't)
public void moveActor(int destinationIn)
{
LinkedList <Integer> actorMoves = calculateMoves(destinationIn); // get the int values representing the co-ordinates the actor must move through to get to destination

// Iterates through the actorMoves
for(int i = 0 ; i < actorMoves.size() ; i++)
{
System.out.println("MoveActor: Move " + i + ": " + actorMoves.get(i));

// Resets the actor Id of the current position to -1 to denote empty
int currentPos = actorList.get(selectedActorId).getActorPosition();
board.setActorIdInTile(currentPos, -1);

// Sets the actor position in the actor object and on board
actorList.get(selectedActorId).setActorPosition(actorMoves.get(i));
board.setActorIdInTile(actorMoves.get(i), selectedActorId);

// Starts a thread to update the board
new Thread()
{
public void run()
{
System.out.println("Paint thread started");
repaint();
}
}.start();
// Puts main thread to sleep
try
{
System.out.println("Main thread sleeping");
Thread.sleep(500);
}
catch (InterruptedException ex)
{
Logger.getLogger(ImageLoader6.class.getName()).log(Level.SEVERE, null, ex);
}

System.out.println("Main thread active");
}

}

// Calculates the tiles the actor will have to pass through to get to their destination
public LinkedList calculateMoves(int destinationIn)
{
// Get current position
int currentPos = actorList.get(selectedActorId).getActorPosition();

// Get Destination
int destination = destinationIn;
int movementValue = 0;

// Create a linked List to store the moves
LinkedList <Integer> movesList = new LinkedList<>();

do
{
currentPos = currentPos + movementValue;
movementValue = 0;
System.out.println("CalcMove: Current position is " + currentPos);
int currentY = currentPos / BOARD_WIDTH;
int currentX = currentPos % BOARD_WIDTH;
int destinationX = destination % BOARD_WIDTH;
int destinationY = destination / BOARD_WIDTH;

if (destinationX > currentX)
{
movementValue += 1;
System.out.println("CalcMove: Destination X " + (destinationX) + " is greater than Current X position" + (currentX));
System.out.println("CalcMove: movementValue is " + movementValue);
}
if (destinationX < currentX)
{
movementValue -= 1;
System.out.println("CalcMove: Destination X " + (destinationX) + " is less than Current X position" + (currentX));
System.out.println("CalcMove: movementValue is " + movementValue);
}
if (destinationY > currentY)
{
movementValue += BOARD_WIDTH;
System.out.println("CalcMove: Destination Y " + (destinationY) + " is greater than Current Y position" + (currentY));
System.out.println("CalcMove: movementValue is " + movementValue);
}
if (destinationY < currentY)
{
movementValue -= BOARD_WIDTH;
System.out.println("CalcMove: Destination Y " + (destinationY) + " is less than Current Y position" + (currentY));
System.out.println("CalcMove: movementValue is " + movementValue);
}

movesList.add(currentPos + movementValue);
}while(currentPos + movementValue != destination);

return movesList;
}
}[/CODE]

This is for the individual tiles

[CODE]/*
* A tile is a single square on the board
*/
public class Tile
{
/* INSTANCE FIELDS */
private int actorId;
private String tileType;

/* CONSTRUCTOR */
public Tile(String tileTypeIn)
{
actorId = -1;
tileType = tileTypeIn;
}

/* METHODS */

// Returns the actor Id of a given tile
public int getActorId()
{
return actorId;
}

// Sets the actor Id in a tile
public void setActorId(int actorIdIn)
{
actorId = actorIdIn;
}

// Returns the type of tile (i.e. grass or sand)
public String getTileType()
{
return tileType;
}
}[/CODE]

This allows Imageloader6 to interact with the individual tiles

[CODE]/*
* Allows the main program (Imageloader) to interact with the individual tiles
*/
import java.util.ArrayList;

public class GameBoard
{
/* INSTANCE FIELDS */
public static ArrayList <Tile> grid;

/* CONSTRUCTOR */
public GameBoard()
{
grid = new ArrayList <Tile> (100);

for (int i = 0; i < 100; i++)
{
if(i % 10 > 1 && i % 10 < 8)
{
grid.add(i, new Tile("sand"));
}

else
{
grid.add(i, new Tile("grass"));
}

}
}

/* METHODS */

// Returns actor Id of a given tile
public int getTileActorId(int terrainTileIn)
{
return grid.get(terrainTileIn).getActorId();
}

// Sets actor Id of a tile
public void setActorIdInTile(int terrainTileIn, int actorIdIn)
{
grid.get(terrainTileIn).setActorId(actorIdIn);
}

// Returns tile type
public String getTileType(int terrainTileIn)
{
return grid.get(terrainTileIn).getTileType();
}

}[/CODE]

This class is for the Actors, or characters which will make up the game

[CODE]
public class Actor
{
/* INSTANCE FIELDS */
private int position;
private int actorId;
private Image charImage;

/* CONSTRUCTOR */
public Actor(int actorIdIn, int positionIn, String imageLocation)
{
actorId = actorIdIn;
position = positionIn;
charImage = new ImageIcon(imageLocation).getImage();
}

/* METHODS */

// Returns actor's position
public int getActorPosition()
{
return position;
}

// Sets actor's position
public void setActorPosition(int positionIn)
{
position = positionIn;
}

// Returns image for the actor
public Image getActorImage()
{
return charImage;
}

}[/CODE] Edited by Bartley

Share this post


Link to post
Share on other sites
Swing has [url="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html"]rules you must follow to use it concurrently[/url].

Share this post


Link to post
Share on other sites

This topic is 1917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this