问题描述
能有人帮一个新手拿到他的比赛计时器? :P
我一直在试图为两个最后一天的各种方法,我仍然不能得到它的工作。是否有人在这里有一个解决方案?
基本上,我有一个游戏,目的是收集的内容6在最快的时间。所以,我当然希望从0开始计数,用户可以跟踪他是怎么回事的计时器显示。
下面是相关类的骨架 -
公共类起点扩展的Applet实现Runnable { 任务T =新任务();
菜单菜单=新菜单();
INT NUM;
公共静态枚举状态{
菜单,
游戏,
}; 公共静态状态= STATE.MENU; 公共无效的init(){
的setSize(800,600);
} 公共无效的start(){ 如果(NUM 2){
线程线程=新主题(本);
thread.start();
NUM ++;
}
} 公共无效的run(){ 如果(国家== STATE.GAME){
而(真){
重绘(); 尝试{
视频下载(17);
}赶上(InterruptedException的E){
e.printStackTrace();
}
}
}否则如果(国家== STATE.MENU){ 而(真){
重绘();
尝试{
视频下载(17);
}赶上(InterruptedException的E){
e.printStackTrace();
}
}
} } 公共无效漆(图形G){ 如果(国家== STATE.GAME){ ball.paint(G); }否则如果(国家== STATE.MENU){
menu.render(G);
}
}
}公共类任务扩展的TimerTask {
INT秒;
INT分钟;
公共字符串的时间; 公共无效的run(){ 秒++; 如果(秒== 60){
秒= 0;
分钟++;
} 字符串s =将String.valueOf(秒);
串m =将String.valueOf(分钟); 如果(秒-1,10){
时间=(M +:0+ S);
}其他{
时间=第(m +:+ S);
}
}
}公共类开始{ 私有静态定时ourClock;
私有静态的TimerTask ourTask; 公共静态无效的更新(){
ourClock =新的Timer();
ourTask =新任务();
ourClock.scheduleAtFixedRate(ourTask,1000,1000);
}
}
我知道这是很多,但是如果有人可以帮我,以显示在我的小程序中的任务类的字符串时间,这将是非常有益的。
谢谢!
P.S请注意,这是一个小程序不是一个JApplet的。 : - /
另外,如果你能想到的,不使用当前类我有一个办法,去了!我接受任何建议,我只想让这件事完成了,我已经工作好几天......
这是错误消息,如果我只是尝试画字符串时间
异常螺纹AWT-EventQueue的-1显示java.lang.NullPointerException:字符串为空
在sun.java2d.SunGraphics2D.drawString(SunGraphics2D.java:2880)
在Start.paint(Start.java:23)
在StartingPoint.paint(StartingPoint.java:229)
在StartingPoint.update(StartingPoint.java:215)
在sun.awt.RepaintArea.updateComponent(RepaintArea.java:255)
在sun.lwawt.LWRepaintArea.updateComponent(LWRepaintArea.java:47)
在sun.awt.RepaintArea.paint(RepaintArea.java:232)
在sun.lwawt.LWComponentPeer.handleJavaPaintEvent(LWComponentPeer.java:1312)
在sun.lwawt.LWComponentPeer.handleEvent(LWComponentPeer.java:1196)
在java.awt.Component.dispatchEventImpl(Component.java:4959)
在java.awt.Container.dispatchEventImpl(Container.java:2292)
在java.awt.Component.dispatchEvent(Component.java:4705)
在java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
在java.awt.EventQueue.access $ 400(EventQueue.java:97)
在java.awt.EventQueue中的$ 3.run(EventQueue.java:697)
在java.awt.EventQueue中的$ 3.run(EventQueue.java:691)
在java.security.AccessController.doPrivileged(本机方法)
在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:75)
在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:86)
在java.awt.EventQueue中的$ 4.run(EventQueue.java:719)
在java.awt.EventQueue中的$ 4.run(EventQueue.java:717)
在java.security.AccessController.doPrivileged(本机方法)
在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:75)
在java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
在java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
在java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
在java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
我认为你是从错误的角度看这一点。
在Java中,你可以把现有的类中创建一个内部类。这将使你的实例化类顶嘴到它的父。
这其实是(从我有限的经验),在Java中使用定时器的标准。
公共类开始{ 私有静态定时ourClock;
公共静态字符串的时间;
公共静态无效的更新(){
ourClock =新的Timer();
ourClock.scheduleAtFixedRate(新的TimerTask(){
公共无效的run(){
INT秒;
INT分钟; 秒++; 如果(秒== 60){
秒= 0;
分钟++;
} 字符串s =将String.valueOf(秒);
串m =将String.valueOf(分钟); 如果(秒-1,10){
时间=(M +:0+ S);
}其他{
时间=第(m +:+ S);
}
},1000,1000);
}
}
您可以找到这个@几个很好的例子。
虽然,这个问题是怎么样的其他问题重复的,我认为它是更多的角度,还是范式一个问题,Java有其他语言没有的。通常情况下,使用内部或嵌套类都是真的是你看到的东西在日常的Java的。但是,它被击中或C ++和C#错过。在有些国家,它被认为是非常糟糕的做法(PHP),和其他人则只是简单地不允许(Objective-C的)。
有了这样说,只是因为一个人不喜欢,这并不意味着它不应该被使用,只是你应该检讨为什么要使用它,以及它如何适应您的环境。
can someone please help a newbie to get a timer on his game? :PI'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.
public class Start {
private static Timer ourClock;
public static String time;
public static void update(){
ourClock = new Timer();
ourClock.scheduleAtFixedRate(new TimerTask(){
public void run() {
int seconds;
int minutes;
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);
}
},1000,1000);
}
}
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.
这篇关于实现一个定时器到一个applet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!