?
Solved

Timers

Posted on 2005-05-09
10
Medium Priority
?
307 Views
Last Modified: 2010-03-31
Hello!

Well, Ive been working up on something a little, just trying to get fimilier with a lot of different things that Java can do and I've run across something that I'd like to get some feedback on.

I've found a few sources such as:
http://java.sun.com/docs/books/tutorial/essential/threads/timer.html
http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html

and I have a few books.

Maybe its the thinking that Im getting confused the most about.  What I would like to do is to get an image(bad guy) to pop onto my 10x10 playing board and chase the player around.  I to set it as a timer so that every 30 seconds a bad guy comes out and every two seconds or so he'll move closer to the player.  However, if I do in fact make the bad guy chase the player around, isn't that leaning more on the AI side of the house?

10x10 board
|  |  |  |  |  |  |  |  |  |  |
|  |  |  |  |  |  |  |  |  |  |  x = the player
|  |  |  |  |  |  |  |  |  |B|  b = bad guy
|  |  |  |  |  |  |  |  |  |  |
|X|  |  |  |  |  |  |  |  |  |    in more depth this is what im looking at:  http://pastebin.com/281910

Any suggestions would be helpfull.  Expecially if you've found a great timer tutorial or an idea perhaps.

Regards,
Dave
0
Comment
Question by:ildave1
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13964038
A Swing Timer would seem the way to go, what problems are you having implement it?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13964054
You just need two timers really. One fixed to a BadGuyFactory, the role of which would be to produce bad guys every 30 seconds and a timer on the BadGuy, that decreased the delta between it and the good guy every 2 seconds
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:ildave1
ID: 13964150
Objects:
  I think its how to combine both tasks which is getting to me.  Using a timer to have some image come up onto the board to start chasing the good guy.

CEHJ:
  BadGuyFactory?  I've seen a lot of this 'factory' stuff, expecially when it comes to U.I. or pulling system icons. Is there some place that references this?

>> that decreased the delta between it and the good guy every 2 seconds
  0_0.... So its no AI, its just math? hah!  Sounds a bit odd to implement though .
0
 
LVL 92

Expert Comment

by:objects
ID: 13964162
You have two actions, one that would create a new bad guy and add them to the board, and another that would move an existing bad guy.
When you create a new bad guy, you would also start the timer that would up update their position every n seconds.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 13964179
The factory pattern is not as deep as it sounds. Here's a good piece on it - don't be put off by the title:

http://www.itec.uni-klu.ac.at/~harald/ds2001/rmi/factory/factory2.html

>>Sounds a bit odd to implement though.

You need to develop a strategy for calculating the distance between good and bad guy and progressively decrease it

0
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 13964542
Your action class would essentially be your factory, no need for a seperate factory from what you've said.
0
 
LVL 16

Accepted Solution

by:
gnoon earned 1600 total points
ID: 13966080
The class I wrote below is a javax.swing.Timer subclass, which receives argument of array of Square in constructor.
The class will show image itseft on the board at a random location, and move itself to a random direction step by 1.

Try to use it inside your GUI class, you may get some idea with it :)
Just add this code at the end of constructor of GUI class

    new BadGuy(square).walk();
    new BadGuy(square).walk();


// BadGuy.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class BadGuy extends javax.swing.Timer
{

    public static final Icon PIC = javax.swing.plaf.metal.MetalIconFactory.getTreeFolderIcon();
    public static final int MOVE_DELAY = 700;

    public BadGuy(Square[][] square)
    {
        super(MOVE_DELAY, new Moving(square));
    }

    public void walk()
    {
        // Start timer,
        // Moving.actionPerformed() will be called for every $MOVE_DELAY miliseconds.
        start();
    }
}

class Moving implements ActionListener
{
    private final int MAX_ROW = 9;
    private final int MAX_COL = 9;
    private final int MIN_ROW = 0;
    private final int MIN_COL = 0;
    private Square[][] square;
    private int row=-1, col=-1;

    public Moving(Square[][] square)
    {
        this.square = square;
    }

    private int random(int min, int max)
    {
        Random r = new Random(Math.abs(new Random().nextInt()));
        int i = -1;
        do { i = Math.abs(r.nextInt())%(max+1); }while(i<min || i>max);
        return i;
    }

    // This method is called for every N miliseconds (see BadGuy.MOVE_DELAY).
    public void actionPerformed(ActionEvent e)
    {
        if(square == null) return;
        int oldRow = row;
        int oldCol = col;
        if(row < 0)
            row = random(MIN_ROW,MAX_ROW);
        if(col < 0)
            col = random(MIN_COL,MAX_COL);

        boolean verticalDirect = (random(0,Integer.MAX_VALUE)%2)==0;
        int step = ((random(0,Integer.MAX_VALUE)%2)==0) ? 1 : -1 ;
        if(verticalDirect)
            row += step;
        else
            col += step;

        if(row < 0)
            row = MAX_ROW+row+1;
        else
            row = row%(MAX_ROW+1);
        if(col < 0)
            col = MAX_COL+col+1;
        else
            col = col%(MAX_COL+1);

        // Reset image at current location if available
        if(oldRow >= 0 && oldCol >= 0)
            square[oldRow][oldCol].setPic(javax.swing.plaf.metal.MetalIconFactory.getTreeHardDriveIcon());

        // Show bad guy image at the new location on the board
        square[row][col].setPic(BadGuy.PIC);
    }
}
0
 
LVL 3

Author Comment

by:ildave1
ID: 13970898
I have a great idea for that!

Thanks gnoon!

Regards,
Dave
0
 
LVL 16

Expert Comment

by:gnoon
ID: 13974406
Thanks :-)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question