Results 1 to 5 of 5
- 11-03-2012, 11:41 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
How do you display an image on a JFrame?
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.
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.Java 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; // ) } } }
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.Java 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; // ) } } }
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. All help will be MUCH appreciated.Java Code:package galaga; public class run { public static void main(String[] args) { mainmethod bob = new mainmethod(); } }
Last edited by themkrage; 11-03-2012 at 11:43 PM.
- 11-03-2012, 11:59 PM #2
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: How do you display an image on a JFrame?
I haven't looked through all of this... BUT.... simplicity first.
try copying the png to a safe location like c:\temp and do
to see if it isn't just a case of not finding your image.Java Code:ImageIcon Spaceship = new ImageIcon("C:\\TEMP\\CRAPPYSPACESHIP.png");
-
Re: How do you display an image on a JFrame?
cross-posted: Why won't this image display?
To the original poster, cross-posting without notifying all threads can frustrate anyone who tries to help you when they find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be that if you feel that you absolutely must cross-post, to at least provide links in both cross-posts to each other.
- 11-04-2012, 09:20 PM #4
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
Re: How do you display an image on a JFrame?
Yes this was the problem! thanks so much it works now and now my boss galaga program is complete!!!! Thank you sooooooooooooooooooooooooooooooooooooooooooooooooo oooooooooooooooooooooooooooooooooooooooooooooooooo oooooooooooooooooooooooooooooooooooooooooooooooooo oooooooooooooooooooooooooooooooooooooooooooooooooo oooooooooooooooooooooooooooooooooooooooooooooooooo oooo
much!!!
-
Re: How do you display an image on a JFrame?
Please notify all cross-posts that you've got a solution. Thank you in advance.
Similar Threads
-
How to display two tables using Jframe?
By ynglsuresh in forum AWT / SwingReplies: 1Last Post: 03-20-2012, 11:42 AM -
Why won't this rectangle display in my JFrame?
By wikemol in forum New To JavaReplies: 1Last Post: 01-07-2012, 07:14 AM -
Second JFrame won't display as I intend
By elnbado in forum AWT / SwingReplies: 0Last Post: 03-08-2011, 12:45 PM -
can display image in JFrame?
By xCLARAx in forum AWT / SwingReplies: 14Last Post: 04-03-2009, 07:02 PM -
How to display scrolling text and image on a JFrame
By Abhi_vk in forum AWT / SwingReplies: 1Last Post: 06-20-2008, 10:19 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks