Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8471799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:53:42+00:00 2026-06-10T16:53:42+00:00

I need some help with making a tiled map, I’m just getting a white

  • 0

I need some help with making a tiled map, I’m just getting a white screen instead of a map with images ( tiles) on it. Can someone help with that?

World.java:

   package game.test.src;

   import java.awt.Graphics;
   import java.awt.Image;
   import java.awt.Rectangle;

   import javax.swing.ImageIcon;

    public class World {

private Rectangle[] blocks;
private Image[] blockImg;
private final int arrayNum = 500;

//Block Images
private Image BLOCK_GRASS, BLOCK_DIRT, BLOCK_STONE, BLOCK_SKY;

private int x, y;

public World(){
    BLOCK_GRASS = new ImageIcon("C:/Users/Pim/Desktop/2D game test/Game test 2/src/game/test/src/images/tile_grass").getImage();
    BLOCK_DIRT = new ImageIcon("C:/Users/Pim/Desktop/2D game test/Game test 2/src/game/test/src/images/tile_dirt").getImage();
    BLOCK_STONE = new ImageIcon("C:/Users/Pim/Desktop/2D game test/Game test 2/src/game/test/src/images/tile_stonek").getImage();
    BLOCK_SKY = new ImageIcon("C:/Users/Pim/Desktop/2D game test/Game test 2/src/game/test/src/images/tile_sky").getImage();
    blocks = new Rectangle[500];
    blockImg = new Image[500];
    
    loadArrays();
}

private void loadArrays()
{
    for(int i = 0; i < arrayNum; i++)
    {
        if(x >= 500){
            x = 0;
            y += 20;
        }
        if(i >= 0 && i < 100)
        {
            blockImg[i] = BLOCK_SKY;
            blocks[i] = new Rectangle(x, y, 20, 20);
        }
        if(i >= 100 && i < 125)
        {
            blockImg[i] = BLOCK_GRASS;
            blocks[i] = new Rectangle(x, y, 20, 20);
        }
        if(i >= 125 && i < 225)
        {
            blockImg[i] = BLOCK_DIRT;
            blocks[i] = new Rectangle(x, y, 20, 20);
        }
        if(i >= 225 && i < 500)
        {
            blockImg[i] = BLOCK_STONE;
            blocks[i] = new Rectangle(x, y, 20, 20);
        }
        x += 20;
        
    }
    
}

public void draw(Graphics g)
{
    for(int i = 0; i< arrayNum; i++){
        g.drawImage(blockImg[i], blocks[i].x, blocks[i].y, null);
    }
}   
}

And here is GamePanel.java:

    package game.test.src;

import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

public class GamePanel extends JPanel implements Runnable {
    //Double buffering
    private Image dbImage;
    private Graphics dbg;
    //JPanel variables
    static final int GWIDTH = 500, GHEIGHT = 400;
    static final Dimension gameDim = new Dimension(GWIDTH, GHEIGHT);
    //Game variables
    private Thread game;
    private volatile boolean running = false;
    //Game Objects
    World world;
    
    public GamePanel(){
        world = new World();
        
        setPreferredSize(gameDim);
        setBackground(Color.WHITE);
        setFocusable(true);
        requestFocus();
        //Handle all key inputs from user
        addKeyListener(new KeyAdapter(){
            @Override
            public void keyPressed(KeyEvent e){
                
            }
            @Override
            public void keyReleased(KeyEvent e){
                
            }
            @Override
            public void keyTyped(KeyEvent e){
                
            }
        });
        
    }
    
    public void run(){
        while(running){
            gameUpdate();
            gameRender();
            paintScreen();
            
        }
    }
    
    private void gameUpdate(){
        if(running && game != null){
            
        }
    }
    
    private void gameRender(){
        if(dbImage == null){ // Create the buffer
            dbImage = createImage(GWIDTH, GHEIGHT);
            if(dbImage == null){
                System.err.println("dbImage is still null!");
                return;
            }else{
                dbg = dbImage.getGraphics();
            }
        }
        //Clear the screen
        dbg.setColor(Color.WHITE);
        dbg.fillRect(0, 0, GWIDTH, GHEIGHT);
        //Draw Game elements
        draw(dbg);
    }
    
    /* Draw all game content in this method */
    public void draw(Graphics g){
        world.draw(g);
    }
    
    private void paintScreen(){
        Graphics g;
        try{
            g = this.getGraphics();
            if(dbImage != null && g != null){
                g.drawImage(dbImage, 0, 0, null);
            }
            Toolkit.getDefaultToolkit().sync(); //For some operating systems
            g.dispose();
        }catch(Exception e){
            System.err.println(e);
        }
    }
    
    public void addNotify(){
        super.addNotify();
        startGame();
    }
    
    private void startGame(){
        if(game == null || !running){
            game = new Thread(this);
            game.start();
            running = true;
        }
    }
    
    public void stopGame(){
        if(running){
            running = false;
        }
    }
    }

and Main.java:

package game.test.src;

import javax.swing.JFrame;

public class Main extends JFrame
{
    GamePanel gp;
    
    public Main()
    {
        gp = new GamePanel();
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);
        add(gp);
    }
    

    
    public static void main(String[] args)
    {
        Main m = new Main();
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:53:43+00:00Added an answer on June 10, 2026 at 4:53 pm

    I have slightly modified your code, try

    import java.awt.*;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.*;
    
    public class World {
    
      private Rectangle[] blocks;
      private Image[] blockImg;
      private final int arrayNum = 500;
    
      //Block Images
      private Image BLOCK_GRASS, BLOCK_DIRT, BLOCK_STONE, BLOCK_SKY;
    
      private int x, y;
    
      public World(){
        BLOCK_GRASS = new ImageIcon(Frame1.class.getResource("images/openFile.gif")).getImage();
        BLOCK_DIRT = new ImageIcon(Frame1.class.getResource("images/openFile.gif")).getImage();
        BLOCK_STONE = new ImageIcon(Frame1.class.getResource("images/openFile.gif")).getImage();
        BLOCK_SKY = new ImageIcon(Frame1.class.getResource("images/openFile.gif")).getImage();
        blocks = new Rectangle[500];
        blockImg = new Image[500];
    
        loadArrays();
      }
    
      private void loadArrays()
      {
        for(int i = 0; i < arrayNum; i++)
        {
          if(x >= 500){
            x = 0;
            y += 20;
          }
          if(i >= 0 && i < 100)
          {
            blockImg[i] = BLOCK_SKY;
            blocks[i] = new Rectangle(x, y, 20, 20);
          }
          if(i >= 100 && i < 125)
          {
            blockImg[i] = BLOCK_GRASS;
            blocks[i] = new Rectangle(x, y, 20, 20);
          }
          if(i >= 125 && i < 225)
          {
            blockImg[i] = BLOCK_DIRT;
            blocks[i] = new Rectangle(x, y, 20, 20);
          }
          if(i >= 225 && i < 500)
          {
            blockImg[i] = BLOCK_STONE;
            blocks[i] = new Rectangle(x, y, 20, 20);
          }
          x += 20;
    
        }
    
      }
    
      public void draw(Graphics g)
      {
        for(int i = 0; i< arrayNum; i++){
          g.drawImage(blockImg[i], blocks[i].x, blocks[i].y, null);
        }
      }
    }
    
    class GamePanel extends JPanel implements Runnable {
      //Double buffering
      private Image dbImage;
      private Graphics dbg;
      //JPanel variables
      static final int GWIDTH = 500, GHEIGHT = 400;
      static final Dimension gameDim = new Dimension(GWIDTH, GHEIGHT);
      //Game variables
      private Thread game;
      private volatile boolean running = false;
      //Game Objects
      World world;
    
      public GamePanel(){
        world = new World();
    
        setPreferredSize(gameDim);
        setBackground(Color.WHITE);
        setFocusable(true);
        requestFocus();
        //Handle all key inputs from user
        addKeyListener(new KeyAdapter(){
          @Override
          public void keyPressed(KeyEvent e){
    
          }
          @Override
          public void keyReleased(KeyEvent e){
    
          }
          @Override
          public void keyTyped(KeyEvent e){
    
          }
        });
    
      }
    
      public void run(){
        while(running){
          gameUpdate();
    //      gameRender();
    //      paintScreen();
          repaint();
    
        }
      }
    
      private void gameUpdate(){
        if(running && game != null){
    
        }
      }
    
      private void gameRender(){
        if(dbImage == null){ // Create the buffer
          dbImage = createImage(GWIDTH, GHEIGHT);
          if(dbImage == null){
            System.err.println("dbImage is still null!");
            return;
          }else{
            dbg = dbImage.getGraphics();
          }
        }
        //Clear the screen
        dbg.setColor(Color.WHITE);
        dbg.fillRect(0, 0, GWIDTH, GHEIGHT);
        //Draw Game elements
        draw(dbg);
      }
    
      /* Draw all game content in this method */
      public void draw(Graphics g){
        world.draw(g);
      }
      public void paintComponent(Graphics g){
        super.paintComponent(g);
        gameRender();
        paintScreen(g);
      }
      private void paintScreen(Graphics g){
        try{
    //      g = this.getGraphics();
          if(dbImage != null && g != null){
            g.drawImage(dbImage, 0, 0, null);
          }
          Toolkit.getDefaultToolkit().sync(); //For some operating systems
    //      g.dispose();
        }catch(Exception e){
          System.err.println(e);
        }
      }
    
      public void addNotify(){
        super.addNotify();
        startGame();
      }
    
      private void startGame(){
        if(game == null || !running){
          game = new Thread(this);
          game.start();
          running = true;
        }
      }
    
      public void stopGame(){
        if(running){
          running = false;
        }
      }
    }
    class Main extends JFrame
    {
      GamePanel gp;
    
      public Main()
      {
        gp = new GamePanel();
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(gp);
        setResizable(false);
        setVisible(true);
      }
    
    
    
      public static void main(String[] args)
      {
        Main m = new Main();
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help with making a UIImageView float on the screen and when it
I need some help making a linq query that will select a list of
I need some help on 2 points below 1) How I can pull the
I need some help with an app i'm making using MapKit I'm struggling with
I am struggling getting my head around NSMutableArrays and need some help. I am
I need some help making this program for class. We are working with g++
Ok this is really making me crazy and I need some help. I have
I need some help setting a css button. My intention is having 3 images
I need some help regarding making a form using PHP, MySQL, and jQuery. Here
I am making NO headway in writing a regular expression and need some help.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.