Results 1 to 3 of 3
  1. #1
    bluecora is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Help Java Applet loop

    hi, im a noob programmer but i understand something about java. I watched all the video tutorials about java gamimg, but now I want to convert the game into applet but i can't, specially because it doesn't support loops :S

    And if I use the method repaint() the FPS get too low...

    Java Code:
    
    import java.awt.Graphics;
    
    import java.awt.AWTException;
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Window;
    
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseWheelEvent;
    import java.awt.event.MouseWheelListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    
    public class HelloWorld extends JApplet implements MouseMotionListener , MouseListener,MouseWheelListener{ 
        
       /**
        * 
        */
       
       private static final long serialVersionUID = 1L;
       private Image menu;
       private ScreenManager s;
       private Image bg;
       private Image baseFinal;
       private Image baseInicial;
       private Image menuUtilizador;
       private Image botao1;
       private Image botao2;
       private Image skate;
       private Image rampa;
       //
       private Animation policia;
       private Animation ladrao;
       private Animation preso1;
       private Animation preso2;
       private Animation preso3;
       private Animation preso4;   
       private Animation celaFinal;
    
       //
       private String mess="";
       private String messe="";
       //
       private boolean running=true;
       private boolean start=true;
       private boolean mover=false;
       private boolean emCimaB1=true;
       private boolean arrastarSkate=false;
       private boolean arrastarRampa=false;
       boolean pararCela=false;
       boolean podeSubir = false;   
       boolean PrisioneiroSobe=false;
    
       boolean podeDescer=false;
       boolean bateuRampa=false;
       //
       private Sprite sprite;
       private Sprite cela ;
       //
       private long startingTime=System.currentTimeMillis();
       private long cumTime= startingTime;
       private long timePassed = System.currentTimeMillis() - cumTime;
       //
       private Point ponto;
       //
       private int skateX=1000;
       private int skateY=1000;
       private int rampaX=1000;
       private int rampaY=1000;
       //Called when this applet is loaded into the browser.
       
       public void init() {
          
          addMouseListener(this);
          addMouseMotionListener(this);
          addMouseWheelListener(this);
          mess="press escape to exit";
          setForeground(Color.WHITE);
          loadImages();
          
            //Execute a job on the event-dispatching thread; creating this applet's GUI.
       
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                   
                    public void run() {
    
                       startingTime=System.currentTimeMillis();
                      cumTime= startingTime;
                      
                      timePassed = System.currentTimeMillis() - cumTime;
                         cumTime+=timePassed;
                         
                         
                         update(timePassed);
                         updatePrisao( timePassed);
                         preso1.update(timePassed);
                         preso2.update(timePassed);
                         preso3.update(timePassed);
                         preso4.update(timePassed);
                         
                      
                         
                         try{
                            Thread.sleep(20);
                         }catch(Exception ex){}
                     
                     
                    }
                    }
                    
                    
                    
                    );
            } catch (Exception e) {
                System.err.println("createGUI didn't complete successfully");
            }
        
       }
          
       
       
        
        public void loadImages(){
    bg=new ImageIcon("C:/Java/workspace/PAP/Imagens/back.png").getImage();
          
          baseFinal=new ImageIcon("C:/Java/workspace/PAP/Imagens/final.png").getImage();
          baseInicial=new ImageIcon("C:/Java/workspace/PAP/Imagens/inicial.png").getImage();
          skate=new ImageIcon("C:/Java/workspace/PAP/Imagens/skate.png").getImage();
          rampa=new ImageIcon("C:/Java/workspace/PAP/Imagens/rampa.png").getImage();
          
          menu=new ImageIcon("C:/Java/workspace/PAP/Imagens/menu.png").getImage();
          menuUtilizador=new ImageIcon("C:/Java/workspace/PAP/Imagens/menuUtilizador.png").getImage();
          
          
          
          
          botao1=new ImageIcon("C:/Java/workspace/PAP/Imagens/BotaoIniciar1.png").getImage();
          botao2=new ImageIcon("C:/Java/workspace/PAP/Imagens/BotaoIniciar2.png").getImage();
          
          Image police1= new ImageIcon("C:/Java/workspace/PAP/Policia/Image1.png").getImage();
          Image police2= new ImageIcon("C:/Java/workspace/PAP/Policia/Image2.png").getImage();
          Image police3= new ImageIcon("C:/Java/workspace/PAP/Policia/Image3.png").getImage();
          Image police4= new ImageIcon("C:/Java/workspace/PAP/Policia/Image4.png").getImage();
          Image police5= new ImageIcon("C:/Java/workspace/PAP/Policia/Image5.png").getImage();
          Image police6= new ImageIcon("C:/Java/workspace/PAP/Policia/Image6.png").getImage();
          Image police8= new ImageIcon("C:/Java/workspace/PAP/Policia/Image8.png").getImage();
          Image police9= new ImageIcon("C:/Java/workspace/PAP/Policia/Image9.png").getImage();
          Image police10= new ImageIcon("C:/Java/workspace/PAP/Policia/Image10.png").getImage();
          
          
          Image prisao=new ImageIcon("C:/Java/workspace/PAP/Imagens/prisao.png").getImage();
    
          
          
       //
       Image cela1=new ImageIcon("C:/Java/workspace/PAP/Imagens/primeiro.png").getImage();
       Image cela12=new ImageIcon("C:/Java/workspace/PAP/Imagens/primeiro1.png").getImage();
       Image cela13=new ImageIcon("C:/Java/workspace/PAP/Imagens/primeiro2.png").getImage();
    
       Image cela2=new ImageIcon("C:/Java/workspace/PAP/Imagens/segundo.png").getImage();
       Image cela22=new ImageIcon("C:/Java/workspace/PAP/Imagens/segundo1.png").getImage();
       Image cela23=new ImageIcon("C:/Java/workspace/PAP/Imagens/segundo2.png").getImage();
       
       Image cela3=new ImageIcon("C:/Java/workspace/PAP/Imagens/treceiro.png").getImage();
       Image cela32=new ImageIcon("C:/Java/workspace/PAP/Imagens/treceiro2.png").getImage();
       Image cela33=new ImageIcon("C:/Java/workspace/PAP/Imagens/treceiro3.png").getImage();
       
       Image cela4=new ImageIcon("C:/Java/workspace/PAP/Imagens/preso4.png").getImage();
       Image cela42=new ImageIcon("C:/Java/workspace/PAP/Imagens/preso41.png").getImage();
       Image cela43=new ImageIcon("C:/Java/workspace/PAP/Imagens/preso42.png").getImage();
       
       
       //
       
       
       Image ladrao1=new ImageIcon("C:/Java/workspace/PAP/Ladrao/ladrao2test.png").getImage();
       Image ladrao2=new ImageIcon("C:/Java/workspace/PAP/Ladrao/ladrao3test.png").getImage();
       Image ladrao3=new ImageIcon("C:/Java/workspace/PAP/Ladrao/ladrao4test.png").getImage();
       Image ladrao4=new ImageIcon("C:/Java/workspace/PAP/Ladrao/ladrao5test.png").getImage();
       Image ladrao5=new ImageIcon("C:/Java/workspace/PAP/Ladrao/ladrao6test.png").getImage();
       
       policia =new Animation();
       policia.addScene(police1, 50);
       policia.addScene(police2, 50);
       policia.addScene(police3, 50);
       policia.addScene(police4, 50);
       policia.addScene(police5, 50);
       policia.addScene(police6, 50);
       policia.addScene(police8, 50);
       policia.addScene(police9, 50);
       policia.addScene(police10, 50);
       
       //
    
       preso1 = new Animation();
       preso1.addScene(cela1, 150);
       preso1.addScene(cela12, 150);
       preso1.addScene(cela1, 150);
       preso1.addScene(cela13, 150);
       preso1.addScene(cela1, 150);
       
       preso2 = new Animation();
       preso2.addScene(cela2, 150);
       preso2.addScene(cela22, 150);
       preso2.addScene(cela2, 150);
       preso2.addScene(cela23, 150);
       preso2.addScene(cela2, 150);
       
       preso3 = new Animation();
       preso3.addScene(cela3, 150);
       preso3.addScene(cela32, 150);
       preso3.addScene(cela3, 150);
       preso3.addScene(cela33, 150);
       preso3.addScene(cela3, 150);
       
       preso4 = new Animation();
       preso4.addScene(cela4, 150);
       preso4.addScene(cela42, 150);
       preso4.addScene(cela4, 150);
       preso4.addScene(cela43, 150);
       preso4.addScene(cela4, 150);
       
       //
    
       ladrao = new Animation();
       ladrao.addScene(ladrao1, 50);
       ladrao.addScene(ladrao2, 50);
       ladrao.addScene(ladrao3, 50);
       ladrao.addScene(ladrao4, 50);
       ladrao.addScene(ladrao5, 50);
       
       sprite = new Sprite(ladrao);
       sprite.setX(180);
       sprite.setY(340);
       sprite.setVelocityX(0.2f);
       sprite.setVelocityY(0.3f);
       
       
       celaFinal = new Animation();
       celaFinal.addScene(prisao, 150);
       celaFinal.addScene(prisao, 150);
       cela = new Sprite(celaFinal);
       cela.setX(600);
       cela.setY(2);
       cela.setVelocityX(0);
       cela.setVelocityY(0.2f);
       
    
        }
        
        
        
        
      //update sprite 
         public void update (long timePassed){
            
            if(mover){
               if(policia.getIndex()==8)
               sprite.update(timePassed);
               if(policia.getIndex()!=8)
               policia.update(timePassed);
               
            }
            
            if(PrisioneiroSobe==false){
               
            
            if(sprite.getX()<-100 ){
               sprite.setVelocityX(Math.abs(sprite.getVelocityX()));
               mess="Ladrao Fugio";
            
            }else if(sprite.getX() + sprite.getWidth() >= 850){
               //sprite.setVelocityX(-Math.abs(sprite.getVelocityX()));
               mess="bateu lado Direito";
               sprite.setVelocityX(0.1f);
               sprite.setVelocityY(0.1f);
               
            }
            
         
            if( bateuRampa && sprite.getY() <=300 ){
            sprite.setVelocityY(Math.abs(sprite.getVelocityY()));
            mess="bateu em Cima";
            sprite.setY(300);
            
            }else if(sprite.getY() + sprite.getHeight() >= 600){
               //sprite.setVelocityY(-Math.abs(sprite.getVelocityY()));
               mess="bateu em Baixo"+policia.getIndex();
               sprite.setVelocityY(0);
    
            } 
            
            if(sprite.getX() + sprite.getWidth() >= rampaX &&  sprite.getX() + sprite.getWidth() <= rampaX+350){
               
               sprite.setVelocityY(0.2f);
               sprite.setVelocityX(0.15f);
            
               
               sprite.setVelocityY(-Math.abs(sprite.getVelocityY()));
               bateuRampa=true;
            }else if( bateuRampa && sprite.getY() + sprite.getHeight() >=470 && sprite.getX() + sprite.getWidth() >= 650 && sprite.getX() + sprite.getWidth() <= 756){
               mess="bateu na base final";
               sprite.setVelocityY(0);
               sprite.setVelocityX(0);
               podeDescer=true;
               sprite.setX(690);
               
               
            }
            }
            
         }
    
    
         public void updatePrisao(long timePassed){
            
            
            if(cela.getY() <= 0 ){
               pararCela=true;
               podeSubir=false;
            }
            
            
            if(pararCela && podeDescer==false){
               cela.setVelocityY(0);
         
            }
            
            if(cela.getY() + cela.getHeight() >650){
               podeDescer=false;
               podeSubir=true;
               PrisioneiroSobe=true;
    
            }
            
            if(PrisioneiroSobe){
            sprite.setX(cela.getX()+70);
            sprite.setY(cela.getY()+100);
            }
            if(podeDescer){
               cela.update(timePassed);
               mess="mexe";
            }
            
            if(podeSubir){
               cela.setY(cela.getY()-10);
               cela.setVelocityY(0.02f);
               cela.setVelocityY(-Math.abs(sprite.getVelocityY()));
            }
               
         }
         
         
         public void stop(){   
            running=false;
         }
        
        
        
        public void paint(Graphics g) {
           
          if(start){
          g.drawImage(menu,0,0,null);
          if(emCimaB1)
          g.drawImage(botao1,310,300,null);
          else
          g.drawImage(botao2,310,300,null);   
          }
          else{
    
          g.drawImage(bg,0,0,null);
          
          
          g.drawImage(baseFinal,605,450,null);
          g.drawImage(baseInicial,0,450,null);
          g.drawImage(menuUtilizador,600,281,null);
          
          g.drawImage(rampa,rampaX,rampaY,null);
          if(rampaY+50<=500 || rampaX<=200){
          g.drawImage(rampa,1000,1000,null);
          rampaX=1000;
          rampaY=1000;
          }
          
          g.drawImage(skate,skateX,skateY,null);
          if(skateY+20<=500 || skateX<=200){
             g.drawImage(skate,1000,1000,null);
             skateX=1000;
             skateY=1000;
             }
    
          g.drawImage(preso1.getImage(),200,0,null);
          g.drawImage(preso2.getImage(),300,0,null);
          g.drawImage(preso3.getImage(),400,0,null);
          g.drawImage(preso4.getImage(),500,0,null);
    
          g.drawImage(sprite.getImage(), Math.round(sprite.getX()),Math.round(sprite.getY()),null);
          g.drawImage(policia.getImage(),40,322,null);
    
    
          g.drawString(mess,400,600);
          g.drawImage(cela.getImage(), Math.round(cela.getX()),Math.round(cela.getY()),null);
    
    
          g.drawString(messe,40,50);
          
       
        }
      }
        
          //mouse listener interface
          public void mousePressed(MouseEvent e){
             
             if(e.getX()>608 && e.getX()<670 && e.getY()>312 && e.getY()<347)
                arrastarSkate=true;
             else
                arrastarSkate=false;
             
             if(e.getX()>723 && e.getX()<790 && e.getY()>316 && e.getY()<348)
                arrastarRampa=true;
             else
                arrastarRampa=false;
             
             
             
             if(e.getX()>=310 && e.getX()<=510 && e.getY()>=300 && e.getY()<=400 )
             start=false;
             
             this.ponto=e.getPoint();
             messe="Carregou "+ponto;
          
             e.consume();
          }
          
          
          
    
          public void mouseReleased(MouseEvent e){      
             this.ponto=e.getPoint();
    
             messe="Deixou "+ponto;
    
             e.consume();
       }
          
          public void mousecliked(MouseEvent e){}
          public void mouseEntered(MouseEvent e){}
          public void mouseExited(MouseEvent e){}
    
    
    
          //mouse motion interface
          public void mouseDragged(MouseEvent e){
    
             if(arrastarSkate){
             skateX=e.getX();
             skateY=e.getY();
             }
             
             if(arrastarRampa){
             rampaX=e.getX();
             rampaY=e.getY();
             }
             
             this.ponto=e.getPoint();
             messe="Arrastar"+ponto;
       
             e.consume();
          }
          
          public void mouseMoved(MouseEvent e){
             
             if(e.getX()>=310 && e.getX()<=510 && e.getY()>=300 && e.getY()<=400 )
             emCimaB1=false;
             else
             emCimaB1=true;
             
             this.ponto=e.getPoint();
             messe="mexendo"+ponto;
             
             
          }
          
          //Wheel interface
          public void mouseWhellMoved(MouseWheelEvent e){
             this.ponto=e.getPoint();
             messe="mexendo no wheel"+ponto;
             
             
             e.consume();
          }
          
          
    
          @Override
          public void mouseWheelMoved(MouseWheelEvent e) {
    
          }
    
          @Override
          public void mouseClicked(MouseEvent arg0) {
             
          }
          
          public void keyPressed(KeyEvent e){   
             
             int keyCode =((KeyEvent) e).getKeyCode();      
             if(start==false){
                
          if(keyCode==KeyEvent.VK_ENTER){
             mover=true;
    
          }   
          else{
             mess="pressed:"+KeyEvent.getKeyText(keyCode);
             e.consume();
             }
          }      
             if(keyCode==KeyEvent.VK_ESCAPE){
                stop();
             }   else{
                mess="pressed:"+KeyEvent.getKeyText(keyCode);
                e.consume();
             }
             
             if(keyCode==KeyEvent.VK_PRINTSCREEN){
               try {          
                   Robot robot = new Robot();
                   // Capture the screen shot of the area of the screen defined by the rectangle
                   BufferedImage bi=robot.createScreenCapture(new Rectangle(800,600));
                   ImageIO.write(bi, "jpg", new File("C:imageTest.jpg"));       
               } catch (AWTException e1) {
                   e1.printStackTrace();
               } catch (IOException e1) {
                   e1.printStackTrace();
               }
           }
       }
             
             
             
    
          
          //keyReleased
          public void keyReleased(KeyEvent e){
             int keyCode =e.getKeyCode();
             mess="Relessed:"+KeyEvent.getKeyText(keyCode);
             e.consume();
          }
          
          
          //last method from interface
          public void keyTyped(KeyEvent e){
             e.consume();
          }
          
          
        }
    i dont put the repaint becouse i dont now where i go put :S

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Help Java Applet loop

    it doesn't support loops
    Can you explain what you mean? Where do you want to have a loop?

    i dont put the repaint becouse i dont now where i go put
    You would call the repaint() method any time you have changed variables used by the paint method to draw on the screen.

    You should change the code to use a JPanel and move the drawing code to its paintComponent() method.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Help Java Applet loop

    I guess you didn't like the answers I already gave you? Crossposted: Applet loop problem

    I guess I'm just wasting my time trying to help you.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  2. Replies: 30
    Last Post: 08-26-2011, 09:05 PM
  3. Java Applet to Applet communication
    By jsman in forum Java Applets
    Replies: 3
    Last Post: 04-05-2011, 02:23 PM
  4. applet class inside java.applet.* or java.awt.*
    By Manish87 in forum Java Applets
    Replies: 1
    Last Post: 09-27-2010, 02:15 PM
  5. Replies: 10
    Last Post: 09-17-2010, 04:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •