can someone please help a newbie to get a timer on his game? :P I've been trying various approaches for the last day or two and I still can't get it working. Does anyone here have a solution?
Basically, I have a game where the aim is to collect 6 of the items in the fastest time. So of course I want a timer display that counts up from 0 where the user can keep track of how he is going.
Here are skeletons of the relevant classes-
public class StartingPoint extends Applet implements Runnable{
Task t = new Task();
Menu menu = new Menu();
int num;
public static enum STATE{
MENU,
GAME,
};
public static STATE State = STATE.MENU;
public void init() {
setSize(800, 600);
}
public void start() {
if(num < 2){
Thread thread = new Thread(this);
thread.start();
num++;
}
}
public void run() {
if (State == STATE.GAME) {
while (true) {
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} else if (State == STATE.MENU) {
while (true) {
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void paint(Graphics g) {
if (State == STATE.GAME) {
ball.paint(g);
} else if (State == STATE.MENU) {
menu.render(g);
}
}
}
public class Task extends TimerTask {
int seconds;
int minutes;
public String time;
public void run() {
seconds++;
if (seconds == 60) {
seconds = 0;
minutes++;
}
String s = String.valueOf(seconds);
String m = String.valueOf(minutes);
if (seconds < 10) {
time = (m + ":0" + s);
} else {
time = (m + ":" + s);
}
}
}
public class Start {
private static Timer ourClock;
private static TimerTask ourTask;
public static void update(){
ourClock = new Timer();
ourTask = new Task();
ourClock.scheduleAtFixedRate(ourTask,1000,1000);
}
}
I know it's a lot, but if someone could please help me to display the String "time" from the task class on my applet, it would be really helpful.
Thanks!
P.S note that it's an Applet not a JApplet. :-/ Also, if you can think of a way that doesn't use the current classes I have, go for it! I'm open to any suggestions, I just want to get this thing finished, I've been working on it for days...
This is the error message if I just try to paint the string "time"
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException: String is null
at sun.java2d.SunGraphics2D.drawString(SunGraphics2D.java:2880)
at Start.paint(Start.java:23)
at StartingPoint.paint(StartingPoint.java:229)
at StartingPoint.update(StartingPoint.java:215)
at sun.awt.RepaintArea.updateComponent(RepaintArea.java:255)
at sun.lwawt.LWRepaintArea.updateComponent(LWRepaintArea.java:47)
at sun.awt.RepaintArea.paint(RepaintArea.java:232)
at sun.lwawt.LWComponentPeer.handleJavaPaintEvent(LWComponentPeer.java:1312)
at sun.lwawt.LWComponentPeer.handleEvent(LWComponentPeer.java:1196)
at java.awt.Component.dispatchEventImpl(Component.java:4959)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
I think you are looking at this from the wrong perspective.
In Java, you can create an inner class within your existing class. This will allow your instanced class to talk back to it's parent.
It is actually (from my limited experience) the standard for using Timers in Java.
You can find a few good examples of this @
And while, this question is kind of a duplicate of the other questions, I think it is more of an issue with perspective or paradigms that Java has that other languages don't. Typically the use of inner or nested classes are really something you see in day-today Java. However, it is hit or miss with C++ and C#. In some, it is considered really bad practice (PHP), and others it is just simply not allowed (Objective-C).
With that being said, just because one person doesn't like it doesn't mean it shouldn't be used, just that you should review why you are using it, and how it fits into your environment.