Hey there.
I am trying to practice my Java and work on this basic game I'm creating.
I am at the stage where I am trying to draw the tiled map, but I'm having two problems. The first problem is a logical one, I am trying to create a for loop that will draw the below image, and assign every 20x20 square a rectangle. I am having problems logically accomplishing this. The second problem is, I started to try a way to do it, but I get this error:
Exception in thread "Thread-2" java.lang.NullPointerException
at bandits.World.draw(World.java:43)
at bandits.GamePanel.draw(GamePanel.java:101)
at bandits.GamePanel.gameRender(GamePanel.java:96)
at bandits.GamePanel.run(GamePanel.java:48)
at java.lang.Thread.run(Thread.java:722)
Tile.jpg
Here is the code for my 3 classes:
package bandits; import javax.swing.JFrame; public class Bandits extends JFrame { GamePanel gp; public Bandits(){ gp = new GamePanel(); setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setResizable(false); add(gp); } public static void main(String[] args){ Bandits b = new Bandits(); } }package bandits; import javax.swing.JPanel; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; 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, HEIGHT); //Game Variables private Thread game; private volatile boolean running = false; World world; public GamePanel(){ world = new World(); setPreferredSize(gameDim); setFocusable(true); requestFocus(); addKeyListener(new KeyAdapter(){ @Override public void keyPressed(KeyEvent e){ } @Override public void keyReleased(KeyEvent e){ } }); } @Override public void run(){ while(running){ gameUpdate(); gameRender(); paintScreen(); } } @Override 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; } } private void gameUpdate() { if(running && game !=null){ } } private void gameRender() { if(dbImage == null){ dbImage = createImage(GWIDTH, GHEIGHT); if(dbImage == null){ System.err.println("DBImage is still null!"); return; }else{ dbg = dbImage.getGraphics(); } } dbg.setColor(Color.WHITE); dbg.fillRect(0, 0, getWidth(), getHeight()); draw(dbg); } /* Everything that is graphics gets drawn here*/ 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(); g.dispose(); }catch(Exception e){ System.err.println(e); } } }package bandits; import java.awt.*; import javax.swing.ImageIcon; public class World { private Rectangle[] blocks; private Image[] blockImg; private final int arrayNum = 625; private Image BLOCK_FLOOR, BLOCK_WALL; private int x= 0; private int y= 0; public World(){ BLOCK_FLOOR = new ImageIcon("C:/Users/Thomas/Documents/NetBeansProjects/Images/Floor.gif").getImage(); BLOCK_WALL = new ImageIcon("C:/Users/Thomas/Documents/NetBeansProjects/Images/Wall.gif").getImage(); blocks = new Rectangle[625]; blockImg = new Image[625]; loadArrays(); } private void loadArrays(){ for(int i = 0; i < arrayNum; i++){ if(x > 500){ x = 0; y += 20; } if(x <= 20){ blockImg[i] = BLOCK_WALL; 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); } } }
Any help would be appreciated, this is not a homework assignment, just trying to refine, practice, and improve my Java skills.