-1

It's summer so I thought to teach myself a little bit more so I started to create a pacman game but I'm having an issue just in beginning.

public class PacMan {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {

    GameApp runapp = new GameApp();

    runapp.run();
}

}

class GameApp

public class GameApp {  
    public void run() throws IOException {

        GameCanvas game = new GameCanvas();
        PacPlay play = new PacPlay();
        JFrame frame = new JFrame(game.getTitle());
        frame.setSize(game.getWidth(), game.getHeight());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.add(play);
    } 
} 

other class

public class PacPlay extends JPanel implements ActionListener , KeyListener {

    int X , Y = 0 ;
    int KeyCode ;

    BufferedImage PacUP ;
    BufferedImage PacDOWN ;
    BufferedImage PacLEFT ;
    BufferedImage PacRIGHT ;  

    public PacPlay() throws IOException {
        PacRIGHT = ImageIO.read(new File("images\\right.GIF"));
    }

    public void PaintComponent(Graphics2D g) {
        g.drawImage(PacRIGHT , X, Y , null);
    }

    @Override
    public void keyPressed(KeyEvent ke) {
        KeyCode = ke.getKeyCode();
    }
}

All I get here is just an empty frame. Where am I going wrong?

AMATURE
  • 11
  • 6
  • Time to do some debugging, and start with seeing if you're actually getting an image. Start simple by just trying to read in the image, put it into an ImageIcon and then display the icon in a JOptionPane. **Edit**: and oh yeah, call `setVisible(true)` on the JFrame **after** adding all components as per Reimeus's answer. And don't forget to call the `super.paintComponent(g)` method in your override. – Hovercraft Full Of Eels Jul 20 '14 at 15:53
  • 2
    And start variables with a lowercase letter `KeyCode` -> `keyCode` – takendarkk Jul 20 '14 at 16:01
  • i have 4 different pacman gifs for 4 direction , so i was thinking about making 4 methods that draws every time key is pressed on previous x, y ordinates . is they any better way to do it ? and still getting empty frame , – AMATURE Jul 20 '14 at 17:43
  • What's `GameCanvas` and why are you ignoring it? What's `PacPlay`? – MadProgrammer Jul 21 '14 at 01:31
  • GameCanvas class just have dimensions , PacPlay class have methods to draw and move it around, will also the maze and collision detection algo – AMATURE Jul 21 '14 at 12:30

1 Answers1

5

setVisible should be called after all components have been added to the frame. Also Java is case sensitive, so its

@Override 
public void paintComponent(Graphics g) { // note Graphics instead of Graphics2D 
   super.paintComponent(g);
   g.drawImage(pacRightImage, x, y, this);
}

when doing custom painting. Remember to invoke super.paintComponent(g) to paint background components

Read: Performing Custom Painting

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • still nothing ! tried many things in last hour still on point 0 , can figure it out where i m getting wrong – AMATURE Jul 20 '14 at 17:32
  • 2 questions: 1) Do you get an error when the image is loaded? 2) Is the image an animated GIF by any chance? – Reimeus Jul 20 '14 at 18:31
  • no errors , yes its a GIF , i also tried PNG still same i have 4 different pacman gifs for 4 direction , so i was thinking about making 4 methods that draws every time key is pressed on previous x, y ordinates . is they any better way to do it ? and still getting empty frame – AMATURE Jul 20 '14 at 20:10
  • I don't see any `main` method in `GameApp`. Place the code in the `run` method in an anonymous `Runnable` so that it can be called by `SwingUtilities.invokeLater` – Reimeus Jul 20 '14 at 20:51
  • its in a different class that i didnt showed , i have include it in the question now – AMATURE Jul 20 '14 at 20:52