I currently have a Frame will a bunch of panels. I am trying to paint an image to one of those panels from another class file. How can I do this without Swing? And if It can not be done without swing how can I do it then. I'm postin ght two class files files below. I received some great help from Nathan and Bryan about painting to a Canvas or Frame, but this is not what I need. I need to add an image from one class file to a preexisting Panel within a frame.
Brian, Let me see if I understand what you are trying to do... It looks like your application will have a set of math questions, which have an associated image that you want to paint on a panel in your main application. However, instead of storing the image in your main application, you want to store it in your question object. (Am I right so far?) If this is correct all you would have to do is this, create your question file like so :
Then in your main class you would have some collection of questions, like an array or vector of questions, and you would set them all up in the beginning of your program by loading each question with its corresponding image ( if you name your images something like "ques0.gif", "ques1.gif", etc. this makes it easier to do with a loop, you can just do something like this :
) Once you have all the questions loaded with images, you can choose one question to be the current question displayed and then just use its getImage() method to get its image... you can have that image paint to the Panel or the Frame... One way to do this is to make a subclass of Panel ( which I will call ImagePanel ) that stores a image and paints it :
Then use this ImagePanel in your main application to display the image. The flow of the program would go something like this:
Choose a current question from the collection of questions.
Get the image from the question and use it to set the image for the ImagePanel.
Respond to user input.
Repeat as necessary.
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Joined: Feb 03, 2001
Nate, I've been following your lead and have been successful loading five questions into an array. The only problem is when I go to add the images as a handle to the array, I'm getting a null Pointer exception. The gifs are in the correct folder. Here's my code... Thanks... You've really been a big help so far.
Also, could you explain how an array of classes is created when the class is not one when it was created. Here's your code. The part that is giving me the nullpointexception is questGen[i].setImage(tempImage); Thanks!! [This message has been edited by Brian Snyder (edited March 02, 2001).] [This message has been edited by Brian Snyder (edited March 02, 2001).]
Brian, You have created the array, but you have not created the items inside the array yet... Here is what you would do instead:
An array is a special type of object in Java that contains a number of references to objects of the type specified when the array was created... however, the original references are set to null for any type of object ( arrays of primitives are all set to their 0 values ). The array is not null, but the objects inside it are... that is why you were getting the exception. HTH, -Nate [This message has been edited by Nathan Pruett (edited March 06, 2001).]
Joined: Feb 03, 2001
Thanks Nate!! I got it working already. My probplem what my design. Here's how I feel now.