Results 1 to 4 of 4
  1. #1
    JayRee is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Having few problems with a swing.timer

    Hey guys, Just finishing off my assignment (make a lunar lander game).

    But I'm having trouble with the timer.

    I have the game run on a timer which reprints the paint everytime it loops.
    But when the Lunar ship crashes or lands, its carrying on looping.

    I want a way to stop the timer, and then have an option to restart the game (timer).

    Heres my main code:

    Java Code:
    /**
     * Write a description of class LunarLander here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    
    //Functionality
    import javax.swing.JFrame;      // window functionality
    import java.awt.Color;          // RGB color stuff
    
    import javax.swing.Timer;       // import timer functionality
    import java.awt.event.*;        // functionality for the event fired by the timer
    
    import java.awt.*;              // Graphics stuff from the AWT library, e.g. Graphics
    import javax.swing.*;           // Graphics stuff from the Swing library, e.g. JLabel
    import java.awt.image.BufferedImage; // Graphics drawing canvas
    import java.awt.event.KeyEvent;
    import java.util.Random;
    
    
    public class LunarLander extends JFrame{
    
        // The Java window
        private static JFrame window;
        
        // Graphics "Handle"
        private static Graphics gr; 
        
        // Image Storage
        private static Image theBackground; // Background Image
        private static Image theLander; // PNG of the Lunar Lander
        private static Image theBang; // Animated Explosion
        private static Image theLand; // Lunar lander landed PNG
        public static Random randomGen= new Random();
        
        private static int landerX = randomGen.nextInt(800 - 50), landerY = 50; // Starting position of the lander
        private static boolean  explosion=false; // Sets explosion to false
        public static int speedY = 0;
        public static int speedX = 0;
        public static double speedStopping = 1;
        public static int speedAccelerating = 2;
        public static int topLandingSpeed = 5;
                                            
        
        
        // Main Method, Create window
        public static void main(String[] args) {
            
            //Window Setup
            window = new JFrame(); // Renames the new window
            
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Sets the X to close
            window.setTitle("Moon Lander, Can you be the first man on the moon!"); // Title Name
            window.setSize(800, 600); // Window Size
            window.setResizable(true); // Disables resizable window
            window.setLocationRelativeTo(null); // Sets location to top of the screen
            
            BufferedImage canvas=new BufferedImage(800,600,BufferedImage.TYPE_INT_ARGB); // Creates A Canvas
            gr=canvas.getGraphics(); // Creates handle for drawing on the canvas
            JLabel label=new JLabel(new ImageIcon(canvas)); // Creates label to draw on
            window.add(label);
            
            window.setVisible(true); // Displays above
            
            //Loading Images
            theBackground = GameImage.loadImage("Images//Background.png"); // Loads background
            theLander = GameImage.loadImage("Images//LunarLander.png"); // Loads lander
            theBang = GameImage.loadImage("Images//explosion.gif"); // Loads Explosion
            theLand = GameImage.loadImage("Images//LunarLanded.png"); //Loads landed lunar
                 
            GameKeyboard.initialise(); // Starts keyboard input
            
            
            
            //Timer Setup
            ActionListener taskPerformer = new ActionListener()
                {
                    public void actionPerformed(ActionEvent evt) // Calls the actionPerformed() method
                    {
                        doTimerAction();
                    }    
                };
            Timer screenTimer = new Timer(75, taskPerformer); // Creates Timer
            screenTimer.start(); // Starts Timer
        }
        
        // Method to draw all needed images, then timer resets to display on the screen
        public static void doTimerAction(){
            
            gr.drawImage(theBackground, 0, 0, null); // Overwrites everything with the background image
            
            // User Instructions
            gr.setColor( Color.red );  // Sets colour red
            gr.setFont(new Font("Arial", Font.BOLD, 16)); // Sets font
            gr.drawString("Arrow keys to move, land on the platform to win", 200,200); // Output and location
            
    
            
            // Get keys pressed from the keyboard
            char key= GameKeyboard.getKey();
            int specialKey=GameKeyboard.getSpecialKey();
            
            
            //Keyboard Binding
            
            if (specialKey == 38){
                speedY -= speedAccelerating;
            }else{ 
                speedY += speedStopping;
            }
            
            if (specialKey == 37){
                speedX -= speedAccelerating;
            }else{
                speedX += speedStopping;
            }
                
            if (specialKey == 39){
                speedX += speedAccelerating;
            }else{
                speedX -= speedStopping;
           }
            
            landerX += speedX;
            landerY += speedY;
            
            // Check if landed
                
            if (landerX>=709 && landerX<=739 && landerY==482){
                    
                    gr.drawImage(theBackground, 0, 0, null);
                    gr.drawImage(theLand, landerX - 25, landerY - 50, null);
                    gr.setColor( Color.red );  // Sets colour red
                    gr.setFont(new Font("Arial", Font.BOLD, 16)); // Sets font
                    gr.drawString("Congratulations, You have landed!", 200,400);
                    
                    screenTimer.cancel();  << heres where I want it to stop 
                }  
    
            
    
            gr.setColor( Color.red );  // Sets colour red
            gr.setFont(new Font("Arial", Font.BOLD, 16)); // Sets font
            gr.drawString("speedX" + speedX + "speedY" + speedY + "speedAccelerating" +speedAccelerating + "speedStopping" + speedStopping + "landerY" + landerY + "landerX" + landerX, 200,400);
            gr.drawImage(theLander, landerX, landerY, null); // Draws lander and its position
            
        //}    
        window.repaint();  // Re paints everything onto the screen
        }    
    }

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    351
    Rep Power
    4

    Default Re: Having few problems with a swing.timer

    Hi JayRee, welcome to the forums.

    It may be best to seperate the graphics from the loop and just make a call to the relevant method. By seperating out the functionality it may be easier to see what you are doing.
    Within the loop you could move the ship and check to determine its current state. At the end of the loop you would call the paint method. Aassuming you crashed you could also stop the timer and show a dialog box asking the user if they want to play again.

    Regards.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Having few problems with a swing.timer

    Quote Originally Posted by Ronin View Post
    At the end of the loop you would call the repaint() method.
    FTFY :)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    351
    Rep Power
    4

    Default Re: Having few problems with a swing.timer

    Thanks for the correction.

Similar Threads

  1. Thread v swing timer
    By musico in forum New To Java
    Replies: 1
    Last Post: 06-23-2011, 09:51 PM
  2. help with Swing Timer animation
    By tomas1991 in forum New To Java
    Replies: 7
    Last Post: 03-19-2010, 09:06 AM
  3. Swing Timer Questions
    By morfasto in forum New To Java
    Replies: 9
    Last Post: 11-14-2009, 03:48 PM
  4. Timer on swing
    By finzaiko in forum AWT / Swing
    Replies: 3
    Last Post: 04-02-2009, 07:45 AM
  5. Problems with Graphics and a Timer...
    By r0binho0d in forum Java Applets
    Replies: 5
    Last Post: 07-26-2008, 03:03 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
  •