1 Reply Latest reply: Aug 12, 2011 4:34 PM by jschellSomeoneStoleMyAlias RSS

    Problem using the applet for the game design!

    877439
      import java.awt.*;
      import java.applet.*;
      import java.awt.event.*;
      import javax.swing.*;
      import java.util.Random;
      /* <applet code="Gameshtap2.class" width ="500" height="600">
      </applet>
      */
      public class Gameshtap2 extends Applet implements ActionListener,KeyListener,MouseListener,MouseMotionListener,Runnable
      {
      Thread t;
      Button b1,b2;
      int y=0;
      int Xpos=480;
      char keyChar='A'; 
      public void init()
      {
      t=new Thread();
      b1=new Button("Start");
      b2=new Button("Stop");
      
      add(b1);
      add(b2);
      
      b1.addActionListener(this);
      b2.addActionListener(this);
      addKeyListener(this);
      this.requestFocus();
      this.setFocusable(true);
      addMouseListener(this);
      addMouseMotionListener(this);
      }
      
      public void paint(Graphics g)
      
      {
      int no;
      g.drawArc(0,y,100,100,30,300);
      g.drawLine(50,y+50,50+(int)(50*Math.cos(3.14159/6)),y+50-(int)(50*Math.sin(3.14159/6)));
      g.drawLine(50,y+50,50+(int)(50*Math.cos(3.14159/6)),y+50+(int)(50*Math.sin(3.14159/6)));  
       
            
       for(int i=0;i<10;i++)
         {
       
         no=(int)(Math.random()*600);
         g.drawOval(Xpos,no,10,10);
         if(Xpos<=480 && Xpos>=0)
            {
           Xpos=Xpos--;
           }
          if(Xpos>480 || Xpos<0)
          {
            Xpos=480;
           }
      }
      }
      
      public void actionPerformed(ActionEvent e)
      {
       if(e.getSource()==b1)
           {
           if (t == null)
              {
                t=new Thread(this); 
                t.start();        
           }
       } else if(e.getSource()==b2)
           {
           if(t!=null)     
                    t = null;
             }
       
      }
      
      public synchronized void keyPressed(KeyEvent ep)
      {
      
      
       switch(ep.getKeyCode())
         {
           case KeyEvent.VK_DOWN:y+=10;
                       
                                if(y>=490)
                                   y=0;
                                else if(y<=0)
                                y=490;               
                     repaint();     
                              break;     
      
          case KeyEvent.VK_UP:y-=10;
                       
                                if(y>=490)
                                   y=0;
                                else if(y<=0)
                                y=490;     
                     repaint();
                              break;     
         default:keyChar=ep.getKeyChar();
         }
        
       }   
      
      public  synchronized void run()
          {
             
      while(true)
          {
           repaint();
         try
         {
              t.sleep((int)(Math.random()*200));
                 
          }
         catch(InterruptedException e1)
         {
             e1.printStackTrace();
         }
              
      }
      
      
      }
      
      public void keyReleased(KeyEvent er)
      {
         System.out.println("released");
       }
      public void keyTyped(KeyEvent er)
      {
         System.out.println("Typed");
       }  
      
      public void mouseClicked(MouseEvent me)
      {
        int m=me.getX();
        int p=me.getY();
         showStatus("X coordinate is:"+m+"Y coordinate is:"+p);
      
      }
      public void mouseMoved(MouseEvent mp)
      {
      }
      public void mouseDragged(MouseEvent mn)
       {
        }
      public void mousePressed(MouseEvent mr)
      {
      
      }
      public void mouseReleased(MouseEvent mc)
       {
        }
      public void mouseExited(MouseEvent ma)
      {
       }
      public void mouseEntered(MouseEvent ml)
       {
       }
      
      }
      --------------------------------------------------------------
      we are trying to implement the game where the big arc controlled by the keyboard is gobbyling up
      the smaller circles,but we are unable to run this program
      regards,
      Jack