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.
Code :
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.
Code :
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.
Code :
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.
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
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.
Re: Why won't this image display?
Quote:
Originally Posted by
Programming_Hobbyist
"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.
Re: Why won't this image display?
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.