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 3 of 3

Thread: Help me with My Game.

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me with My Game.

    I am making a tower defense game. I followed a tutorial on how to make a game, but it was full screen. After making some of the game, I wanted to make it windowed. Now, I have a grey window that shows nothing. I think it is a problem with how I made the Graphics object:

    Errors:

    Exception in thread "main" java.lang.NullPointerException
    at game.Main.movieLoop(Main.java:79)
    at game.Main.run(Main.java:64)
    at game.Main.<init>(Main.java:45)
    at game.Main.main(Main.java:31)

    package game;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.util.concurrent.CopyOnWriteArrayList;
    import javax.swing.*;
     
    public class Main extends JFrame {
     
    	private static final long serialVersionUID = 1L;
     
    	public Input listener;
    	private boolean running;
    	private Image green;
     
    	private CopyOnWriteArrayList<Tower1> Tower1List1;
     
    	private Image tower1Image;
    	private Item Tower1Icon;
    	private Image sidebar;
     
    	private int icon1Clicked = 0;
    	private int mousePressed = 0;
    	private int mouseUsed = 0;
    	private int drawTower1 = 0;
    	private int money = 100;
     
    	private Point mouse;
     
    	public static void main(String[] args){
    		new Main();
    	}
     
    	public Main(){
     
    		JFrame f = new JFrame();
    		f.setTitle("GAME!!!");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setSize(1024,768);
    		f.setResizable(false);
    		f.setVisible(true);
    		f.createBufferStrategy(2);
    		f.setBackground(Color.GREEN);
     
    		run();
     
    	}
     
    	public void loadVariables(){
     
    		mouse = new Point();
    		Tower1List1 = new CopyOnWriteArrayList<Tower1>();
    		tower1Image = new ImageIcon("C:\\test\\redGun.png").getImage();
    		Tower1Icon = new Item(tower1Image, getWidth()-240,70);
    		sidebar = new ImageIcon("C:\\test\\sidebar.png").getImage();
    		green = new ImageIcon("C:\\test\\greenB.jpg").getImage();		
     
    	}
     
    	public void run(){
    			running = true;
    			loadVariables();
    			listener = new Input();
    			movieLoop();
    	}
     
    	public void movieLoop(){
    		long startingTime = System.currentTimeMillis();
    		long cumTime = startingTime;
     
    		while(running){
     
    			long timePassed = System.currentTimeMillis() - cumTime;
    			cumTime += timePassed;
    			update(timePassed);
    			BufferStrategy s = getBufferStrategy();
    LINE 79 -->>	        Graphics2D g =  (Graphics2D) s.getDrawGraphics();
    			draw(g);
    			s.show();
    			g.dispose();
     
    			try{
    				Thread.sleep(20);
    			}catch(Exception e){}
    		}	
    	}
     
    	public void draw(Graphics g){
    		setFont(new Font("Arial", Font.PLAIN, 20));		
     
    		g.drawImage(green,0,0,null);		
    		g.setColor(Color.WHITE);
    		g.drawImage(sidebar, getWidth()-256,0, null);	
    		g.drawString("$" + Integer.toString(money),  getWidth()-200, 45);
    		g.drawString("$50", getWidth()-237,140);
     
    		g.drawImage(Tower1Icon.getImage(), Tower1Icon.getX(), Tower1Icon.getY(),null);
     
    		for (Tower1 tower1 : Tower1List1){
    			g.drawImage(tower1.getI(),tower1.getX()-25,tower1.getY()-25,null);
    		}
     
    		if(icon1Clicked == 1){
    			g.drawImage(Tower1Icon.getImage(),mouse.x-Tower1Icon.getWidth()/2,mouse.y-Tower1Icon.getHeight()/2,null);			
    		}
    	}		
     
    	//update position
    	public void update(long timePassed){
     
    		if(drawTower1 == 1){
    			Tower1 tower1 = new Tower1(mouse.x,mouse.y);
    			Tower1List1.add(tower1);
    			money -= 50;
    			drawTower1 = 0;
    		}
     
    			Rectangle rect1 = new Rectangle(Tower1Icon.getX(), Tower1Icon.getY(),Tower1Icon.getHeight(),Tower1Icon.getWidth());
     
     
    			if(mousePressed == 1 && mouseUsed == 0 ){
    				if( rect1.contains(mouse.x,mouse.y)){
    						if(!(money < 50)){
    							icon1Clicked = 1;
    							mouseUsed = 1;
    						}
    					}	
    			}else{
    				icon1Clicked = 0;			
    		}
    	}
     
    	//user input
    	public class Input implements KeyListener, MouseMotionListener, MouseListener, MouseWheelListener{
    		Input(){	
    			setFocusTraversalKeysEnabled(false);
    			addKeyListener(this);
    			addMouseMotionListener(this);
    			addMouseListener(this);
    			addMouseWheelListener(this);
    		}
     
    		//mouse click listener
    		public void mousePressed(MouseEvent e){
    			mousePressed = 1;
    		}
     
    		//unused mouse functions
    		public void mouseReleased(MouseEvent e){
    			mousePressed = 0;
     
    			if(mouseUsed == 1){
    				drawTower1 = 1;
    				mouseUsed = 0;
    			}
    		}
     
    		//record mouse position
    		public void mouseDragged(MouseEvent e){
    				mouseMoved(e);
    		}
    		public void mouseMoved(MouseEvent e){
    			mouse.x = e.getX();
    			mouse.y = e.getY();
    		}
     
    		//unused input functions
    		public void keyPressed(KeyEvent e){e.consume();}
    		public void keyReleased(KeyEvent e){e.consume();}		
    		public void keyTyped(KeyEvent e){e.consume();}
    		public void mouseClicked(MouseEvent e){e.consume();}
    		public void mouseEntered(MouseEvent e){e.consume();}
    		public void mouseExited(MouseEvent e){e.consume();}
    		public void mouseWheelMoved(MouseWheelEvent e){
    			money++;
    			e.consume();}	
    	} 
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: Help me with My Game.

    A few tips:

    1) If you want to do animation, I recommend reading a bit on threads and Swing (for example, sett Threads and Swing ). Swing is a single threaded model, and all drawing or updates to the GUI should be done on that single thread (the EDT - Event Dispatch Thread). Using a Swing timer if you want animation.

    2) see Trail: 2D Graphics (The Java™ Tutorials) To paint to a component, do so by overriding the paintComponent method

    3) Read the API's (for example, a quote from the getBufferedStrategy
    Returns the BufferStrategy used by this component. This method will return null if a BufferStrategy has not yet been created or has been disposed.
    - quite probably the source of your exception - and another example: avoid using method names that may conflict with those defined in the API - eg update)

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with My Game.

    Quote Originally Posted by copeg View Post
    A few tips:

    1) If you want to do animation, I recommend reading a bit on threads and Swing (for example, sett Threads and Swing ). Swing is a single threaded model, and all drawing or updates to the GUI should be done on that single thread (the EDT - Event Dispatch Thread). Using a Swing timer if you want animation.

    2) see Trail: 2D Graphics (The Java™ Tutorials) To paint to a component, do so by overriding the paintComponent method

    3) Read the API's (for example, a quote from the getBufferedStrategy

    - quite probably the source of your exception - and another example: avoid using method names that may conflict with those defined in the API - eg update)
    Thanks for the tips. The thing is, I made the buffer strategy in the Main constructor.

Similar Threads

  1. Game !
    By Ahmed.Ibrahem in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2011, 07:01 PM
  2. DOS board game
    By SageNTitled in forum Java Theory & Questions
    Replies: 5
    Last Post: March 4th, 2010, 02:53 PM
  3. Game 3x3
    By Koren3 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 07:43 PM
  4. Job offers to program Hobo Wars
    By MooncakeZ in forum Paid Java Projects
    Replies: 7
    Last Post: September 17th, 2009, 09:41 PM
  5. [SOLVED] Fixing of bug for Pong game
    By phoenix in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 14th, 2009, 01:19 PM