Basic Questions on animating tile based games & frameworks/engines

Started by
12 comments, last by Bartley 11 years, 6 months ago
Thanks a lot !Null! I'm going to try this out now!
Advertisement
Sweet. be sure to let us know how you get on
/********************************************************************************\
/**********************He Who Dares, Wins**********************************\
/********************************************************************************\
** 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.length; j++)
{
map[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.length; j++)
{
switch(map[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.length; j++)
{
map[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.length; j++)
{
switch(map[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;
}
}
}
}
}
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);
}
}
}
}
}

This topic is closed to new replies.

Advertisement