Results 1 to 20 of 23
- 06-03-2010, 10:25 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 11
- Rep Power
- 0
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.
- 06-03-2010, 12:03 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You have to do it in a loop, not with the if condition check-up.
- 06-03-2010, 03:22 PM #3
- 06-03-2010, 04:33 PM #4
You have to be careful which way the pointer > points.int x=0;
if (x>3)
Here x starts at 0 and will not be >3
- 06-04-2010, 03:12 AM #5
Member
- Join Date
- Jun 2010
- Posts
- 11
- Rep Power
- 0
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? - XLast edited by Bbob; 06-04-2010 at 04:18 AM.
- 06-04-2010, 04:14 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Is this what you really doing, or can you show your code here?
I'm confusing with 50+5. Why did you really did that?Java Code:pic.setX(pic.getX()+50+5);
As all the above explanation pointed within a simple loop you can do that. Give a try and let us know.
- 06-04-2010, 04:31 AM #7
Member
- Join Date
- Jun 2010
- Posts
- 11
- Rep Power
- 0
- 06-04-2010, 04:35 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Is that 5 or letter s (as a variable) added to 50? In two posts you've two different things.
- 06-04-2010, 12:05 PM #9
Member
- Join Date
- Jun 2010
- Posts
- 11
- Rep Power
- 0
Oh sorry that was suppose to be a 5.
- 06-05-2010, 11:31 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Then you don't you put it as 55?
- 06-05-2010, 12:17 PM #11
Member
- Join Date
- Jun 2010
- Posts
- 11
- Rep Power
- 0
Ok. Ill do that but please help me with my main issue. How do I add a counter to a variable
- 06-05-2010, 12:22 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
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
- 06-05-2010, 12:29 PM #13
Member
- Join Date
- Jun 2010
- Posts
- 11
- Rep Power
- 0
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.
- 06-05-2010, 12:33 PM #14
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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.
- 06-05-2010, 12:37 PM #15
Member
- Join Date
- Jun 2010
- Posts
- 11
- Rep Power
- 0
Ok.
Could anyone give me an idea on how to create a loop that generates different variables?
- 06-05-2010, 12:38 PM #16
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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
- 06-05-2010, 12:43 PM #17
Member
- Join Date
- Jun 2010
- Posts
- 11
- Rep Power
- 0
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.
- 06-05-2010, 01:04 PM #18
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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.
- 06-05-2010, 01:28 PM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
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
- 06-05-2010, 03:01 PM #20
I wonder what generates means? Could it mean giving a value to a variable?create a loop that generates 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.
Similar Threads
-
Counter
By ks1615 in forum New To JavaReplies: 6Last Post: 02-20-2009, 03:02 AM -
Need helps making counter
By Lifeis2evil in forum New To JavaReplies: 3Last Post: 11-26-2008, 11:58 PM -
Frequency Counter
By justlearning in forum New To JavaReplies: 0Last Post: 05-07-2008, 10:50 PM -
[SOLVED] BST Frequency Counter
By theonly in forum Advanced JavaReplies: 7Last Post: 04-29-2008, 11:33 PM -
Help with static variable counter
By silvia in forum New To JavaReplies: 1Last Post: 07-19-2007, 07:53 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks