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.setBounds(0,0,WIDTH,HEIGHT);
        panel.setPreferredSize(new Dimension(WIDTH,HEIGHT));
        panel.setLayout(null);
        panel.add(this);

        window.setBounds(0,0,WIDTH,HEIGHT);
        window.setVisible(true);

        window.addWindowListener(new WindowAdapter() {

            public void WindowClosing(WindowEvent e) {

                System.exit(0);
            }
        });

    }   

    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);
            System.exit(0);
            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");
                     stuff.add(btn);
                     content.add(stuff);
                    setLayout(new FlowLayout());
                    setContentPane(content);
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    setSize(300,300):
                    setVisible(true);
          }
}

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

To get rid of your problems with current code:

panel.add(this);

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);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.