2

I've been staring at this for days...

I think the Imageicon part is correct, and is referencing the right object. It has something to do with creating the board, map, and stuff, and I'm not really good with that.

public class Maze {
    public static final int SCREEN_SIZE = 1024;
    public static void main(String[] args){
        new Maze();
    }

    // help from: http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
    public Maze(){
        JFrame f = new JFrame();
        f.setTitle("Maze");
        f.add(new Board());
        f.setSize(SCREEN_SIZE,SCREEN_SIZE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

So you create the screen, make the board, get the images, and then paint depending on the contents of the array.

public class Board extends JPanel implements ActionListener {
    public static final int TILE_SIZE = 32;
    private Timer timer;
    public Map m;

    public Board(){
    m = new Map();
    timer = new Timer(25, this);
    timer.start();
}

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint(); // TODO Auto-generated method stub
    }

    public void paint(Graphics g){
        super.paint(g);
        for (int y = 0; y < TILE_SIZE; y++) {
            for (int x = 0; x < TILE_SIZE; x++) {
                if(m.Map[x][y] == 0) {
                      g.drawImage(m.getFloor(), x * 32, y * 32, null);
                }
                if (m.Map[x][y] == 1) {
                    g.drawImage(m.getWall(), x * 32, y * 32, null);
                }
            }
        }
    }
}

public class Map {
    public static Image floor, wall;
    public int Map[][] = new int[Maze.SCREEN_SIZE / Board.TILE_SIZE][Maze.SCREEN_SIZE / Board.TILE_SIZE];

    public Map() {
        ImageIcon img = new ImageIcon("C://Users/Brian2/Pictures/backgrounds/FLOOR.png");
        floor = img.getImage();
        img = new ImageIcon("C://Users/Brian2/Pictures/backgrounds/WALL");
        wall = img.getImage();
        GenMap();
    }

    public void GenMap() {
        for(int i = 0; i < Maze.SCREEN_SIZE / Board.TILE_SIZE; i++){
            for(int j = 0; j < Maze.SCREEN_SIZE / Board.TILE_SIZE; j++){
                Map[i][j] = 1;
            }
        }
    }

    public Image getWall() {
        return wall;
    }

    public Image getFloor() {
        return floor;
    }

    public void printMap() {}
}

It won't show anything on the JPanel that opens when I run it. I don't know what's wrong. There are no errors and I've been stuck for about 4 days.

Alex
  • 3,111
  • 6
  • 27
  • 43
  • 1
    For `JPanel`, override `paintComponent(Graphics)` rather than `paint(Graphics)`. 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson May 14 '14 at 04:40
  • I'm quite happy to tell you that it's an issue with your images. Try using `ImageIO.read` instead of `ImageIcon` as it will throw an `IOException` when the images can't be loaded. You should also be passing a none `null` value to `drawImage`'s `ImageObserver` parameter, in this case `this` will be more then suitable. – MadProgrammer May 14 '14 at 04:45

1 Answers1

1

I tried to reproduce your issue, but it worked for me. Depends which JDK you use, I used 1.6.0_45.

However I assume you want to paint a map of a "world".

I reorganized your code because I think you tried to do also unnecessary things, like keeping an integer array to model the world, but in fact you translate it to images.

The Board class:

package org.myself.mazetest;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Board extends JPanel implements ActionListener
{
   private static final int BOARD_SIZE = 1024;

   private static final int FRAME_VERTICAL_BORDERS = 40;

   private static final int FRAME_HORIZONTAL_BORDERS = 20;

   private static final int TILE_SIZE = 32;

   private static final int TIMER_DELAY = 25;

   private final Timer timer;

   private final int tileCount;

   public Map map;


   public Board( )
   {
      tileCount = Board.BOARD_SIZE / Board.TILE_SIZE;
      map = new Map( tileCount );
      timer = new Timer( Board.TIMER_DELAY, this );
      timer.start();
      setSize( Board.BOARD_SIZE, Board.BOARD_SIZE );
   }


   @Override
   public void actionPerformed( final ActionEvent e )
   {
      repaint(); // TODO Auto-generated method stub
   }


   @Override
   public void paintComponent( final Graphics g )
   {
      super.paintComponent( g );
      Image img;
      for ( int y = 0; y < tileCount; y++ )
      {
         for ( int x = 0; x < tileCount; x++ )
         {
            img = map.getImageFor( x, y );
            if ( img != null )
            {
               g.drawImage( img, x * Board.TILE_SIZE, y * Board.TILE_SIZE, null );
            }
         }
      }

   }


   public static void main( final String[] args )
   {
      final JFrame f = new JFrame();
      f.setTitle( "Maze" );
      f.setLayout( null );
      f.add( new Board() );
      f.setSize( Board.BOARD_SIZE + Board.FRAME_HORIZONTAL_BORDERS,
            Board.BOARD_SIZE + Board.FRAME_VERTICAL_BORDERS );
      f.setLocationRelativeTo( null );
      f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      f.setVisible( true );
   }
}

And the Map class:

package org.myself.mazetest;

import java.awt.Image;

import javax.swing.ImageIcon;

public class Map
{
   private final Image floor, wall;

   private final Image mapArray[][];

   private final int tileCount;


   public Map( final int tileCount )
   {
      mapArray = new Image[tileCount][tileCount];
      this.tileCount = tileCount;
      ImageIcon img = new ImageIcon( "C:\\Users\\Brian2\\Pictures\\backgrounds\\FLOOR.png" );
      floor = img.getImage();
      //TODO: sure without extension?
      img = new ImageIcon( "C:\\Users\\Brian2\\Pictures\\backgrounds\\WALL" ); 
      wall = img.getImage();
      //now build up the world's map :)
      genMap();
   }


   private void genMap()
   {
      //we build the inner area
      //that's why we start from 1, and go until tileCount - 1
      for ( int row = 1; row < (tileCount - 1); row++ )
      {
         for ( int col = 1; col < (tileCount - 1); col++ )
         {
            mapArray[row][col] = floor;
         }
      }

      //we build the wall itself
      final int firstRow = 0;
      final int lastRow = tileCount - 1;
      final int firstCol = 0;
      final int lastCol = tileCount - 1;
      for ( int col = 0; col < tileCount; col++ )
      {
         mapArray[firstRow][col] = wall;
         mapArray[lastRow][col] = wall;
      }
      for ( int row = 0; row < tileCount; row++ )
      {
         mapArray[row][firstCol] = wall;
         mapArray[row][lastCol] = wall;
      }

      //this also can be done as follows, but I don't want do make confusion
      /*
      for ( int colOrRow = 0; colOrRow < tileCount; colOrRow++ )
      {
         mapArray[firstRow][colOrRow] = wall;
         mapArray[lastRow][colOrRow] = wall;

         mapArray[colOrRow][firstCol] = wall;
         mapArray[colOrRow][lastCol] = wall;
      }
      */
   }


   public Image getImageFor( final int col, final int row )
   {
      return mapArray[row][col];
   }

}

Attached you find the output (with wall and floor images got from google). The output of the main method

Please check your wall image, in your example it is without extension... this can lead to problems in some image reading API-s.

The code from Maze class I moved to board because in fact it was just a frame creation, and I think is not needed to use a separate class for that.

I tried to give verbose names and comments, but feel free to come back to me if is unclear something.

rSzabi
  • 66
  • 2