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

Thread: Can't figure out why my image isn't showing up!!

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't figure out why my image isn't showing up!!

    This program is actually from a tutorial I found and I tried to change it around and make it into my own. Basically the tutorial had the user create a box and be able to move it around the screen. I am simply trying to load an image and move it around the screen but for some reason Java doesn't want me to. (It's very mean like that sometimes)

    The code runs and all and no errors come up, but that is all I get is the black background I set and no image I am trying to load.

    *****This is the Class with the main method on it, not much going on in here*********
     
    package captain;
     
    import javax.swing.*;
     
    public class MainGame  {
     
    	JFrame window;
    	Screen panel;
     
    	public MainGame(){
    		window = new JFrame("Collistion detection, movement, double buffering, and a game loop!");
    		panel = new Screen();
    		window.setSize(800,600);
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.getContentPane().add(panel);
    		window.setVisible(true);		
    	}
     
    	public void go(){
    		panel.startGame();
    	}
     
    	public static void main(String[] args){
    		MainGame game = new MainGame();
    		game.go();
    	}
     
    }

    ********This is the class that I am actually trying to draw my images on the screen with*********
     
    package captain;
     
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.*;
    import java.awt.image.*;
    import javax.swing.*;
     
    public class Screen extends JPanel implements KeyListener{
     
    	BufferedImage buffer;
    	Entity player;
    	Entity enemy;
    	Image myImage;
    	Image playerImage;
    	Image enemyImage;
     
    	public Screen(){
    		setIgnoreRepaint(true);
    		addKeyListener(this);
    		setFocusable(true);
    	}
     
    	public void loadImages(){
    		myImage = new ImageIcon("C:\\Users\\Thrash\\Desktop\\practice\\CaptainStandRight.jpg").getImage();
    	}
     
    	public void keyTyped(KeyEvent e){		
    	}
     
    	public void keyPressed(KeyEvent e){		
    		int key = e.getKeyCode();
     
    		if(key ==KeyEvent.VK_LEFT)
    			player.left = true;
    		if(key == KeyEvent.VK_RIGHT)
    			player.right = true;
    		if(key == KeyEvent.VK_UP)
    			player.up = true;
    		if(key == KeyEvent.VK_DOWN)
    			player.down = true;		
    	}
     
    	public void keyReleased(KeyEvent e){		
    		int key = e.getKeyCode();
     
    		if(key ==KeyEvent.VK_LEFT)
    			player.left = false;
    		if(key == KeyEvent.VK_RIGHT)
    			player.right = false;
    		if(key == KeyEvent.VK_UP)
    			player.up = false;
    		if(key == KeyEvent.VK_DOWN)
    			player.down = false;			
    	}
     
    	public void Initialize(){
    		buffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
    		player = new Entity(myImage, 100,100);		
    	}
     
    	public void update(){		
    		player.move();		
    	}
     
    	public void drawBuffer(){		
    		Graphics2D b = buffer.createGraphics();
    		b.setColor(Color.BLACK);
    		b.fillRect(0, 0, 800, 600);
    		b.drawImage(player.getImage(),100,100,null);
    		b.dispose();		
    	}
     
    	public void drawScreen(){		
    		Graphics2D g = (Graphics2D)this.getGraphics();
    		g.drawImage(buffer,0,0,this);
    		Toolkit.getDefaultToolkit().sync();
    		g.dispose();		
    	}
     
    	public void startGame(){
    		Initialize();
    		while(true){
    			try{
    				update();
    				drawBuffer();
    				drawScreen();
    				Thread.sleep(15);
    			}
    			catch(Exception e){
    				e.printStackTrace();
    			}
    		}		
    	}
    }
    ********And this is the class that controls my player / entity************
    package captain;
     
    import java.awt.Image;
     
    public class Entity {
     
    	int x,y,speed;
    	boolean up,down,left,right,collision;
    	Image image;
     
    	public Entity(Image image, int x, int y){
    		this.image = image;
    		this.x = x;
    		this.y = y;
    		speed = 3;
    		up = down = left = right = collision = true;
    	}
     
    	public int getX(){
    		return x;
    	}
     
    	public int getY(){
    		return y;
    	}
     
    	public Image getImage(){
    		return image;
    	}
     
    	public int getWidth(){
    		return image.getWidth(null);
    	}
     
    	public int getHeight(){
    		return image.getHeight(null);
    	}
     
    	public void move(){
    		if (up)
    			y -= speed;
    		if(down)
    			y += speed;
    		if(left)
    			x -= speed;
    		if(right)
    			x += speed;		
    	}
     
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't figure out why my image isn't showing up!!

    It could be because you never put the image in the same directory as your java code.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't figure out why my image isn't showing up!!

    Not familiar with image drawing, but what is this code doing:

    b.drawImage(player.getImage(),100,100,null);
    b.dispose();

    What are you disposing?

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,100
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Can't figure out why my image isn't showing up!!

    I would recommend overriding the paintComponent method of the JPanel as opposed to creating your own while loop and attempting any custom painting by retrieving the Graphics of the JPanel. If you need to fire any animations, use a SwingTimer to call repaint()

  5. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (November 8th, 2010)

Similar Threads

  1. showMessageDialog is not showing!
    By jonathan920 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 27th, 2010, 04:14 AM
  2. showing page
    By ighor10 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 26th, 2010, 08:52 AM
  3. Showing database results
    By randyrr in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 27th, 2010, 11:26 PM
  4. Showing a picture in a GUI
    By joachim89 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 15th, 2010, 01:42 PM
  5. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 09:50 AM