• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass arguments from main to the paint method.  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to take command line arguments and pass them to a paint method.
This is a test program that will just draw some equations.
How can I get the input array clinputs[] to be used in public void paint( Graphics g) ?

thank you
cwc
1885

 
Sheriff
Posts: 4294
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could make clinputs[] a class member.
 
Sheriff
Posts: 11496
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or change your LinePlot constructor to accept an array of int. you should also refactor that duplication out.
 
Craig Coleman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You could make clinputs[] a class member.


Thank you for your reply. I've tried that at the top level but I must have done it in the wrong location.
Where and how would I declare the clinputs[].
 
Knute Snortum
Sheriff
Posts: 4294
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class members are within the class (obviously) but outside of any method definition.

 
Junilu Lacar
Sheriff
Posts: 11496
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By putting these values in a class variable, you are saying that all LinePlot objects will have the same inputs. Would you ever create multiple LinePlot objects that had different inputs? What if you were getting your inputs from somewhere else, like reading them from a file instead of getting them from the command line?
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If your inputs are to be passed as an array of numbers, you should be able to do this sort of thing with a constructor callGet your LinePlot class working so you can instantiate it like that. Then worry about how you are going to pass the details from elsewhere.
And as Junilu says, try making the array a private non‑static field.
 
Craig Coleman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Class members are within the class (obviously) but outside of any method definition.



This did the trick . Thank you! I did not declare it as a static previously.
 
Craig Coleman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

If your inputs are to be passed as an array of numbers, you should be able to do this sort of thing with a constructor callGet your LinePlot class working so you can instantiate it like that. Then worry about how you are going to pass the details from elsewhere.
And as Junilu says, try making the array a private non‑static field.


I like this : LinePlot lines = new LinePlot(new int[]{1, 2, 3, 4, 5, 6, 7, 8});
I need to get this to work. Thanks for the lines.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you made that array a static field? It is a mistake to make something static in order to get it to compile in 99.9% of occasions. Take the static marker off and don't try to initialise it from the main method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!