Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with displaying a text from a method

    Hello, i am trying to display a text from a method but i cant figure out how to call it within the text command. I am looking to display the counter's current (four-digit) value in the center of the window using the Counter's value() method. Here is my code so far, any tips would be appreciated, thank you.
    Code:
     class Counter
    {
      private int count;
      
      public Counter()
      {
        int count = 0;
      }
      
      public void increment()
      {
        count = count + 1;
        if(count > 9999){
          count = 0;
        }
      }
        
        public void reset()
        {
         count = 0;
        }
      
        public String value ()
        {
          if(count <10)
          {
            return "000" + count;
          }
          if(count <100)
          {
            return "00" + count;
          }
          if(count < 1000)
          {
            return"0" + count;
          }
          return "" + count;
        }
        
    }
    
    void setup()
    {
      size(400, 400);
    }
    
    void draw()
    {
      background(150);
      textAlign(CENTER);
      text(counter, 200, 120);
    
    }
    Last edited by VIPStephan; 12-12-2013 at 09:07 AM. Reason: fixed code BB tag

  • #2
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
     class Counter
    {
    private int count;
    
    public Counter()
    {
    int count = 0;
    }
    
    public void increment()
    {
    count = count + 1;
    if(count > 9999){
    count = 0;
    }
    }
    
    public void reset()
    {
    count = 0;
    }
    
    public String value ()
    {
    if(count <10)
    {
    return "000" + count;
    }
    if(count <100)
    {
    return "00" + count;
    }
    if(count < 1000)
    {
    return"0" + count;
    }
    return "" + count;
    }
    
    }
    
    void setup()
    {
    size(400, 400);
    }
    
    void draw()
    {
    background(150);
    textAlign(CENTER);
    text(counter, 200, 120);
    
    }

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,695
    Thanks
    6
    Thanked 1,011 Times in 984 Posts
    Moved.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Your missing a lot of data here. It looks to me that the last two methods are plunked in from another class (an extension of a Component perhaps), but there is no indication what the counter is.
    My suspicion is that the counter is a type of Counter. If so, then you can simply call counter.value(). You can also simplify the Counter's method value by using a print formatter instead (ie: return String.format("%04d", count);).
    Last edited by Fou-Lu; 12-12-2013 at 09:33 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •