Results 1 to 2 of 2
  1. #1
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default Image not showing up!!

    Hello, been awhile since I asked a question on here but I am still trying to learn this fun and sometimes frustrating language.

    But the code that I have was a part of a tutorial I found that involved making a screen, then drawing two rectangles on there and moving one of them around, I decided to make it so that I can input my own image and move it around instead (since I am interested in game programming) But the image isn't showing up, I have gotten the image I am trying to use to show up in other programs through following tutorials and stuff so I get the general idea of how they work but I can't figure this out. Please help, thanks ahead of time.

    *******Class with main method*******
    Java Code:
    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();
    	}
    	
    }
    ********Class controlling my screen and what is on it********
    Java Code:
    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();
    			}
    		}		
    	}
    }
    ********Class to control the player / entity*****
    Java Code:
    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
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I see you have a method called loadImages, but I don't see that you've called this method anywhere. Also, is this supposed to be an "active" graphics program? I'm not very familiar with this type of coding since most of the graphics programs I use use passive graphics, and in this situation, a while (true) with a Thread.sleep(...) will freeze my GUI.

Similar Threads

  1. Table not showing up
    By dilpreet28 in forum New To Java
    Replies: 3
    Last Post: 08-04-2010, 08:05 AM
  2. showing image in web application
    By Juuno in forum Advanced Java
    Replies: 3
    Last Post: 05-11-2009, 05:36 AM
  3. Showing
    By bostonstate in forum New To Java
    Replies: 3
    Last Post: 08-25-2008, 08:49 PM
  4. Replies: 0
    Last Post: 01-08-2008, 06:29 PM
  5. Why isn't this showing?
    By JToolTip in forum Java Applets
    Replies: 2
    Last Post: 07-08-2007, 12:54 AM

Posting Permissions

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