[hotjoe.com] HotJoe Java Help Forums
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Visit java.com
Please send email if you are having login problems - see the posts below for more info.
Hotmail and Yahoo! users - please see the Hotmail post or the Yahoo! post for information on lost emails.
Need help making a SpaceInvaders game in Java  XML
Forum Index » Swing and AWT
Author Message
AFANDANGL3R

Newbie

Joined: 06/02/2009 19:25:10
Messages: 1
Offline

So here is my code:



import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.*;
import javax.swing.JPanel;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;

public class invaders extends Canvas {
public static final int Width = 800;
public static final int Height = 600;

public invaders() {
JFrame openspace = new JFrame("Space Invaders");

JPanel window = (JPanel)openspace.getContentPane();
setBounds(0,0,Width,Height);
window.setPreferredSize(new Dimension(Width,Height));
window.setLayout(null);
window.add(this);

openspace.setBounds(0,0,Width,Height);
openspace.setVisible(true);
openspace.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}


public void paint(Graphics graph) {
// graph.setColor(Color.red);
// graph.fillOval( Width/2-10, Height/2-10,20,20);
// spacealien = loadImage("spaceAlien.jpg");
// graph.drawImage(spaceAlien, 50, 50, this);
}


public static void main(String[] args) {
invaders spaceinvaders = new invaders();
}
}




I am trying to add pictures into the JFrame but I don't know how. I am following this tutorial http://www.planetalia.com/cursos/Java-Invaders/JAVA-INVADERS-04.tutorial but I get confused when they talk about how to add in an image.
This is their Code:



1 package version04;
2 /**
3 * Curso B?sico de desarrollo de Juegos en Java - Invaders
4 *
5 * (c) 2004 Planetalia S.L. - Todos los derechos reservados. Prohibida su reproducci?n
6 *
7 * http://www.planetalia.com
8 *
9 */
10
11
12 import java.awt.Canvas;
13 import java.awt.Dimension;
14 import java.awt.Graphics;
15 import java.awt.event.WindowAdapter;
16 import java.awt.event.WindowEvent;
17 import java.awt.image.BufferedImage;
18 import java.net.URL;
19
20 import javax.imageio.ImageIO;
21 import javax.swing.JFrame;
22 import javax.swing.JPanel;
23
24 public class Invaders extends Canvas {
25 public static final int WIDTH = 800;
26 public static final int HEIGHT = 600;
27
28
29 public Invaders() {
30 JFrame ventana = new JFrame("Invaders");
31 JPanel panel = (JPanel)ventana.getContentPane();
32 setBounds(0,0,WIDTH,HEIGHT);
33 panel.setPreferredSize(new Dimension(WIDTH,HEIGHT));
34 panel.setLayout(null);
35 panel.add(this);
36 ventana.setBounds(0,0,WIDTH,HEIGHT);
37 ventana.setVisible(true);
38 ventana.addWindowListener( new WindowAdapter() {
39 public void windowClosing(WindowEvent e) {
40 System.exit(0);
41 }
42 });
43 }
44
45 public BufferedImage loadImage(String nombre) {

46 URL url=null;
47 try {
48 url = getClass().getClassLoader().getResource(nombre);
49 return ImageIO.read(url);
50 } catch (Exception e) {
51 System.out.println("No se pudo cargar la imagen " + nombre +" de "+url);
52 System.out.println("El error fue : "+e.getClass().getName()+" "+e.getMessage());
53 System.exit(0);
54 return null;
55 }
56 }
57
58

59 public void paint(Graphics g) {
60 BufferedImage bicho = loadImage("res/bicho.gif");

61 g.drawImage(bicho, 40, 40,this);

62 }
63
64 public static void main(String[] args) {
65 Invaders inv = new Invaders();
66 }
67 }
68




I know there must be an easier way. Please help, thanks!!!!
 
Forum Index » Swing and AWT
Go to:   
Powered by JForum 2.1.8 © JForum Team
This site built by Scott Dunbar of Xigole Systems. © 2005-2010 - Scott Dunbar
Java and the Java Get Powered logo are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.
hotjoe.com and xigole.com have no affiliation with Sun Microsystems, Inc.