The Artima Developer Community
Sponsored Link

Java Answers Forum
*********NEED HELP WITH TIMERS************

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

*********NEED HELP WITH TIMERS************ Posted: May 20, 2002 10:04 PM
Reply to this message Reply
Advertisement
I am making this game, and it needs a couple timers, I am having trouble with the syntax.
The game is just a little blue square (canvas()) hopping around the screen in an applet. You click on it and it changes color. I need a timer to make the game stop after 1 minute. And I have no idea where to start.Can anyone help with this????

I also have another problem, when I make it so that there are two creatures hopping around the screen, if I click on one, they both stop, as opposed to just one stopping, and the other moving.Anyone help with this?????

Here is some code: This is the creature Canvas.

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
import javax.swing.Timer;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.image.*;

public class Creature extends Canvas
{
private Timer timer;
protected Applet applet;
private int hitCounter=0;
private long pauseTime;

private final int x=(int)(Math.random()*480+1);
private final int y=(int)(Math.random()*480+1);

private final int CANVAS_WIDTH = (int)(Math.random()*100+50);
private final int CANVAS_HEIGHT = (int)(Math.random()*100+50);
private Image img;

public Creature(Applet a)//Allows user to set the rate of Creature as well.
{
applet=a;
int DELAY=(int)(Math.random()*1000+500);//Num=whatever you want it to be.
timer=new Timer(DELAY,new CreatureActionListener());
setLocation(x,y);
setSize (CANVAS_WIDTH, CANVAS_HEIGHT);
addMouseListener(new CreatureMouseListener());
startCreature();

}


public void paint(Graphics screen)
{
screen.setColor(Color.blue);
screen.fillRect(x,y,CANVAS_WIDTH, CANVAS_HEIGHT);
}


//-----------------------------------------------------------------------
//Sets up the MouseListener for the creature.
//-----------------------------------------------------------------------
private class CreatureMouseListener implements MouseListener
{
public void mousePressed (MouseEvent event)
{
if(timer.isRunning())//Checks to see that the timer is running.
{

hitCount();//hitCounter++;(Counts the number of hits).
applet.showStatus("You got me! "+getHits()+" times.");
pauseTime=System.currentTimeMillis();

while(System.currentTimeMillis()<= pauseTime+2300)
{
stopCreature();//timer.stop();
setBackground (Color.gray);


}
move();
}
}
//Empty definitions for unused methods.
public void mouseEntered (MouseEvent event) {}
public void mouseExited (MouseEvent event) {}
public void mouseClicked (MouseEvent event) {}
public void mouseReleased (MouseEvent event) {}
}


//-----------------------------------------------------------------------
//Sets up ActionListener for creature.
//-----------------------------------------------------------------------
private class CreatureActionListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
//stopCreature();//timer.stop();
move();
}
}


//-----------------------------------------------
public void startCreature()
{
applet.showStatus("Catch me if you can!");
if(!(timer.isRunning()))//(timer.isRunning==false).
timer.start();
}


public void stopCreature()
{
if(timer.isRunning())
timer.stop();
}


public void hitCount()
{
hitCounter++;
}


public void move()
{
setBackground (Color.blue);
stopCreature();
int newX=(int)(Math.random()*480+1);
int newY=(int)(Math.random()*480+1);
setLocation(newX,newY);
repaint();
startCreature();
}



public int getHits()
{
return hitCounter;
}

}

Heres some more code: This is the applet.

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
import java.awt.*;
import java.applet.*;
import Creature;
import java.awt.event.*;
import javax.swing.Timer;
import java.awt.*;
import java.util.Vector;

public class CreatureCatcher2 extends Applet
{
Creature PPP;
Creature GGG;
Vector creatureVector=new Vector();
private int creatureCounter=0;
private int missCounter=0;

private Timer runtimer;
private final int timer_interval=100;
private long time_length=System.currentTimeMillis();

private Applet applet;
private Graphics screen;


public void init()
{
PPP=new Creature(this);
GGG=new Creature(this);
runtimer=new Timer (timer_interval, new AppletActionListener());
addMouseListener(new AppletMouseListener());
setBackground(Color.gray);
setSize(700,700);
add(PPP);
add(GGG);
/*for(int i=0;i<timer_interval;i++)
{
for(int count=0;count<5;count++)
{
Creature ooo=new Creature(this);
creatureVector.addElement(ooo);
ooo.startCreature();
}
}*/
PPP.startCreature();
GGG.startCreature();

}

public void paint(Graphics screen)
{
screen.drawString("You have "+missCounter+" misses.",20,20);

}


private class AppletMouseListener implements MouseListener
{
public void mousePressed (MouseEvent event)
{
missCounter++;//(Counts the number of misses).
repaint();
}

//Empty definitions for unused methods.
public void mouseEntered (MouseEvent event) {}
public void mouseExited (MouseEvent event) {}
public void mouseClicked (MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
}

private class AppletActionListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
creatureCounter++;
}
}

}

Topic: web browser Previous Topic   Next Topic Topic: Classes

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use