Ok so I am trying to make galaga and I have this spaceship image saved into a .png file and want to display it as what you are moving. Previously I had the ship as a rectangle that moved left and right if you hit left or right adn it worked fine. Now I want a spaceship image as teh rectangle so I changed g.fillrect to g.drawImage and it didn't seem to work. Now all it is is a moving bullet and no ship. If you need any more info please ask me because I have had this problem for over a week now and it is very frustrating.
This is the code with a rectangle. This code works and has no problems in it.
package galaga; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; 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.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; ImageIcon Spaceship = new ImageIcon("CRAPPYSPACESHIP.png"); Image Ship = Spaceship.getImage(); ImageObserver bo; public mainmethod() { super("Galaga"); setVisible(true); setLocation(150, 150); setSize(400, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); addKeyListener(bob); setBackground(Color.white); } public class KL implements KeyListener { public void keyPressed(KeyEvent e) { 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); if (x <= 10) { x = 10; System.out .println("Collision detected with left side of window!"); } } 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 ); } if (x >= 300) { x = 300; System.out .println("Collision detected with right side of window!"); } repaint(); if (e.getKeyCode() == KeyEvent.VK_SPACE) { READY = true; System.out.println("FIRE! \n"); } } 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(); } } } @Override public void keyTyped(KeyEvent e) { } } public void paint(Graphics g) { Image dbImage = createImage(getWidth(), getHeight()); Graphics dbGraphics = dbImage.getGraphics(); paintComponent(dbGraphics); g.drawImage(dbImage, 0, 0, this); repaint(); } public void paintComponent(Graphics g) { g.setColor(Color.CYAN); Rectangle blarp = new Rectangle(x, y, 150, 150); g.fillRect((int) blarp.getX(), (int) blarp.getY(), 90, 50); g.drawImage(Ship, (int) blarp.getX(), (int) blarp.getY(), 90, 50, this); if (yShoot<4) { yShoot=(int) blarp.getY(); xShoot=(int) ((int) blarp.getX() + 37.5); repaint(); } g.setColor(Color.RED); Rectangle Bullet = new Rectangle(xShoot, yShoot, 10, 20); g.fillRect((int) Bullet.getX(), (int) Bullet.getY(), 20, 30); repaint(); if(yShoot<323) { yShoot = yShoot - 5; repaint(); } 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 is the new code that uses the image and won't display the image.package galaga; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; 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.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; ImageIcon Spaceship = new ImageIcon("CRAPPYSPACESHIP.png"); Image Ship = Spaceship.getImage(); ImageObserver bo; public mainmethod() { super("Galaga"); setVisible(true); setLocation(150, 150); setSize(400, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); addKeyListener(bob); setBackground(Color.white); } public class KL implements KeyListener { public void keyPressed(KeyEvent e) { 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); if (x <= 10) { x = 10; System.out .println("Collision detected with left side of window!"); } } 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 ); } if (x >= 300) { x = 300; System.out .println("Collision detected with right side of window!"); } repaint(); if (e.getKeyCode() == KeyEvent.VK_SPACE) { READY = true; System.out.println("FIRE! \n"); } } 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(); } } } @Override public void keyTyped(KeyEvent e) { } } public void paint(Graphics g) { Image dbImage = createImage(getWidth(), getHeight()); Graphics dbGraphics = dbImage.getGraphics(); paintComponent(dbGraphics); g.drawImage(dbImage, 0, 0, this); repaint(); } public void paintComponent(Graphics g) { g.setColor(Color.CYAN); Rectangle blarp = new Rectangle(x, y, 150, 150); g.drawImage(Ship, (int) blarp.getX(), (int) blarp.getY(), 90, 50, this); if (yShoot<4) { yShoot=(int) blarp.getY(); xShoot=(int) ((int) blarp.getX() + 37.5); repaint(); } g.setColor(Color.RED); Rectangle Bullet = new Rectangle(xShoot, yShoot, 10, 20); g.fillRect((int) Bullet.getX(), (int) Bullet.getY(), 20, 30); repaint(); if(yShoot<323) { yShoot = yShoot - 5; repaint(); } 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; } } }
If anyone sees what is wrong or can figure it out with more info please answer or ask! this is really frustrating and I don't understand why it won't work. I think it might have something to do with an imageobserver but I have no idea what that it.