• 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

how to use drawImage in an object

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
help!
im a teacher with a java class (1st time) and kids love using drawImage. now i want them to put it in individual classes. but now the drawImage doesnt work. any ideas? thanks, jeff
in an object
public void draw(Graphics g)
{
Image smallSub=getImage(getCodeBase(),"LittleHopper.jpg");
g.drawImage(smallSub,230+subX, 240+subY, this);
}
which is initialized, then called in main interface:
public void paint(Graphics g)
{

player.draw(g);
}
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It is not efficient to load a new image with each call to draw.
Also, the getImage method returns immediately, not waiting for the image to finish loading. So if LittleHopper has any size to it you won't get anything to show up from the call to draw. Not right away anyway.

A more efficient way is to load the image only once, in or via a method call from either init (Applet) or a constructor (class, such as Player). Looks like you are working with an older sdk so I used MediaTracker in the example. Although you might get by fine without a MediaTracker it is an option and will ensure that the image is fully loaded before you attempt to render it.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Swing / JFC / AWT forum...
 
Jeff Borland
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Craig Wood,
thanks so much for your reply. my problem is not w/ the image loading in a class (yes, normally i load my image in the init, i was just shrotening the code for demo purposes).
my problem is i have two classes and having the second one drawImages. for example
MAIN GAME - calls SUBMARINE
when another iteration of the game happens, i want the drawing done in a method of submarine like draw. It works fine for all none images (jpg,gif). is there a way to do this you know?
thanks so much for any help.
also thanks for your last reply, it was incredibly extensive.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the applet code below we could just as well load the single image in the ImageDrawingPanel class and send a reference along in each Duke constructor:

Or we could leave it as is and send a (potentially different) fileName string into each new Duke constructor. In this case we would also have to change moveDuke to keep the dukes inside their boundries:

 
Jeff Borland
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ken,
thanks so much, thats works great...
im so psyched.
once again, forums have restored my faith in the goodness of man. sounds like an exaggeration, but it is full of truth.
thanks again,
my class will be very appreciative
below is the link of my class if you're curious:
http://www.drborland.com/deering/java/index.htm
 
Jeff Borland
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ken,
quick? i went to school today and found out imageio lib does not exist in sdk1.3 (the only thing that would load on all the comps).
can i just copy my folder from 1.4 onto those machines or is there a better way?
(so it gave an error importing the javax.imageio
thanks,
jeff
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic