This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes how to use drawImage in an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to use drawImage in an object" Watch "how to use drawImage in an object" New topic
Author

how to use drawImage in an object

Jeff Borland
Greenhorn

Joined: May 05, 2004
Posts: 9
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);
}


Jeff C Borland
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535

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.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Swing / JFC / AWT forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jeff Borland
Greenhorn

Joined: May 05, 2004
Posts: 9
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

Joined: Jan 14, 2004
Posts: 1535
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

Joined: May 05, 2004
Posts: 9
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

Joined: May 05, 2004
Posts: 9
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to use drawImage in an object
 
Similar Threads
BufferedImage(s)
Casting problem. Image to BufferedImage
Draw bitmap in Canvas
using graphics object
JTextArea image