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.