0

I am a beginner programmer trying to create a Pacman game using Java eclipse. I am at the beginning of the process and I'm simply trying to get my main "Princess Pacman" character on the JFrame screen, but, I have this Override error popping up. I have also tried it with out override but it doesn't seem to be working for me that way either.

Here is my code:

import java.awt.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.awt.event.KeyEvent;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Pacman extends JFrame {
    public static final int WIDTH = 500;
    public static final int HEIGHT = 500;

    public static void main(String args[]){
        Pacman gui = new Pacman();
        gui.setVisible(true);
    }

    BufferedImage princess = null;
    public Pacman(){
        super("Princess Pacman");
        //set size of playing space
        setSize(WIDTH,HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try{
             princess =ImageIO.read(new File("images/Elsa.jpeg"));
        }
        catch (IOException e){
            System.out.println("image not found");
        }

    }

    @Override
    public void draw(Graphics2D g){
        g.drawImage(princess.getScaledInstance(100, 100, Image.SCALE_DEFAULT), 0, 0, this);
    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Aliya
  • 3
  • 1

2 Answers2

1

You're trying to override a method that does not exist in the JFrame class. Remove the override annotation.

DSF
  • 804
  • 7
  • 15
  • Oh, good to know. Thank you. However, this does not fix my problem that the image is not showing up on the JFrame. Any suggestions? – Aliya Apr 06 '15 at 06:20
1
  • draw is not a method defined by JFrame or any of its parent classes, therefore it can't be override
  • draw is never called by anything that actually paints
  • You should avoid painting directly to top level containers, there's just to many things been painted onto it.
  • You can use a JLabel, but there are issues with this. Instead, create a custom class extending from JPanel and override its paintComponent method, making sure you call super.paintComponent before you render the image

Take a closer look at Painting in AWT and Swing, Performing Custom Painting and this for example

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366