#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Location
    Canada, EH!
    Posts
    9
    Rep Power
    0

    Trouble with Frame Background


    Hey Guys I'm back. Im making a java top-down 2-d game, and I need to set the background for the frame. Ill give you guys the classes you need to help.
    BackGround.java
    Code:
    package com.GitHub.Dtroll.DeathMatch;
    
    import java.awt.Image;
    
    import javax.swing.ImageIcon;
    
    public class BackGround {
    	private Image bg;
    	private int x;
    	private int y;
    	BackGround() {
    		ImageIcon ii = new ImageIcon(this.getClass().getResource(
    				"background.png"));
    		Image bg = ii.getImage();
    	}
    
    	public Image getImage() {
    
    		return bg;
    	}
    
    	public int getX() {
    		return x;
    	}
    
    	public int getY() {
    		return y;
    	}
    
    }
    Board.java
    Code:
    package com.GitHub.Dtroll.DeathMatch;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class Board extends JPanel implements ActionListener {
    
    	private Timer timer;
    	private Player player;
    	private ArrayList zombieenemy;
    	private boolean ingame;
    	private int B_WIDTH;
    	private int B_HEIGHT;
    	private Image image;
    	private BackGround bg;
    	private int[][] pos = { { 2380, 29 }, { 2500, 59 }, { 1380, 89 },
    			{ 780, 109 }, { 580, 139 }, { 680, 239 }, { 790, 259 },
    			{ 760, 50 }, { 790, 150 }, { 980, 209 }, { 560, 45 }, { 510, 70 },
    			{ 930, 159 }, { 590, 80 }, { 530, 60 }, { 940, 59 }, { 990, 30 },
    			{ 920, 200 }, { 900, 259 }, { 660, 50 }, { 540, 90 }, { 810, 220 },
    			{ 860, 20 }, { 740, 180 }, { 820, 128 }, { 490, 170 }, { 700, 30 } };
    
    	public Board() {
    		// /Death Match/src/com/GitHub/Dtroll/DeathMatch/background.png
    
    		addKeyListener(new TAdapter());
    		setFocusable(true);
    		setBackground(Color.BLACK);
    		setDoubleBuffered(true);
    		ingame = true;
    
    		setSize(256, 256);
    
    		player = new Player();
    
    		initZombie();
    
    		timer = new Timer(5, this);
    		timer.start();
    	}
    
    	public void addNotify() {
    		super.addNotify();
    
    	}
    
    	public void initZombie() {
    		zombieenemy = new ArrayList();
    
    		// for (int i = 0; i < pos.length; i++) {
    		// zombieenemy.add(new Zombie(pos[i][0], pos[i][1]));
    	}
    
    	// }
    
    	public void paint(Graphics g) {
    		super.paint(g);
    		
    		
    			Graphics2D g2 = (Graphics2D) g;
    			g2.drawImage(bg.getImage(), bg.getX(), bg.getY(), this);
    			
    		if (ingame) {
    
    			Graphics2D g2d = (Graphics2D) g;
    
    			// if (player.isVisible())
    			g2d.drawImage(player.getImage(), player.getX(), player.getY(), this);
    
    			ArrayList ms = player.getMissiles();
    
    			for (int i = 0; i < ms.size(); i++) {
    				Arrow m = (Arrow) ms.get(i);
    				g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
    			}
    
    			for (int i = 0; i < zombieenemy.size(); i++) {
    				Zombie a = (Zombie) zombieenemy.get(i);
    				if (a.isVisible())
    					g2d.drawImage(a.getImage(), a.getX(), a.getY(), this);
    			}
    
    			g2d.setColor(Color.WHITE);
    			g2d.drawString("Zombies left: " + zombieenemy.size(), 5, 15);
    
    		} else {
    			String msg = "Game Over";
    			Font small = new Font("Helvetica", Font.BOLD, 14);
    			FontMetrics metr = this.getFontMetrics(small);
    
    			g.setColor(Color.white);
    			g.setFont(small);
    			g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2,
    					B_HEIGHT / 2);
    		}
    
    		Toolkit.getDefaultToolkit().sync();
    		g.dispose();
    	}
    
    	public void actionPerformed(ActionEvent e) {
    
    		if (zombieenemy.size() == 0) {
    			ingame = true;
    		}
    
    		ArrayList ms = player.getMissiles();
    
    		for (int i = 0; i < ms.size(); i++) {
    			Arrow m = (Arrow) ms.get(i);
    			if (m.isVisible())
    				m.Move();
    			else
    				ms.remove(i);
    		}
    		for (int i = 0; i < zombieenemy.size(); i++) {
    			Zombie a = (Zombie) zombieenemy.get(i);
    			if (a.isVisible())
    				a.move();
    			else
    				zombieenemy.remove(i);
    		}
    
    		player.move();
    		checkCollisions();
    		repaint();
    	}
    
    	public void checkCollisions() {
    
    		Rectangle r3 = player.getBounds();
    
    		for (int j = 0; j < zombieenemy.size(); j++) {
    			Zombie a = (Zombie) zombieenemy.get(j);
    			Rectangle r2 = a.getBounds();
    
    			if (r3.intersects(r2)) {
    				player.setVisible(true);
    				a.setVisible(false);
    				ingame = true;
    			}
    		}
    
    		ArrayList ms = player.getMissiles();
    		for (int i = 0; i < ms.size(); i++) {
    			Arrow m = (Arrow) ms.get(i);
    
    			Rectangle r1 = m.getBounds();
    
    			for (int j = 0; j < zombieenemy.size(); j++) {
    				Zombie a = (Zombie) zombieenemy.get(j);
    				Rectangle r2 = a.getBounds();
    
    				if (r1.intersects(r2)) {
    					m.setVisible(false);
    					a.setVisible(false);
    				}
    			}
    		}
    	}
    
    	private class TAdapter extends KeyAdapter {
    
    		public void keyReleased(KeyEvent e) {
    			player.keyReleased(e);
    		}
    
    		public void keyPressed(KeyEvent e) {
    			player.keyPressed(e);
    		}
    	}
    }
    Skeleton.java
    Code:
    package com.GitHub.Dtroll.DeathMatch;
    
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    public class Skeleton extends JFrame {
    	/**
    	 * Needed!
    	 */
    	
    	
    	private static final long serialVersionUID = 1L;
    	public <ImagePanel> Skeleton() {
    		
    		
    		add(new Board());
    		setTitle("DeathMatch");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(256, 256);
            
            setLocationRelativeTo(null);
            setVisible(true);
            setResizable(true);
    	}
    	
    	
    	
    	public static void main(String[] args) {
            new Skeleton();
        }
    }
    When ever I run the code, the player moves around the screen, but my console gets filled with errors.

    Here is the error code:

    Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at com.GitHub.Dtroll.DeathMatch.Skeleton.paint(Skeleton.java:33)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:694)
    	at javax.swing.RepaintManager.access$700(RepaintManager.java:41)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1672)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:682)
    	at java.awt.EventQueue.access$000(EventQueue.java:85)
    	at java.awt.EventQueue$1.run(EventQueue.java:643)
    	at java.awt.EventQueue$1.run(EventQueue.java:641)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:652)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Please Help
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Location
    Canada, EH!
    Posts
    9
    Rep Power
    0
    Also if i do the drawImage function for the bg in Skeleton instead, the player can move around and such but I get the error and the background is black.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Location
    Canada, EH!
    Posts
    9
    Rep Power
    0
    anyone?
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    15
    Hi Dtroll,

    I suspect there is something up with the paint method in Board. You might want to comment out the content of that and see if that cures the problem, then gradually reintroduce sections to work out where the issue lies.

    Without Player, Arrow and Zombie classes, it is difficult to help further.

    slink
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.GitHub.Dtroll.DeathMatch.Skeleton.paint(Skeleton.java:33)
    Have you found what variable is null at line 33? Then find out why that variable has a null value by backtracking in the code to see why the value is null.

    The posted code does NOT show a paint() method in the Skeleton class????
    Last edited by NormR; November 8th, 2012 at 07:11 AM.

IMN logo majestic logo threadwatch logo seochat tools logo