Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Making a WorldGrid

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Making a WorldGrid

    I want to create a map in a game I'm making. Right now I have already made a texture for the map, but I want the map to be cut up in 50 * 50 blocks of pixels. Every block will be of a certain type of land. For example there will be plains, forest, water etc. I also want to be able to set certain settings for all the blocks like who owns the area.
    Right now this is my code:
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
     
    import org.lwjgl.input.Mouse;
    import org.newdawn.slick.*;
    import org.newdawn.slick.state.*;
     
    public class GameScreen extends BasicGameState 
    {
    	int day = 1;
    	int month = 1;
    	int year = 2012;
     
    	static boolean paused = false;
     
    	ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor(); 
     
    	Image optionButton, tutorial, grid;
     
    	float mapX = 0;
    	float mapY = 0;
     
    	int heightgrid, lengthgrid;
     
    	public GameScreen(int state)
    	{
    		timer.scheduleAtFixedRate(new Runnable() 
    		{
    		    public void run() 
    		    {
    		    	if(Main.playing && !paused)
    		    	{
    		    		day++;
    		    	}
    		    }
    		}, 0, 1, TimeUnit.SECONDS);
    	}
     
    	public void init(GameContainer container, StateBasedGame state) throws SlickException
    	{
    		container.setShowFPS(Main.fpsCounter);
    		optionButton = new Image("res/gui/buttons/smalloptionbutton.png");
    		tutorial = new Image("res/maps/tutorial.png");
    		grid = new Image("res/grid.png");
     
    		if(optionButton == null || tutorial == null || grid == null)
    		{
    			System.err.println("Image problems");
    		}
     
    		heightgrid = tutorial.getHeight() / 50;
    		lengthgrid = tutorial.getWidth() / 50;
    	}
     
    	public void render(GameContainer container, StateBasedGame state, Graphics graphics) throws SlickException
    	{
    		graphics.setBackground(Color.black);
    		if(!paused)
    		{
    			tutorial.draw(mapX, mapY);
    			graphics.setColor(Color.white);
    			graphics.drawString("Day " +day, 100, 100);
    			graphics.drawString("Month " +month, 100, 150);
    			graphics.drawString("Year " +year, 100, 200);
     
    			for(int j = 0; j <= heightgrid; j++)
    			{
    				for(int i = 0; i <= lengthgrid; i++)
    				{
    					grid.draw(i * 50, j * 50);
    				}
    			}
    		}
    		if(paused)
    		{
    			graphics.setColor(Color.white);
    			graphics.drawString("Pause Screen", 100, 100);
    			graphics.drawString("Press B to get back to playing.", 100, 150);
    			optionButton.draw(200, 200);
    		}
    	}
     
    	public void update(GameContainer container, StateBasedGame state, int delta) throws SlickException
    	{
    		Input input = container.getInput();
     
    		int posX = Mouse.getX();
    	    int posY = Mouse.getY();
     
    		if(day > 30)
    		{
    			month++;
    			day = 1;
    		}
    		if(month > 12)
    		{
    			year++;
    			month = 1;
    		}
     
    		if(!paused)
    		{
    			if(Mouse.isInsideWindow() == false)
    		    {
    		    	paused = true;
    		    }
     
    		    if(input.isKeyDown(Input.KEY_ESCAPE))
    		    {
    		    	paused = true;
    		    }
     
    		    if(input.isKeyDown(Input.KEY_DOWN))
    		    {
    		    	mapY-=5;
    		    }
     
    		    if(input.isKeyDown(Input.KEY_UP))
    		    {
    		    	mapY+=5;
    		    }
     
    		    if(input.isKeyDown(Input.KEY_LEFT))
    		    {
    		    	mapX+=5;
    		    }
     
    		    if(input.isKeyDown(Input.KEY_RIGHT))
    		    {
    		    	mapX-=5;
    		    }
    		}
     
    		if(paused)
    		{
    			if(input.isKeyDown(Input.KEY_B))
    		    {
    		    	paused = false;
    		    }
     
    			if(posX > 200 && posX < 300 && posY > 370 && posY < 400)
    		    {
    		    	if(Mouse.isButtonDown(0))
    		    	{
    		    		state.enterState(3);
    		    		OptionScreen.fromPause = true;
    		        }
    		    }
    		}
    	}
     
    	public int getID()
    	{
    		return 4;
    	}
    }
    The code i have doesn't really work, because it simply draws the grid textures on the screen, but I want them on the map texture (tutorial.png). I want those squares to stay in position when I move the map around my screen with the arrows and be able to set certain properties to those grid blocks. One being for example water and the other being ground.
    Does anyone know how to do that?


  2. #2
    Junior Member ShadeDarkan's Avatar
    Join Date
    Jul 2012
    Location
    Houston, TX
    Posts
    13
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default Re: Making a WorldGrid

    You are obviously doing something beyond my capabilities, but you might be able to get what you want done by using the java.awt.Graphics2D class to draw the grid straight to the tutorial image object before rendering it.

Similar Threads

  1. Problem of Making Jar
    By cppcppcpp in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2012, 11:54 AM
  2. Making Methods
    By kilroyjr in forum Java Theory & Questions
    Replies: 7
    Last Post: April 7th, 2011, 05:19 PM
  3. Making a clock
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 7th, 2011, 04:36 PM
  4. about making .class into .jar
    By sibbe in forum Java Theory & Questions
    Replies: 7
    Last Post: November 13th, 2010, 01:50 PM
  5. Digital map application with Java GUI
    By donjuan in forum AWT / Java Swing
    Replies: 3
    Last Post: May 15th, 2009, 03:32 AM