-
April 17th, 2011, 12:24 PM
#1
Help with accessing variables in other classes
I am relatively new to programming and I am having trouble with my JApplet program. I am looking for a way to get the value for a variable in a different class. I want to use the value of a textField that is created in one class as a cap for a 'for loop' in another class. When the user presses a button, the program should draw a number of pictures equal to the number in the text field.
The program layout is somewhat like this:
public class sampleProgram extends JApplet implements ActionListener{
... JTextField sampleField;
... JButton sampleButton;
... int numPictures;
... public void init{
...... PaintSurface sampleSurface = new PaintSurface();
...... this.add(sampleSurface);
...... JPanel samplePanel = new JPanel();
...... JButton sampleButton = new JButton("Draw");
...... JButton.addActionListener(this)
...... JTextField sampleField = new JTextField(5);
...... samplePanel.add(sampleButton);
...... samplePanel.add(sampleField);
...... this.add(samplePanel);
...... //Scheduled pool thread executor
... }
... public void actionPerformed(actionEvent e){
...... if(e.getSource == sampleButton){
......... numPictures = Integer.parseInt(sampleField.getText());
...... }
... }
}
class AnimationThread(){
... //Animation thread stuff(with repaint())
}
class PaintSurface extends JComponent{
... //import image
... int numberOfPictures = ??????
... public void paint(Graphics g);
...... for(int i = 0; i < numberOfPictures; i++){
......... g.drawImage(image,0,0,this);
...... }
... }
}
So, I need a way for numberOfPictures in the PaintSurface class to access numPictures in the sampleProgram class. I don't know if this is a simple command or if I will need to rewrite everything I have. I have tried using interfaces and objects to get the information but I am not very familiar with either, so I haven't had much luck. Any help would be much appreciated!
-
April 17th, 2011, 12:56 PM
#2
Re: Help with accessing variables in other classes
There are three main ways to access values in another class object. You can access the variable directly in the other object, or call a method on the object that returns a value, or have the other object pass the value to the target class as a method parameter. Providing access to the raw data is typically undesirable; providing a getXxxx() method to return a suitable value, or passing the value as a parameter is preferred.
If the PaintSurface class is a stand-alone class (i.e. it could be used elsewhere) and needs the number of images to work with, its constructor can take the number of images as a parameter. The SampleProgram class can then pass the number of images to the PaintSurface class when it constructs it.
But where does PaintSurface get its image from?
In theory, there is no difference between theory and practice, but not in practice...
Anon.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
April 17th, 2011, 02:50 PM
#3
Re: Help with accessing variables in other classes
Thank you for the reply. I used a toolkit object and assigned it a bitmap image from my computer (not a url). I have not tried your solution yet, but you are saying that I should include the sampleProgram variable as a parameter when I create the new PaintSurface?
-
April 17th, 2011, 03:31 PM
#4
Re: Help with accessing variables in other classes
Just tried it and it works perfectly! Thanks so much!
-
April 18th, 2011, 03:22 AM
#5
Re: Help with accessing variables in other classes
 Originally Posted by shindig
Just tried it and it works perfectly! Thanks so much!
You're welcome.
That is what a constructor is for - to initialise a class object to a valid state. Any essential information it can't get for itself must be passed in to it.
If you're not failing every now and again, it's a sign you're not doing anything very innovative...
Woody Allen
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|