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

Thread: Why won't this image display?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Why won't this image display?

    ok so I am making a copy of the famous game Galaga (or trying to) and I want to replace the rectangle I put as your ship to a image that I made in photshop. Everything in this program works except the iamge I a made does not display itself. Here is the code I had before that has absoluteyly nothing wrong with it. The ship is just a rectangle and everything works fine and there are no problems.
    package galaga;
     
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.image.ImageObserver;
    import java.io.File;
     
    import javax.sound.midi.Soundbank;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
     
    public class mainmethod extends JFrame {
    	int x = 200, y = 325;
    	KL bob = new KL();
    	int xShoot = (int) (x + 37.5);
    	int yShoot = y;
    	boolean READY = false;
    	boolean enemy1hit = false;
    	boolean BulletShown = false;
    	JButton Start= new JButton("Start Game!");
     
    	boolean Gamestarted = false;
     
     
    	ImageIcon Spaceship = new ImageIcon("CRAPPYSPACESHIP.png");
    	Image Ship = Spaceship.getImage();
    	ImageObserver bo;
    	//Stuff for the JFrame (
    	public mainmethod() {
    		super("Galaga");
    		setVisible(true);
    		setLocation(150, 150);
    		setSize(400, 400);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		addKeyListener(bob);
    		setBackground(Color.white);
    		setLayout(new FlowLayout());
     
    	}
    // )
    // Key Listener (
    	public class KL implements KeyListener {
     
    		public void keyPressed(KeyEvent e) {
    			// Move Left (
    			if (e.getKeyCode() == KeyEvent.VK_LEFT) {
     
    				System.out.println("\nStarting with " + x);
    				System.out.println("x is now " + x);
    					System.out.println(" ");
    					x = x - 5;
    					xShoot=(int) (x+37.5);
     
    					System.out.println("Bullet is at: x: " + xShoot + " y: " + yShoot);
    					// )
    					//Collision
    				if (x <= 10) {
    					x = 10;
    					System.out
    						.println("Collision detected with left side of window!");
     
    				}
    			}
    			//move right
    			if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
     
    				System.out.println("\nStarting with " + x);
    				System.out.println("x is now " + x);
    					x = x + 5;
    					System.out.println(" ");
    					xShoot=(int) (x+37.5);
     
    					System.out.println("Bullet is at: x: " + xShoot + " y: " + yShoot );
    				}
    				// Collision again
    				if (x >= 300) {
    					x = 300;
    					System.out
    						.println("Collision detected with right side of window!");
     
     
    			}
     
    			repaint();
    			//Shoot
    			if (e.getKeyCode() == KeyEvent.VK_SPACE) {
    				READY = true;
    				System.out.println("FIRE! \n");
    				BulletShown=true;
    			}
    		}
     
     
     
    		//Stop shooting
    		public void keyReleased(KeyEvent e) {
    			if (e.getKeyCode() == KeyEvent.VK_SPACE) {
    				READY = false;
    				System.out.println("Not READY TO FIRE! \n");
    				if(READY=true) {
    					yShoot = yShoot -5;
    					repaint();
    				}
    			}
    		}
    		//ignore this
    		@Override
    		public void keyTyped(KeyEvent e) {
     
    		}
    	}
     
     
     
     
    	public void paint(Graphics g) {
    		//I don't even know what this means, someone said it just doublebuffers the program
    		Image dbImage = createImage(getWidth(), getHeight());
    		Graphics dbGraphics = dbImage.getGraphics();
    		paintComponent(dbGraphics);
    		g.drawImage(dbImage, 0, 0, this);
    		repaint();
     
    	}
    // This is where I paint the ship, the bullet, and the enemy to the screen
    	public void paintComponent(Graphics g) {
    		g.setColor(Color.CYAN);
     
    		Rectangle blarp = new Rectangle(x, y, 150, 150);
     
    		// This is where I draw the ship(
    		g.fillRect((int) blarp.getX(), (int) blarp.getY(), 90, 50);
    		// )
    		if (yShoot<4) {
    			yShoot=(int) blarp.getY();
    			xShoot=(int) ((int) blarp.getX() + 37.5);
    			repaint();
    			BulletShown=false;
    		}
    		//Bullet Stuff
    		g.setColor(Color.RED);
    		Rectangle Bullet = new Rectangle(xShoot, yShoot, 10, 20);
     
    		if(BulletShown==true){
     
    			g.fillRect((int) Bullet.getX(), (int) Bullet.getY(), 20, 30);
    			repaint();}
     
     
     
     
    		if(yShoot<323) {
    			yShoot = yShoot - 5;
    			repaint();
    		}	
    		// )
    		// Enemy Stuff (
    		if(enemy1hit==false) {
    			g.fillOval(50, 50, 10, 10);
    			repaint();
    		}
    		if(Bullet.getX()<=57 && Bullet.getX()>=42 && Bullet.getY()<=60 && Bullet.getY()>=40) {
    			System.out.println("ENEMY1 Hit!");
    			enemy1hit= true;
     
    		// )
    		}
     
    	}
    }

    There is nothing wrong with this code. This is just so you can compile and run this code to see how the program works. This upcoming code is where I replaced the fillrect() of the ship to drawImage() and I ahve no idea what I am doing wrong.

    package galaga;
     
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.image.ImageObserver;
    import java.io.File;
     
    import javax.sound.midi.Soundbank;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
     
    public class mainmethod extends JFrame {
    	int x = 200, y = 325;
    	KL bob = new KL();
    	int xShoot = (int) (x + 37.5);
    	int yShoot = y;
    	boolean READY = false;
    	boolean enemy1hit = false;
    	boolean BulletShown = false;
    	JButton Start= new JButton("Start Game!");
     
    	boolean Gamestarted = false;
     
     
    	ImageIcon Spaceship = new ImageIcon("CRAPPYSPACESHIP.png");
    	Image Ship = Spaceship.getImage();
    	ImageObserver bo;
    	//Stuff for the JFrame (
    	public mainmethod() {
    		super("Galaga");
    		setVisible(true);
    		setLocation(150, 150);
    		setSize(400, 400);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		addKeyListener(bob);
    		setBackground(Color.white);
    		setLayout(new FlowLayout());
     
    	}
    // )
    // Key Listener (
    	public class KL implements KeyListener {
     
    		public void keyPressed(KeyEvent e) {
    			// Move Left (
    			if (e.getKeyCode() == KeyEvent.VK_LEFT) {
     
    				System.out.println("\nStarting with " + x);
    				System.out.println("x is now " + x);
    					System.out.println(" ");
    					x = x - 5;
    					xShoot=(int) (x+37.5);
     
    					System.out.println("Bullet is at: x: " + xShoot + " y: " + yShoot);
    					// )
    					//Collision
    				if (x <= 10) {
    					x = 10;
    					System.out
    						.println("Collision detected with left side of window!");
     
    				}
    			}
    			//move right
    			if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
     
    				System.out.println("\nStarting with " + x);
    				System.out.println("x is now " + x);
    					x = x + 5;
    					System.out.println(" ");
    					xShoot=(int) (x+37.5);
     
    					System.out.println("Bullet is at: x: " + xShoot + " y: " + yShoot );
    				}
    				// Collision again
    				if (x >= 300) {
    					x = 300;
    					System.out
    						.println("Collision detected with right side of window!");
     
     
    			}
     
    			repaint();
    			//Shoot
    			if (e.getKeyCode() == KeyEvent.VK_SPACE) {
    				READY = true;
    				System.out.println("FIRE! \n");
    				BulletShown=true;
    			}
    		}
     
     
     
    		//Stop shooting
    		public void keyReleased(KeyEvent e) {
    			if (e.getKeyCode() == KeyEvent.VK_SPACE) {
    				READY = false;
    				System.out.println("Not READY TO FIRE! \n");
    				if(READY=true) {
    					yShoot = yShoot -5;
    					repaint();
    				}
    			}
    		}
    		//ignore this
    		@Override
    		public void keyTyped(KeyEvent e) {
     
    		}
    	}
     
     
     
     
    	public void paint(Graphics g) {
    		//I don't even know what this means, someone said it just doublebuffers the program
    		Image dbImage = createImage(getWidth(), getHeight());
    		Graphics dbGraphics = dbImage.getGraphics();
    		paintComponent(dbGraphics);
    		g.drawImage(dbImage, 0, 0, this);
    		repaint();
     
    	}
    // This is where I paint the ship, the bullet, and the enemy to the screen
    	public void paintComponent(Graphics g) {
    		g.setColor(Color.CYAN);
     
    		Rectangle blarp = new Rectangle(x, y, 150, 150);
     
    		// This is where I draw the ship My mistake must be here
    		//I think something is wrong with the drawImage line
    		// I named my Image ship that has the drawing of the ship I made
    		g.drawImage(Ship, (int) blarp.getX(), (int) blarp.getY(), 90, 50, null);
    		// )
    		if (yShoot<4) {
    			yShoot=(int) blarp.getY();
    			xShoot=(int) ((int) blarp.getX() + 37.5);
    			repaint();
    			BulletShown=false;
    		}
    		//Bullet Stuff
    		g.setColor(Color.RED);
    		Rectangle Bullet = new Rectangle(xShoot, yShoot, 10, 20);
     
    		if(BulletShown==true){
     
    			g.fillRect((int) Bullet.getX(), (int) Bullet.getY(), 20, 30);
    			repaint();}
     
     
     
     
    		if(yShoot<323) {
    			yShoot = yShoot - 5;
    			repaint();
    		}	
    		// )
    		// Enemy Stuff (
    		if(enemy1hit==false) {
    			g.fillOval(50, 50, 10, 10);
    			repaint();
    		}
    		if(Bullet.getX()<=57 && Bullet.getX()>=42 && Bullet.getY()<=60 && Bullet.getY()>=40) {
    			System.out.println("ENEMY1 Hit!");
    			enemy1hit= true;
     
    		// )
    		}
     
    	}
    }

    This code compiles, runs and the println()s still show that x and y are changing when you hit the left and right arrow key. Also if you hit space the "ship" still shoots a red rectangle and the enemy still disapears when it is hit (the enemy is the red circle). The only thing that is wrong is that you can't see the image and I don't know why. I have had this problem for weeks and have posted on my java forums and no one has an answer. If you need any more info to solve this just ask. This is really frustrating and I appreciate anyone that even tries to help me. Also the main method for this program is in another class. I highly doubt anything is wrong with it, but just in case anyone wants to see it, here it is.

    package galaga;
     
    public class run {
     
     
    	public static void main(String[] args) {
    		mainmethod bob = new mainmethod();
     
     
    	}
     
    }

    Also I don't know if this even matters, but I am using eclipse to right my code. Thanks to anyone who actually reads this and please help me. If you need more info to solve the problem just ask. Thanks again and please, I need help.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Why won't this image display?

    Not the answer to your question (that will take more time to solve), but you don't want to draw directly in the paint method a JFrame or any top-level window but rather in the paintComponent method (use @Override before it to be sure that you're really overriding the parent method) of a class that derives from JComponent such as one that extends JPanel. Also you never want to call repaint() from within a paint or paintComponent method.

    Edit: also, you don't want to have program logic from within your paint or paintComponent method, including the logic that tests if the bullet hits the enemy or not as you do not have full control over if or when the painting methods are called. These methods should be for drawing and drawing only. You will gain much by reading the painting with Swing tutorials including

  3. #3

    Default Re: Why won't this image display?

    "If you eliminate all logical solutions to a problem, the illogical, no matter how improbable, must invariably be true."

    We know your code works. We know it works with the initial image - but not with the image you wanted. Thus - there's something wrong with the Java API. I recommend you make the move to OpenGL for drawing graphics, asap.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Why won't this image display?

    Quote Originally Posted by Programming_Hobbyist View Post
    "If you eliminate all logical solutions to a problem, the illogical, no matter how improbable, must invariably be true."

    We know your code works. We know it works with the initial image - but not with the image you wanted. Thus - there's something wrong with the Java API. I recommend you make the move to OpenGL for drawing graphics, asap.
    My BS detector just registered off of the map. Please tell me that you're being sarcastic here.

  5. #5

    Default Re: Why won't this image display?

    LoL, good one.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Why won't this image display?

    Don't get me wrong, I'm not putting down OpenGL, and I'm certainly not going to belittle the related LWJGL, but we can't say by any means that the original poster has "eliminate[d] all logical solutions to a problem" as his code has so many large holes as to allow one to drive a Mac truck through them.

Similar Threads

  1. Image won't work!
    By mkrage in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2012, 05:08 PM
  2. Image Doesn't Display with Qualified Image Path????
    By swaginator in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 31st, 2012, 12:29 AM
  3. Won't display calculated fahrenheit and celsius when running.
    By smithmar in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 17th, 2012, 09:00 PM
  4. Can't display an image...
    By barebackingtiger in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2012, 07:21 PM
  5. String won't display in my window
    By JamEngulfer221 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 24th, 2011, 04:23 PM

Tags for this Thread