Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    Bbob is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default How to Add a Counter to a Variable?

    Hi

    I want to add a counter to a variable and I have no idea where to start.

    What Im trying to do is, I want an image to be displayed until it reaches a number of counter.

    Im picturing the code to be like this.

    int x=0;

    if (x>3)
    {
    pic.setX(pic.getX()+50+5);
    x++;
    }

    The output Im hoping to get are images displayed side by side with a space of 5.

    As I understand the picture wont display unless it has a different variable name. So I figured if I add a counter to the variable, everytime the if statement loops it will create a new variable name.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Eranga View Post
    You have to do it in a loop, not with the if condition check-up.

    something like

    Java Code:
    	int x = 0;
    	
    	do {
    		// your code
    		x++;
    	}while(x < 3);

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    int x=0;

    if (x>3)
    You have to be careful which way the pointer > points.
    Here x starts at 0 and will not be >3

  5. #5
    Bbob is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks for the replies.


    I tried the "while" loop and only manage to display 1 image with a huge space.

    Im still having problems with adding counters to variables.


    What I want to happen is:

    X - picture

    X -5 spaces- X -5 spaces- X


    What I manage to do:

    - 10space? - X
    Last edited by Bbob; 06-04-2010 at 04:18 AM.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Is this what you really doing, or can you show your code here?

    Java Code:
    pic.setX(pic.getX()+50+5);
    I'm confusing with 50+5. Why did you really did that?

    As all the above explanation pointed within a simple loop you can do that. Give a try and let us know.

  7. #7
    Bbob is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Is this what you really doing, or can you show your code here?

    Java Code:
    pic.setX(pic.getX()+50+5);
    I'm confusing with 50+5. Why did you really did that?

    As all the above explanation pointed within a simple loop you can do that. Give a try and let us know.

    50 is the width of the image and 5 is the pixel space in between images.

    Please check my last post above yours, I edited it.

    Here my current code:

    Java Code:
    while (x<3)
    		{
    			pic.setX(pic.getX() + 50 + s);
    			x++;
    
    		}

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  9. #9
    Bbob is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    Oh sorry that was suppose to be a 5.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  11. #11
    Bbob is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    Ok. Ill do that but please help me with my main issue. How do I add a counter to a variable

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Bbob View Post
    Ok. Ill do that but please help me with my main issue. How do I add a counter to a variable
    A counter is just a variable that takes the values 0, 1, 2 ... etc. possibly changed by some asytnchronous event but that is not needed. I don't know what 'adding a counter to a variable' is supposed to mean. Variables have nothing to do with each other, that is purely conceptual and only exists in your head.

    kind regards,

    Jos

  13. #13
    Bbob is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    What Im trying to do is to generate a new variable inside the loop until it reaches a certain count.

    As I understand you cant display multiple image unless each of them has a different variable. So I figured that if I can code the "for" loop to generate a new variable and space it, I can generate multiple pictures.

    i.e.

    *-supposedly a counter

    pic* - I need something like this where * would update in the loop until it gets to a number I want.

    I know this can be done outside the for loop but I want to do it inside the loop.

  14. #14
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Variables and variable names have nothing to do with whether an image will display or not as the key is the object not the variable. If you want to display a line of pictures or a grid of pictures, then your best bit (I think, given the little we know of your requirements) is to create several JLabels each holding its own ImageIcon that holds an image. You can use a GridLayout if all the pictures are the same size and have your 5 point gaps easy enough.

    If this doesn't help, then I suggest you continue to describe more about the effect that you are trying to achieve, not the code that you are using to try to achieve it as I think we'll likely recommend that you change your entire approach.

    Or better yet, post a small compilable program that demonstrates your problem, a program that we can run unaltered.
    Last edited by curmudgeon; 06-05-2010 at 12:36 PM.

  15. #15
    Bbob is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    Ok.

    Could anyone give me an idea on how to create a loop that generates different variables?

  16. #16
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Bbob View Post
    Could anyone give me an idea on how to create a loop that generates different variables?
    Have you read a thing that I posted above? It's likely that you need to create objects, not variables. Please look here:
    http://www.catb.org/~esr/faqs/smart-questions.html#goal

  17. #17
    Bbob is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    I have read it.

    Lets just assume for a while that I wont use it to display picture. I just need an idea to get me started on how to create a loop that generates a variable.

    Anyway, Ill study what objects are and see what I learn for there.

    Thanks for the replies.

  18. #18
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Again, you don't need a variable. Again objects are the key here. Study them now.

    If you want to create objects in the loop and hold them in something, hold them in an array or a list such as an ArrayList or LinkedList.

  19. #19
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by curmudgeon View Post
    Again, you don't need a variable. Again objects are the key here. Study them now.

    If you want to create objects in the loop and hold them in something, hold them in an array or a list such as an ArrayList or LinkedList.
    The OP might even want to try a Map<String, Image> where each image is associated with a String, e.g. "var1", "var2", "var3". This all has still nothing to do with 'associating variables with counters'. Java is not an interpreted language (at the source level) and you can't create new variables during runtime. The entire concept doesn't exist.

    kind regards,

    Jos

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    create a loop that generates a variable.
    I wonder what generates means? Could it mean giving a value to a variable?

    The only way I know to "generate" a variable is at source input time. IE when you type in the source
    I think you could use reflection to generate variables at runtime, but that is way past what the OP could understand.

Page 1 of 2 12 LastLast

Similar Threads

  1. Counter
    By ks1615 in forum New To Java
    Replies: 6
    Last Post: 02-20-2009, 03:02 AM
  2. Need helps making counter
    By Lifeis2evil in forum New To Java
    Replies: 3
    Last Post: 11-26-2008, 11:58 PM
  3. Frequency Counter
    By justlearning in forum New To Java
    Replies: 0
    Last Post: 05-07-2008, 10:50 PM
  4. [SOLVED] BST Frequency Counter
    By theonly in forum Advanced Java
    Replies: 7
    Last Post: 04-29-2008, 11:33 PM
  5. Help with static variable counter
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-19-2007, 07:53 PM

Posting Permissions

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