Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default Need help posting Image into JFrame in Shooter game

    Hey, Im kinda new to these forums but im desperate to figure out how to post images into JFrame from the shooter game I am working on. Ive spend at least 5 hours trying to figure out and nothing came out of it =/ And no I will not resort to using JLabels >.<!!! I really need this done by tonight because thats the only problem I have with this program, I will appreciate your help and the time you took to read through my coding ^-^.

    Anyways here is the code

    Shooter class:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    
    public class Shooter extends JFrame implements KeyListener
    {
    	Image image;
    	Graphics graph;
    	
    	boolean up, down, wKey, sKey;
    	int shoot,enter;
    	
    	Player p1 = new Player(5,150,10,50,Color.RED,"Images/P1.gif"); // <-- This is were I set my image path towards 
    //the Player class so it can be converted into a Image object. Though it does not work when you run it =/
    	Player p2 = new Player(585,150,10,50,Color.BLUE,"Images/P2.gif"); //<--- Same with this
    	ArrayList<Bullets> bull = new ArrayList<Bullets>();
    	
    	public Shooter()
    	{
    		setTitle("X-Shooter");
    		setSize(600,500);
    		setResizable(false);
    		setBackground(Color.BLACK);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		addKeyListener (this);
    		up = false;
    		down = false;
    		wKey = false;
    		sKey = false;
    		shoot = 0;
    		enter = 0;
    		
    		
    		setVisible(true);	
    	}
    	public void paint(Graphics graphics)
    	{
    		image = createImage(getWidth(), getHeight());
    		graph = image.getGraphics();
    		
    		paintComponent(graph);
    		graphics.drawImage(image,0,0,this);
    		repaint();
    	}
    	public void paintComponent(Graphics graphics)
    	{
    		graphics.setColor(Color.GREEN);
    		graphics.drawString("Player 1 Health: " + p1.health, 30, 460);
    		graphics.drawString("Player 2 Health: " + p2.health, 460,460);
    		if(p1.health > 0 && p2.health > 0)
    		{
    			for(Bullets b1:bull)
    			{
    				b1.draw(graphics);
    			}
    			update();
    		}
    		else
    		{
    			if(p1.health <= 0)
    			{
    				graphics.setColor(p2.colour);
    				graphics.drawString("Player 2 Wins!", 250, 200);
    			}
    			else
    			{
    				graphics.setColor(p1.colour);
    				graphics.drawString("Player 1 Wins!", 250, 200);
    			}
    		}
    		p1.draw(graphics);//<---This is where I draw my image (Well at least 
    //I think it draws the image but it doesn't work =s)
    		p2.draw(graphics);//<--- Same with this
    	}
    	public void update()
    	{
    		if(wKey && p1.y > 24)
    		{
    			p1.moveUp();
    		}
    		if(sKey && p1.y < 347)
    		{
    			p1.moveDown();
    		}
    		if(up && p2.y > 24)
    		{
    			p2.moveUp();
    		}
    		if(down && p2.y < 347)
    		{
    			p2.moveDown();
    		}
    		if(enter == 1)
    		{
    			Bullets add = p2.getAmmunition();
    			add.xVel = -3;
    			bull.add(add);
    			enter++;
    		}
    		if(shoot == 1)
    		{
    			Bullets add = p1.getAmmunition();
    			add.xVel = 3;
    			bull.add(add);
    			shoot++;
    		}
    		for(int x = 0; x < bull.size(); x++)
    		{
    			bull.get(x).move();
    			if(bull.get(x).shape.intersects(p1.shape) && bull.get(x).xVel < 0)
    			{
    				p1.health --;
    				bull.remove(x);
    				x--;
    				continue;
    			}
    			if(bull.get(x).shape.intersects(p2.shape) && bull.get(x).xVel > 0)
    			{
    				p2.health --;
    				bull.remove(x);
    				x--;
    				continue;
    			}
    		}
    	}
    	public void keyTyped(KeyEvent e){
    	}
    	public void keyPressed(KeyEvent e)
    	{
    	  switch(e.getKeyCode())
    	  {
    		case KeyEvent.VK_UP:up=true;break;
    		case KeyEvent.VK_DOWN:down=true;break;
    		case KeyEvent.VK_W:wKey=true;break;
    		case KeyEvent.VK_S:sKey=true;break;
    		
    		case KeyEvent.VK_SPACE:shoot++;break;
    		case KeyEvent.VK_ENTER:enter++;break;
    	  }
    	}
    	public void keyReleased(KeyEvent e)
    	{
    		switch(e.getKeyCode())
    		{	
    			case KeyEvent.VK_UP:up=false;
    			case KeyEvent.VK_DOWN:down=false;
    			case KeyEvent.VK_W:wKey=false;
    			case KeyEvent.VK_S:sKey=false;
    		
    			case KeyEvent.VK_SPACE:shoot=0;break;
    			case KeyEvent.VK_ENTER:enter=0;break;
    		}
    	}
    		public static void main(String[] args)
    	{
    		KeyListener shoot = new Shooter();
    	}
    }
    Player Class
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    
    public class Player
    {
    	int health;
    	int x,y;
    	int height,width;
    	Rectangle shape;
    	Image img;
    	Color colour;
    	
    	
    	public Player()
    	{
    		x = 0;
    		y = 0;
    		height = 0;
    		width = 0;
    		health = 10;
    		img = null; // <-- No img is contained in this variable yet
    		colour = Color.WHITE;
    		shape = new Rectangle(x,y,width,height);
    	}
    	public Player(int x, int y)
    	{
    		this.x=x;
    		this.y=y;
    		height = 0;
    		width = 0;
    		health = 10;
    		img = null; //<-- Same with this variable
    		colour = Color.WHITE;
    		shape = new Rectangle(x,y,width, height);
    	}
    	public Player(int x, int y, int wide,int high)
    	{
    		this.x=x;
    		this.y=y;
    		height = high;
    		width = wide;
    		health = 10;
    		img = null; //<--Same with this variable
    		colour = Color.WHITE;
    		shape = new Rectangle(x,y,width, height);
    	}
    	public Player(int x, int y, int wide,int high, String s)
    	{
    		this.x=x;
    		this.y=y;
    		height = high;
    		width = wide;
    		img = Toolkit.getDefaultToolkit().getImage(s); //<--- The image path is sent from the Shooter class and it does not go through to print for some reason =S
    		health = 10;
    		
    		colour = Color.WHITE;
    		shape = new Rectangle(x,y,width, height);
    	}
    	public Player(int x, int y, int wide,int high, Color c, String s)
    	{
    		this.x=x;
    		this.y=y;
    		height = high;
    		width = wide;
    		img = Toolkit.getDefaultToolkit().getImage(s); //<--- Same with this
    		health = 10;
    		
    		colour = c;
    		shape = new Rectangle(x,y,width, height);
    	}
    	public void draw(Graphics g)
    	{
    		g.drawImage(img,x,y,null);
    	}
    	public void setImage(String s)
    	{
    		img = Toolkit.getDefaultToolkit().getImage(s);//<---This sets turns the image path 
    //into the required image
    	}
    	public void moveUp()
    	{
    		y-=2;
    		shape.setLocation(x,y);
    	}
    	public void moveDown()
    	{
    		y+=2;
    		shape.setLocation(x,y);
    	}
    	public Bullets getAmmunition()
    	{
    		Bullets b = new Bullets(x+8, y+23, 3, 3, colour);
    		return b;
    	}
    }
    I dont know if you need the bullet class nor do I think it affects the image transfer through class in anyway but ill post it to make a clearer view of the shooter program

    Bullets Class
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    
    public class Bullets
    {
    	int x,y;
    	int xVel;
    	int height, width;
    	Rectangle shape;
    	Color colour;
    	
    	public Bullets()
    	{
    		x = 0;
    		y = 0;
    		height = 0;
    		width = 0;
    		colour = Color.WHITE;
    		shape = new Rectangle(x,y,width,height);
    	}
    	public Bullets(int x, int y, int wide, int high, Color c)
    	{
    		this.x = x;
    		this.y = y;
    		this.xVel = 0;
    		height = high;
    		width = wide;
    		colour = c;
    		shape = new Rectangle(x,y,width, height);
    	}
    	public Bullets(int x, int y, int wide, int high, Color c, int xVel)
    	{
    		this.xVel = xVel;
    		this.x = x;
    		this.y = y;
    		this.xVel = 0;
    		height = high;
    		width = wide;
    		colour = c;
    		shape = new Rectangle(x,y,width, height);
    	}
    	public void draw(Graphics graphics)
    	{
    		graphics.setColor(colour);
    		graphics.fillOval(x,y,width,height);
    	}
    	public void move()
    	{
    		x+= xVel;
    		shape.setLocation(x,y);
    	}
    }

    The program is executable if you copy and paste, its just that no images will show up =/. I photoshoped 2 images in my "Images" file but they wont upload onto the JPanel T.T Please Help this desperate young man in his time of need programmers of the Java Forums!!!!!
    Last edited by Noctis Lucis Caelum; 01-24-2011 at 10:52 PM.

  2. #2
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    And I just figured out the solution >.> The image path wasn't correctly formatted..... Sorry for wasting your time =/

Similar Threads

  1. 2D Shooter Game
    By just_in_deed in forum Java 2D
    Replies: 1
    Last Post: 08-04-2010, 01:53 AM
  2. Add an image to JFrame
    By Eranga in forum AWT / Swing
    Replies: 4
    Last Post: 02-01-2010, 03:09 PM
  3. JFrame + image
    By Blacknight in forum New To Java
    Replies: 2
    Last Post: 05-07-2009, 05:34 AM
  4. Trying to create a map for a Zuul game using JFrame
    By alpdog14 in forum New To Java
    Replies: 1
    Last Post: 04-18-2009, 12:24 AM
  5. can display image in JFrame?
    By xCLARAx in forum AWT / Swing
    Replies: 14
    Last Post: 04-03-2009, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •