• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help please

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

And here's the main
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

  1. Choose a current question from the collection of questions.

  2. Get the image from the question and use it to set the image for the ImagePanel.


  3. Repaint.

  4. Respond to user input.

  5. Repeat as necessary.


  6. HTH,
    -Nate
 
Brian Snyder
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Brian Snyder
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Nate!!
I got it working already. My probplem what my design.
Here's how I feel now.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic