2 Replies Latest reply: Jan 11, 2010 8:18 AM by 843853 RSS

    Need help with converting game application to Applet

    843853
      Im converting my Pacman application to an Applet so i can host it on my website. I can get the board to paint but when i added the Pacman he didnt show up and dont know why.
      Im not getting any compilation errors. Im new to Applets and just self learning it. My Pacman and ghosts run on their own threads and am wondering if this is a problem with applets. I'll post my main Pacman Game class and my Pacman class below.
      Any help would be much appreciated.
      import java.applet.*;
      import java.awt.*;
      import javax.swing.*;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.image.BufferedImage;
      import java.awt.image.BufferStrategy;
      import java.awt.event.KeyEvent;
      import java.awt.event.KeyListener;
      
      public class PacmanGame extends Applet implements KeyListener {
           
           boolean isRunning=true;
           static boolean panic=false;
           static boolean caught=false;
           static int panicCount=0;
           static ThePacman pac;
           //static Ghosts blinky, pinky, inky, clyde;
           static int board[][]= new int[19][19];
           static int initialBoard[][]= { 
           {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
           {1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1},
           {1,3,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,3,1},
           {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
           {1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1},
           {1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1},
           {1,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1},
           {1,2,2,1,0,0,0,0,0,0,0,0,0,0,0,1,2,2,1},
           {1,1,1,1,0,1,0,1,4,4,4,1,0,1,0,1,1,1,1},
           {1,0,0,0,0,1,0,1,1,1,1,1,0,1,0,0,0,0,1},
           {1,1,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,1,1},
           {1,2,2,1,0,1,0,0,0,0,0,0,0,1,0,1,2,2,1},
           {1,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1},
           {1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1},
           {1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1},
           {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
           {1,3,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,3,1},
           {1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1},
           {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} };
           static int count1 = 0;
           static int count2 = 0;
           //static ImageIcon icon = new ImageIcon("Ghost1.gif", "Pacman");
           Image[] picture;
           
              public void init() {
                setSize(800,800);
           for (int i =0;i<19;i++)
                   for (int j=0;j<19;j++)
                   board[i][j]=initialBoard[i][j];      
          setBackground( Color.blue );
          pac = new ThePacman(this);
          pac.start();
         }
      
         public void paint( Graphics g ) {
                
                picture=pac.getImage();  
                 if(picture[pac.getCurrent()] != null){
                   g.drawImage(picture[pac.getCurrent()], pac.getPacX(), pac.getPacY(), 32,32, this);       
                }
                int count1 = 0;
                int count2 = 0;
                int xbound = 32;
                int ybound = 32;
           
                for(count1=0; count1 < 19; count1++)
                     {
                          
                          for(count2=0; count2 < 19; count2++)
                          {     
                               
                               switch (board[count1][count2])
                               {
                                    case 0: 
                                    {
                                         g.setColor(Color.black);
                                         g.fillRect(xbound,ybound,32,32);
                                         g.setColor(Color.yellow);
                                         g.fillOval(xbound+10,ybound+10,7,7);
                                    }
                                    break;
                                    case 1:
                                    {
                                         g.setColor(Color.blue);
                                         g.fillRect(xbound,ybound,32,32);
                                         g.setColor(Color.black);
                                         g.drawRect(xbound,ybound,32,32);
                                    }
                                    break;
                                    case 2:
                                    {
                                         g.setColor(Color.black);
                                         g.fillRect(xbound,ybound,32,32);
                                    }
                                    break;
                                    case 3:
                                    {
                                         g.setColor(Color.black);
                                         g.fillRect(xbound,ybound,32,32);
                                         g.setColor(Color.pink);
                                         g.fillOval(xbound+10,ybound+10,10,10);
                                    }
                                    break;
                                    case 4:
                                    {
                                         g.setColor(Color.black);
                                         g.fillRect(xbound,ybound,32,32);
                                         g.setColor(Color.blue);
                                    }
                                    
                               }
                                         xbound = xbound + 32; 
                          }
                          ybound = ybound + 32;
                          xbound = 32;
                     }
                g.setColor(Color.BLACK);
                //draw ThePacman and Ghosts*/
           
           }
             
          //add keyListener for pacman
          public void keyPressed(KeyEvent k)
            {
                
            }     
           public void keyTyped(KeyEvent k) {}//empty
            public void keyReleased(KeyEvent k) {}//empty
      }
        • 1. Re: Need help with converting game application to Applet
          843853
          import java.awt.*;
          import java.awt.event.KeyEvent;
          import java.awt.event.KeyListener;
          //import javax.swing.JFrame;
          //import javax.swing.JPanel;
          
          class ThePacman extends Thread //implements Runnable
          {
               private boolean isRunning;
               private int pacX, pacY;
               char direction = 'r';
               int SPEED = 200;
               private int current=0;
               public int vx=20;
               PacmanGame parent;
               //Ghosts blinky;
               //ThePacman pac;
               private Thread thread1; 
               static String[] pacmanLeft = {"PacMan1.gif","PacMan2left.gif","PacMan3left.gif","PacMan4left.gif"};
                static String[] pacmanRight = {"PacMan1.gif","PacMan2right.gif","PacMan3right.gif","PacMan4right.gif"};
                static String[] pacmanUp = {"PacMan1.gif","PacMan2up.gif","PacMan3up.gif","PacMan4up.gif"};
                static String[] pacmanDown = {"PacMan1.gif","PacMan2down.gif","PacMan3down.gif","PacMan4down.gif"};
                Image[] pacLeft = new Image[pacmanLeft.length];
                Image[] pacRight = new Image[pacmanRight.length];
                Image[] pacUp = new Image[pacmanUp.length];
                Image[] pacDown = new Image[pacmanDown.length];
               
                public static boolean pleaseWait=true;
                
               public ThePacman(PacmanGame parent)
               {
                  this.parent = parent;
                  isRunning = true;
               }
          
               public void run()
               {
                         while(isRunning)  // a continuous loop
                        {      
                           
                            /*synchronized (this) {
                                 while (pleaseWait==true) {
                                  try {
                                      wait();
                                      System.out.println("waiting");
                                  } catch (Exception e) {}
                                }    
                             }*/
                           //Pacman Images
                           //Toolkit kit = Toolkit.getDefaultToolkit().getImage("C:\\Users\\PHILIP\\Desktop\\WANG\\res\\");
                           Toolkit kit = Toolkit.getDefaultToolkit();
                            for (int i = 0; i < pacLeft.length; i++){
                                pacLeft[i] = kit.getImage(pacmanLeft);
                    pacRight[i] = kit.getImage(pacmanRight[i]);
                    pacUp[i] = kit.getImage(pacmanUp[i]);
                    pacDown[i] = kit.getImage(pacmanDown[i]);
                    }
                         current++;
                    if (current >pacLeft.length - 1)
               {
               current = 0;
               }

                         
                         try
               {
               if(PacmanGame.caught==true)
               {
                    
                    thread1.sleep(3000);
               }
               else
               //if(PacmanGame2.pause==true)
               {
                    thread1.sleep(SPEED);
               }
               parent.repaint();
               }
               catch(InterruptedException e)
               {
                    System.err.println(e);
               }
                    updatePacman();
                         //PacmanGame.updateWorld();
                         //PacmanGame.caught=false;
                    }
                    
          }
          //set the status
          protected void setRunning(Boolean runningFlag)
          {     
               isRunning = runningFlag;
          }
          public void setDirection(char dir){direction = dir;}
          public Image[] getImage()
          {
               Image[]pacDir=pacLeft;
               switch(direction){
                    case('l'):
                         pacDir=pacLeft;
                         break;
                    case('r'):
                         pacDir=pacRight;
                         break;
                    case('u'):
                         pacDir=pacUp;
                         break;
                    case('d'):
                         pacDir=pacDown;
                         break;
               }
               return pacDir;
          }
          //public void stop()
          //{
          //     setRunning=false;
          //}
          public int getPacX(){return pacX;}
          public int getPacY(){return pacY;}
          public void setPacX(int i){pacX=i;}
          public void setPacY(int i){pacY=i;}
          public int getCurrent(){return current;}
          }
          • 2. Re: Need help with converting game application to Applet
            843853
            I managed to work it out. Thread answered!