5 Replies - 358 Views - Last Post: 19 January 2013 - 10:26 PM Rate Topic: -----

#1 Optimizer1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-January 13

Timer problem (setImage)

Posted 18 January 2013 - 05:46 AM

Hey everyone,

Here is the code I use for my timer:

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;  
import java.awt.Graphics;  
import java.awt.Font;

public class DisplayTime extends Game3
{
    
    public void displayTime()
    {
        if((System.currentTimeMillis() - WaveGame3.timeEnd) < 10000){
           int time = ((int)(System.currentTimeMillis() - WaveGame3.timeEnd)/1000);
           if(time == 1){
               GreenfootImage img = new GreenfootImage (100, 30);
               float fontSize = 20.0f;
               Font font = img.getFont();  
               font = font.deriveFont(fontSize);  
               img.setFont(font);
               img.drawString ("10", 200, 20);
               setImage(img);
           }
           if(time == 2){
               //return "9";
               GreenfootImage img1 = new GreenfootImage (100, 30);
               img1.drawString ("9", 200, 20);
               setImage(img1);
           }
           if(time == 3){
               //return "8";
           }
           if(time == 4){
               //return "7";
           }
           if(time == 5){
               //return "6";
           }
           if(time == 6){
               //return "5";
           }
           if(time == 7){
               //return "4";
           }
            if(time == 8){
               //return "3";
           }
           if(time == 9){
               //return "2";
           }
           if(time == 10){
               //return "1";
           }
        }
        //return "Wave in progress";
    }
}


Here:

               GreenfootImage img = new GreenfootImage (100, 30);
               float fontSize = 20.0f;
               Font font = img.getFont();  
               font = font.deriveFont(fontSize);  
               img.setFont(font);
               img.drawString ("10", 200, 20);
               setImage(img);


It should print "10" on the screen.. but instead nothing pops up.
Help please!

Is This A Good Question/Topic? 0
  • +

Replies To: Timer problem (setImage)

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Timer problem (setImage)

Posted 18 January 2013 - 06:10 AM

What are the values expected for (line 11):

( System.currentTimeMillis() - WaveGame3.timeEnd ) < 10000

Are you sure that will ever be true?
Was This Post Helpful? 0
  • +
  • -

#3 Optimizer1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-January 13

Re: Timer problem (setImage)

Posted 18 January 2013 - 06:12 AM

Well the wave starts after 10 seconds.. I got it to work now, i got this:

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;  
import java.awt.Graphics;  
import java.awt.Font;

public class DisplayTime extends Game3
{
    
    public void displayTime()
    {
        if((System.currentTimeMillis() - WaveGame3.timeEnd) < 10000){
           int time = ((int)(System.currentTimeMillis() - WaveGame3.timeEnd)/1000);
           if(time == 1){
               GreenfootImage img = new GreenfootImage (100, 30);
               img.drawString ("10", 2, 20);
               setImage(img);
           }
           if(time == 2){
               GreenfootImage img1 = new GreenfootImage (100, 30);
               img1.drawString ("9", 200, 20);
               setImage(img1);
           }
           if(time == 3){
               GreenfootImage img = new GreenfootImage (100, 30);
               img.drawString ("8", 2, 20);
               setImage(img);
           }
           if(time == 4){
               GreenfootImage img = new GreenfootImage (100, 30);
               img.drawString ("7", 2, 20);
               setImage(img);
           }
           if(time == 5){
               GreenfootImage img = new GreenfootImage (100, 30);
               img.drawString ("6", 2, 20);
               setImage(img);
           }
           if(time == 6){
               GreenfootImage img = new GreenfootImage (100, 30);
               img.drawString ("5", 2, 20);
               setImage(img);
           }
           if(time == 7){
               GreenfootImage img = new GreenfootImage (100, 30);
               img.drawString ("4", 2, 20);
               setImage(img);
           }
            if(time == 8){
               GreenfootImage img = new GreenfootImage (100, 30);
               img.drawString ("3", 2, 20);
               setImage(img);
           }
           if(time == 9){
               GreenfootImage img = new GreenfootImage (100, 30);
               img.drawString ("2", 2, 20);
               setImage(img);
           }
           if(time == 10){
           }
        }
    }
}


It starts showing the counter at 2.. and it skips 9.. any help on that?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Timer problem (setImage)

Posted 18 January 2013 - 09:24 AM

Add a System.out.println(time); after you calculate it
may be it is never == 1
Was This Post Helpful? 0
  • +
  • -

#5 Optimizer1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-January 13

Re: Timer problem (setImage)

Posted 18 January 2013 - 11:08 AM

Hey pbl,

That prints out 9 yes, but not on the screen like the others.. duno how to fix that though.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Timer problem (setImage)

Posted 19 January 2013 - 10:26 PM

I don't really understand what you try to do but your code is really poorly written

You repeat 9 times

GreenfootImage img = new GreenfootImage (100, 30);

and 9 times

setImage(img);

why not making them only once: one before the switch and one after the switch

And why the swith ?

Time 1 draws "10"
Time 2 draws "9"
Time 3 draws "8"
...
Time 9 draws "2"

    if(time >= 1 && time <= 9) {
         GreenfootImage img = new GreenfootImage (100, 30); 
         String str = "" + (11-time);
         img.drawString (str, 2, 20); 
         setImage(img); 
     }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1