I am a student and am pretty new with java. I'm trying to write a basic pacman applet for a class my problem is i have the code written to draw the maze and the pacman but when i run the applet all I am getting is the black background and nothing else. There is no errors being report by java virtual machine or eclipse. If anyone could help me out i would really appreciate it.

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.io.*;
    import java.net.*;

    import javax.swing.Timer;



    public class pacman extends Applet 
    {
        /**
         * 
         */
        private static final long serialVersionUID = -22561259505737423L;
        private final int WIDTH = 28;
        private final int HEIGHT = 31;
        String fileToRead = "dev/maze.txt";
        StringBuffer strBuff;
        char map;
        char c;
        char[][] mapArray = new char[WIDTH][HEIGHT];
        Image[] images = new Image[19];
        int mapWidth = 28, mapHeight = 31;
        URL base; 
        MediaTracker mt;
        int cordx = 100;
        int cordy= 100;

        public static void main(String[] args) {
            pacman array = new pacman();
            array.loadFile("dev/maze.txt");
            array.printArray();
        }

        public void init(KeyEvent e )
        {       
            new Timer(50, (ActionListener) e).start();
            mt = new MediaTracker(this);
            try
            {
                base = getDocumentBase();
            }
            catch (Exception a) {}
            images[0] = getImage(base,"Pac-Images/Pac-Right1.jpg");
            images[1] = getImage(base,"Pac-Images/Pac-Right2.jpg");
            images[2] = getImage(base,"Pac-Images/Pac-Right3.jpg");
            images[3] = getImage(base,"Pac-Images/Pac-Right4.jpg");
            images[4] = getImage(base,"Pac-Images/Mon2Right1.jpg");
            images[5] = getImage(base,"Pac-Images/Mon2Right2.jpg");
            images[6] = getImage(base,"Pac-Images/Mon2Right3.jpg");
            images[7] = getImage(base,"Pac-Images/Tile-BotLeftl.jpg");
            images[8] = getImage(base,"Pac-Images/Tile-BotRight.jpg");
            images[9] = getImage(base,"Pac-Images/Tile-Dot.jpg");
            images[10] = getImage(base,"Pac-Images/Tile-Horizontal.jpg");
            images[11] = getImage(base,"Pac-Images/Tile-PowerDot1.jpg");
            images[12] = getImage(base,"Pac-Images/Tile-PowerDot2.jpg");
            images[13] = getImage(base,"Pac-Images/Tile-PowerDot3.jpg");
            images[14] = getImage(base,"Pac-Images/Tile-Space.jpg");
            images[15] = getImage(base,"Pac-Images/Tile-TopLeft.jpg");
            images[16] = getImage(base,"Pac-Images/Tile-TopRight.jpg");
            images[17] = getImage(base,"Pac-Images/Tile-Vertical.jpg");
            images[18] = getImage(base,"Pac-Images/Tile-Wall.jpg");
            mt.addImage(images[0],0);
            mt.addImage(images[1],1);
            mt.addImage(images[2],2);
            mt.addImage(images[3],3);
            mt.addImage(images[4],4);
            mt.addImage(images[5],5);
            mt.addImage(images[6],6);
            mt.addImage(images[7],7);
            mt.addImage(images[8],8);
            mt.addImage(images[9],9);
            mt.addImage(images[10],10);
            mt.addImage(images[11],11);
            mt.addImage(images[12],12);
            mt.addImage(images[13],13);
            mt.addImage(images[14],14);
            mt.addImage(images[15],15);
            mt.addImage(images[16],16);
            mt.addImage(images[17],17);
            mt.addImage(images[18],18);

            String prHtml = this.getParameter("fileToRead");
            if (prHtml != null) fileToRead  = new String(prHtml);
            loadFile(fileToRead);

            try
            {
                mt.waitForAll();
            }
            catch (InterruptedException a) {}
        }

        public void loadFile(String fname) {
            try {
                BufferedReader reader = new BufferedReader(new FileReader(fname));

                String line;
                int col = 0, row = 0;
                while((line = reader.readLine()) != null && row < HEIGHT) {
                    for(col = 0; col < line.length() && col < WIDTH; col++) {
                        mapArray[col][row] = line.charAt(col);
                    }
                    row++;
                }
                reader.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }

        public void printArray() {
            for(int row = 0; row < HEIGHT; row++) {
                for(int col = 0; col < WIDTH; col++) {
                    System.out.print(mapArray[col][row]);
                }
                System.out.println();
            }
        }

        public void DrawPacMan(Graphics g2) {

            int pacmanx = cordx;
            int pacmany = cordy;
            int count = 0;

                if (count == 0){
                    g2.drawImage(images[0], pacmanx +2, pacmany+2, this);
                    count ++;
                }
                if(count == 1){
                    g2.drawImage(images[1], pacmanx+2, pacmany+2, this);
                    count ++;
                }
                if(count == 2){
                    g2.drawImage(images[2], pacmanx+2, pacmany+2, this);
                    count ++;
                }
                if(count == 3){
                    g2.drawImage(images[3], pacmanx+2,pacmany+2, this);
                    count ++;

                    }
                count = 0;
        }
        public void keyPressed(KeyEvent ke) {
            switch (ke.getKeyCode()) {

            case KeyEvent.VK_RIGHT: {
                cordx+= 1;
            }
            break;

            case KeyEvent.VK_LEFT: {
                cordx-= 1;
            }
            break;

            case KeyEvent.VK_DOWN: {
                cordy+= 1;
            }
            break;

            case KeyEvent.VK_UP: {
                cordy-= 1;
            }
            break;
        }
        repaint();
        }
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();

            if (key == Event.LEFT || key == Event.RIGHT || 
               key == Event.UP ||  key == Event.DOWN)
            {
              cordx+=0;
              cordy+=0;
            }
        }

     public void actionPerformed1(ActionEvent e) {
         repaint();    }

        public void paint(Graphics g)
        {
            setSize(380,420);
            setBackground(Color.black);
            for (int y = 0; y < mapHeight; y++)
                for (int x = 0; x < mapWidth; x++)
                {
                    char arrayIN = mapArray[x][y];
                    int im;
                    switch(arrayIN)
                    {
                        case ' ' : im = 14; break;
                        case 'X' : im = 18; break;
                        case '.' : im = 9;  break;
                        case '-' : im = 10; break;
                        case '|' : im = 17; break;
                        case '[' : im = 16; break;
                        case ']' : im = 15; break;
                        case '(' : im = 8;  break;
                        case ')' : im = 7;  break;
                        case 'O' : im = 11; break;
                        default  : im = 18;
                    };
                    g.drawImage(images[im],x*16,y*16,this);
                    System.out.println("current char"); // for debugging 
                    System.out.println(arrayIN);  // for debugging 

                } 

        }


    }

Is the paint() being called when you expect it to be called?
Are the images being loaded properly so they can be drawn?

Add a call to the printStackTrace method in the catch block so you can see if there are any errors.

thanks, i beleive paint is being called as expected but honestly not sure. i didn't see any errors. I am getting a console output of "current char" but it is not outputting the current char from arrayIN which makes me think there is something wrong with my file being read in to the array. How can i verify the file is being read in correctly?

verify the file is being read in correctly

If the file is a text file, print out the lines in the file to see if you have read the contents correctly.

How are you executing this program? I see a main() method that is not used by applets when they are executed in a browser.

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