Jump to content

  • Log In with Google      Sign In   
  • Create Account


Bartley

Member Since 10 Aug 2010
Offline Last Active Oct 20 2012 08:43 AM
-----

Posts I've Made

In Topic: Java - Basic Animation/Threading Woes

17 October 2012 - 02:29 PM

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

/*
* 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);
	}
}
This contains game logic

/*
*  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;
	}  
}

This is for the individual tiles

/*
* 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;
	}
}

This allows Imageloader6 to interact with the individual tiles

/*
* 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();
	}
  
}

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

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;
	}
  
}

In Topic: Basic Questions on animating tile based games & frameworks/engines

12 October 2012 - 08:48 AM

Figured it out, for what that's worth. Posting incase it's of any use to the next guy!

import javax.swing.*;
import java.awt.*;
import javax.swing.ImageIcon;
public class ImageLoader extends JFrame
{
   
/* INSTANCE FIELDS */
   
    private Image sand;
    private Image grass;
    private final int tileSize = 64;
    private final int WIDTH = 256;
    private final int HEIGHT = 192;
    private int [][] map;
   
/* CONSTRUCTOR */
    public ImageLoader()
    {
	    setSize(WIDTH, HEIGHT);
	    loadImages();
	    prepareMap();
	    printMapIntegers();
	    setVisible(true);
	    repaint();
    }
/* METHODS */
   
    // Loads Images
    public void loadImages()
    {
	    sand = new ImageIcon("C:\\My Folder\\Game Development\\Testing\\Graphics\\sandTile.png").getImage();
	    grass = new ImageIcon("C:\\My Folder\\Game Development\\Testing\\Graphics\\grassTile.png").getImage();
    }
   
    // Sets an Integer for each tile, representing whether it is grass or sand
    public void prepareMap()
    {
	    map = new int [HEIGHT / tileSize][WIDTH / tileSize]; // R = 3 / C = 4
	    for(int r = 0 ; r < 3 ; r++)
	    {
		    for(int c = 0 ; c < 4 ; c++)
		    {
			    // Is a SAND TILE
			    if(c > 0 && c < 3)
			    {
				    map[r][c] = 0;
			    }
			   
			    // Is a GRASS TILE
			    else
			    {
				    map[r][c] = 1;
			    }   
		    }
	    }
    }
   
    // Prints Integer Values for tiles
    public void printMapIntegers()
    {
	    for(int r = 0 ; r < 3 ; r++)
	    {
		    for(int c = 0 ; c < 4 ; c++)
		    {
			    System.out.print("" + map[r][c]);
		    }
		    System.out.println("");
	    }
    }
   
    // Paints the map
    public void paint(Graphics g)
    {
	    for(int r = 0 ; r < 3 ; r++)
	    {
		    for(int c = 0 ; c < 4 ; c++)
		    {
			    // If integer value is 0, draw SAND
			    if(map[r][c] == 0)
			    {
				    g.drawImage(sand, c*64, r*64, null);
			    }
			   
			    // Else, draw GRASS
			    else
			    {
				    g.drawImage(grass, c*64, r*64, null);
			    }
		    }
	    }
    }
}

In Topic: Basic Questions on animating tile based games & frameworks/engines

11 October 2012 - 09:29 AM

** CODE PROVIDED BY !NULL**

Getting somewhere with it, but hindered by limited knowledge of the graphics class.

This is the launcher class:

package graphicstester;
import javax.swing.JFrame;
public class GraphicsLauncher
{
	public static void main(String[] args)
	{
		GraphicsTester gt = new GraphicsTester();
		gt.setTitle("Graphics Tester");
		gt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		gt.setVisible(true);
	  
	}
}

This is the first attempt at the tester. The frame loads, but doesn't draw the images. This may be because the paint method is automatically by the frame too early.

package graphicstester;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.ImageIcon;

public class GraphicsTester extends JFrame
{
	static final int WIDTH = 640;
	static final int HEIGHT = 480;
	int tileSize = 32;
	int map[][];
	Random rand = new Random();
	Image tile1;
	boolean imagesLoaded;
	Graphics g;
  
  
/* CONSTRUCTOR */
  
	public GraphicsTester()
	{
		setSize(WIDTH, HEIGHT);
		loadImages();
		init();
	  
		setVisible(true);
	  
	}
/* METHODS */
  
	public void init()
	{
		map = new int[WIDTH / tileSize][HEIGHT / tileSize];
	  
		// Assigns an integer value to each tile
		for(int i = 0; i < map.length; i++)
		{
			for(int j = 0; j < map[i].length; j++)
			{
				map[i][j] = 0;
			}
		}
	}
  
	// Loads Images
	public void loadImages()
	{
		tile1 = new ImageIcon("c:\\My Folder\\Game Development\\Testing\\Graphics Testing\\Graphics Tester\\Graphics\	ile1.jpg").getImage();
	  
		if(tile1 != null)
		{
			imagesLoaded = true;
			System.out.println("GraphicsTester.loadImages(): Images loaded = " + imagesLoaded);
		}
	}
  
	// Draws the map according to the assigned integer values
	public void paint(Graphics g)
	{
		//Graphics g2 = (Graphics2D)g;
		// Works through the rows
		for(int i = 0; i < map.length; i++)
		{
			// Works through the columns
			for(int j = 0; j < map[i].length; j++)
			{
				switch(map[i][j])
				{
					case(0):
						g.drawImage(tile1,i*32,j*32, null); // Problem, graphics hasn't been initialised
						break;
//					case(1):
//						g.drawImage('your interesting tile',i*32,j*32);
//						break;
					default:
						g.drawImage(tile1,i*32,j*32, null);
						break;
				}
			}
		}
	}
}

This is the second attempt. It throws a null pointer at 78, probably because the graphics object hasn't been initialised. Did some tutorials on drawing images, but not sure how to draw from a loop. Any hints on solving this? At any rate, I'm going to have to learn more about the fundamentals. Time for a trip to the library!

package graphicstester;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.ImageIcon;

public class GraphicsTester extends JFrame
{
	static final int WIDTH = 640;
	static final int HEIGHT = 480;
	int tileSize = 32;
	int map[][];
	Random rand = new Random();
	Image tile1;
	boolean imagesLoaded;
	Graphics g;
  
  
/* CONSTRUCTOR */
  
	public GraphicsTester()
	{
		setSize(WIDTH, HEIGHT);
		loadImages();
		init();
		drawMap();
		setVisible(true);
	  
	}
/* METHODS */
  
	public void init()
	{
		map = new int[WIDTH / tileSize][HEIGHT / tileSize];
	  
		// Assigns an integer value to each tile
		for(int i = 0; i < map.length; i++)
		{
			for(int j = 0; j < map[i].length; j++)
			{
				map[i][j] = 0;
			}
		}
	}
  
	// Loads Images
	public void loadImages()
	{
		tile1 = new ImageIcon("c:\\My Folder\\Game Development\\Testing\\Graphics Testing\\Graphics Tester\\Graphics\	ile1.jpg").getImage();
	  
		if(tile1 != null)
		{
			imagesLoaded = true;
			System.out.println("GraphicsTester.loadImages(): Images loaded = " + imagesLoaded);
		}
	}
  
	// Draws the map according to the assigned integer values
	public void drawMap()
	{
		//Graphics g2 = (Graphics2D)g;
		// Works through the rows
		for(int i = 0; i < map.length; i++)
		{
			// Works through the columns
			for(int j = 0; j < map[i].length; j++)
			{
				switch(map[i][j])
				{
					case(0):
						g.drawImage(tile1,i*32,j*32, null); // Problem, graphics hasn't been initialised
						break;
//					case(1):
//						g.drawImage('your interesting tile',i*32,j*32);
//						break;
					default:
						g.drawImage(tile1,i*32,j*32, null);
						break;
				}
			}
		}
	}
}

In Topic: Basic Questions on animating tile based games & frameworks/engines

10 October 2012 - 07:14 AM

Thanks a lot !Null! I'm going to try this out now!

In Topic: Basic Questions on animating tile based games & frameworks/engines

09 October 2012 - 05:03 PM

I'm just a bit confused as to why you have a grid of buttons.


It may not be a very good set up. I mainly used it because I already had used something similar to make a battleships game. Each button corresponds to a tile object, which stores among other things the ID of the actor (-1 if vacant).

Do you want to select characters with the mouse or whatever and have them move to that point (animating while moving?)


Yep, that's it. You click to select the actor within, then click again to move. It works, but it looks bad. There are lines separating the buttons (which can probably be removed), and animating movement between buttons is a worry. Can I ask how you built the map for your games? Thanks again

PARTNERS