Results 1 to 2 of 2
  1. #1
    Alice.B is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default Help in execution of a Timer

    I am writing a type of side-scrolling dodge game. I am having a slight problem with my logic though. I want to iterate through the 'for' loop going one at a time, with a slight delay between each by using a timer. Unfortunately right now all the circle objects seem to fire off almost at once. I'm sure there's simply something I'm doing wrong, and if anyone could help out I would be very grateful. (I also apologize if this is the wrong forum to ask this in.)

    Here is my current code:

    Java Code:
    //****************************************
    //  NOTE:
    //  It would be wise to create a class called player that does all of the controlling,
    //  rather than having it in the panel class.
    
    package BlockGame;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.Random;
    
    
    /**
     *
     * @author alibresnah10
     */
    public class BlockGamePanel extends JPanel implements 
                                                            KeyListener,
                                                                FocusListener,
                                                                    MouseListener
    {
    
        Random generator = new Random();
        final int WH=300;
        final int AWIDTH, AHEIGHT;
        Timer timer;
        private int arraySize =8;
        private int squareTop=200, squareLeft=200;
        int moveX, moveY;
        private static final int SQUARE_SIZE = 20;
    
    
        Circle[] carray = new Circle[8];
    
    
        public BlockGamePanel(int width, int height)
    
        {
            timer = new Timer(20, new ReboundListener());
            moveX=moveY=generator.nextInt(15);
            for (int i = 0; i < arraySize; i++)
            {
               carray[i]=new Circle();
            }
           
             AWIDTH=width;
             AHEIGHT=height;
    
             setBackground(Color.WHITE);
    
             addKeyListener(this);     // Set up event listening.
             addFocusListener(this);
             addMouseListener(this);
    
    
    
        }
     public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
    
            //change the color for if the panel has focus... needed for keyevents
             if (hasFocus())
                  g.setColor(Color.RED);
             else
                g.setColor(Color.LIGHT_GRAY);
    
            //draw a border that shows whether the panel has focus.
             int width = getSize().width;  // Width of the applet.
             int height = getSize().height; // Height of the applet.
             g.drawRect(0,0,width-1,height-1);
             g.drawRect(1,1,width-3,height-3);
             g.drawRect(2,2,width-5,height-5);
    
    
    
             //draw the player piece, controllable by keyboard.
             g.fillRect(squareLeft, squareTop, SQUARE_SIZE, SQUARE_SIZE);
    
    
    
             for (int i=0; i<carray.length; i++)
             {
                 carray[i].draw(g);
             }
    
    
    
             if (hasFocus()) {
                g.drawString("Arrow Keys Move Player",7,20);
             }
             else
             {
                g.drawString("Click to activate",7,20);
                g.setColor(Color.cyan);
             }
             g.setColor(Color.green);
    
    
            repaint();
        }
    
        public void keyPressed(KeyEvent ke)
        {
            int key = ke.getKeyCode();  // keyboard code for the pressed key
    
             if (key == KeyEvent.VK_LEFT) {
                squareLeft -= 8;
                if (squareLeft < 3)
                   squareLeft = 3;
                repaint();
             }
             else if (key == KeyEvent.VK_RIGHT) {
                squareLeft += 8;
                if (squareLeft > getWidth() - 3 - SQUARE_SIZE)
                   squareLeft = getWidth() - 3 - SQUARE_SIZE;
                repaint();
             }
             else if (key == KeyEvent.VK_UP) {
                squareTop -= 8;
                if (squareTop < 3)
                   squareTop = 3;
                repaint();
             }
             else if (key == KeyEvent.VK_DOWN) {
                squareTop += 8;
                if (squareTop > getHeight() - 3 - SQUARE_SIZE)
                   squareTop = getHeight() - 3 - SQUARE_SIZE;
                repaint();
            }
        }
    
    
    
        public void focusGained(FocusEvent e)
        {
            repaint();
        }
    
        public void focusLost(FocusEvent e)
        {
            repaint();
        }
    
        public void mouseClicked(MouseEvent e) {
            requestFocus();
            timer.start();
        }
     //provide empty definitions
        public void keyTyped(KeyEvent e) { }
        public void keyReleased(KeyEvent e) {    }
        public void mousePressed(MouseEvent e) {    }
        public void mouseReleased(MouseEvent e) {    }
        public void mouseEntered(MouseEvent e) {    }
        public void mouseExited(MouseEvent e) {    }
    
    
        public class ReboundListener implements ActionListener
        {
            public void actionPerformed(ActionEvent action)
            {
    
                for (int i = 0; i < carray.length; i++)
                {
    	    carray[i].move(moveX);
    
    
    	    if (carray[i].getX() < 0 || carray[i].getX() >= AWIDTH - carray[i].getRadius())
                {
                    carray[i].setX(0);
                    carray[i].setY(generator.nextInt(AHEIGHT));
                }
    
    
    	    repaint();
                }
            }
    	// ----------------------------------------------------
    	// actionPerformed is called by the timer -- it updates
    	// the position
    	// ----------------------------------------------------
    
        }
    }


    And here is the 'Circle' class whose objects I am using.


    Java Code:
    import java.awt.*;
    import java.util.Random;
    
    public class Circle
    {
        private int x, y;       // coordinates of the corner
        private int radius;     // radius of the circle
        private Color color;    // color of the circle
        private int width, height =300;
    
        static Random generator = new Random();
    
    
        public Circle()
        {
            radius = Math.abs(generator.nextInt(20)+5);
    
            x = 1;//modify for original plan... scrolling to the left.
            y = Math.abs(generator.nextInt(300));
        }
    
        //----------------------------------------------------------
        // Creates a circle of a given size (diameter).  Other
        // attributes are random (as described above)
        //---------------------------------------------------------
        public Circle(int size)
        {
            radius = Math.abs(size/2);
            color = new Color(Math.abs(generator.nextInt())% 16777216);
            x = 0;
            y = Math.abs(generator.nextInt())%400;
        }
    
    
        //---------------------------------------------------------
        // Draws circle on graphics object given
        //---------------------------------------------------------
        public void draw(Graphics page)
        {
            page.setColor(color);
            page.fillOval(x,y,radius*2,radius*2);
        }
    
    
        //--------------------------------------------------------
        // Shifts the circle's position 
        //--------------------------------------------------------
        public void move (int over)
        {
            x = x + over;
            //y = down;
        }
    
        // ---------------------------------------------
        // Return the x coordinate of the circle corner
        // ---------------------------------------------
        public int getX()
        {
            return x;
        }
    
        public int setX(int set)
        {
            x=set;
            return x;
        }
        public int setY(int set)
        {
            y=set;
            return y;
        }
        // ---------------------------------------------
        // Return the y coordinate of the circle corner
        // ---------------------------------------------
        public int getY()
        {
            return y;
        }
        public int getRadius()
        {
            return radius;
        }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Sorry I didn't go through the code. But as you explain, timers are not much effective for the implementations like that. Better to handle the through Threads, more effective than timers. Multiple timers are really hard to sync.

Similar Threads

  1. Regarding Applet Execution
    By makpandian in forum Java Applets
    Replies: 1
    Last Post: 03-13-2009, 01:49 PM
  2. .BAT execution error
    By hunterbdb in forum Advanced Java
    Replies: 5
    Last Post: 02-23-2009, 06:41 AM
  3. JAR execution
    By patoh in forum New To Java
    Replies: 2
    Last Post: 11-26-2008, 10:09 AM
  4. Replies: 0
    Last Post: 04-04-2008, 03:46 PM
  5. Execution cut
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 06-27-2007, 04:52 PM

Posting Permissions

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