hey all

when i run this code :

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

import java.awt.image.BufferedImage;

import java.net.URL;

import javax.swing.JFrame;
import javax.swing.JPanel;

import javax.imageio.ImageIO;

public class SpaceInvaders {

    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;

    public SpaceInvaders() {

        JFrame window = new JFrame("Invaders");

        JPanel panel = (JPanel)window.getContentPane();
        panel.setPreferredSize(new Dimension(WIDTH,HEIGHT));


        window.addWindowListener(new WindowAdapter() {

            public void WindowClosing(WindowEvent e) {



    public BufferedImage loadImage(String name) {

        URL url = null;

        try {
            url = getClass().getClassLoader().getResource(name);
            return ImageIO.read(url);
        catch(Exception e) {

            System.out.println("Cannot load image " + name + " in " + url);
            return null;

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

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


    public static void main(String[] args) {

        SpaceInvaders inv = new SpaceInvaders();


i get a problems with panel.add(this) and g.drawImage(bicho,40,40,this) anyone know why?? It says cannot resolve symbol...

ps bout the decytption thing... gonna leave it for the mo till i get more exp.

I can tell you how to get rid of the problems, but the program wont do anything. I don't like the coding style you've used, no offense. If I were going to write that, I would do something like this:

public class Invaders extends JFrame
           public Invaders()
                     Container content = getContentPane();
                     JPanel stuff = new JPanel();
                     JButton btn = new JButton("Button");
                    setLayout(new FlowLayout());

That's just my preffered way of working with stuff like that.

To get rid of your problems with current code:


That will not work. First of all, your using the "this" keywork which would not work in the context. Then, your TRYING to add the JFrame to the JPanel, which also wont work.

Delete that line of code.

Then, you have this line of code:

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

Your not currently working with an ImageObserver, so change it to this:

g.drawImage(bicho, 40,40,null);

This article has been dead for over six months. Start a new discussion instead.