944,120 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1909
  • Java RSS
Oct 9th, 2009
0

Help! Adding an Image through a method.

Expand Post »
I don't know what to do... I've been trying for weeks to figure out the problem. You see, I'm supposed to make a game, as a project for school. Since it's going to be a rather large game, I decided not to flood the main file with a bunch of images and such. Instead, I tried to put the images in another program, as I've learned that the "main" program can call upon the smaller ones.

But... It won't read the image! If I copy the code to the main file then it works perfectly, so why does it make such a fuss when put into another file? I don't know where to turn, please help me even if it's something stupidly easy...

Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class Panels
  6. {
  7. static void floorpanel1(int x, int y, Graphics g)
  8. {
  9.  
  10. Image fp1 = Toolkit.getDefaultToolkit().getImage("graphics/Scrapfloor1.gif");
  11. Rectangle hitground = new Rectangle(x, y, 60, 1);
  12.  
  13. g.drawImage(fp1,x,y, 60, 60,this);
  14. }
  15. }

This is the code for the side-program. I'm still learning as I go along... It's called upon to be in the "paint" part of the main program.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CuteCat is offline Offline
10 posts
since Oct 2009
Oct 9th, 2009
0
Re: Help! Adding an Image through a method.
If you put that code in your main class then the "this" on line 13 refers to an instance of the main class. When the same code is in class Panels, "this" refers to an instance of Panel. So maybe that's why the drawimage is doing something different?
Featured Poster
Reputation Points: 1938
Solved Threads: 953
Posting Expert
JamesCherrill is offline Offline
5,809 posts
since Apr 2008
Oct 9th, 2009
0
Re: Help! Adding an Image through a method.
That's most likely it - but what do I replace it with? If I just remove it, it won't be recognized. If I try putting in the name of the main program, gameinit, then it thinks it's a program and says it can't recognize it...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CuteCat is offline Offline
10 posts
since Oct 2009
Oct 9th, 2009
0
Re: Help! Adding an Image through a method.
Can yu pass it as an extra parameter when you call floorpanel1?
Featured Poster
Reputation Points: 1938
Solved Threads: 953
Posting Expert
JamesCherrill is offline Offline
5,809 posts
since Apr 2008
Oct 9th, 2009
0
Re: Help! Adding an Image through a method.
Extra parameter...? I'm sorry, I don't quite understand. How would that help? What would it do?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CuteCat is offline Offline
10 posts
since Oct 2009
Oct 9th, 2009
0
Re: Help! Adding an Image through a method.
Assuming you are calling floorpanel1 from your main class, you can change the signature of the method to
static void floorpanel1(int x, int y, Graphics g, ImageObserver io)
and use the extra parameter in the drawimage call
g.drawImage(fp1,x,y, 60, 60,io);

When you call it from your main class you can use "this" as the fourth parameter.

ps: If you're not actually using the ImageObserver, you may simply be able to use
g.drawImage(fp1,x,y, 60, 60, null);
... but I haven't tried that myself.
Last edited by JamesCherrill; Oct 9th, 2009 at 2:22 pm.
Featured Poster
Reputation Points: 1938
Solved Threads: 953
Posting Expert
JamesCherrill is offline Offline
5,809 posts
since Apr 2008
Oct 9th, 2009
0
Re: Help! Adding an Image through a method.
I see... I haven't used ImageObserver, so I'll try "null", but if it doesn't work I'll try the IO. Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CuteCat is offline Offline
10 posts
since Oct 2009
Oct 15th, 2009
0
Re: Help! Adding an Image through a method.
Update! Although the program itself works if I enter "null" instead of "this", the graphic itself won't show up. I imagine that I told it to upload the image to nowhere...

I tried ImageObserver too, but it said it couldn't find it. Do I need a certain package, or add code to the main program?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CuteCat is offline Offline
10 posts
since Oct 2009
Nov 2nd, 2009
0
Re: Help! Adding an Image through a method.
Here again... Please help. Through the help of a friend, I've switched from Images to JLabels, but the problems continue! Rather than staying where I tell them to be, the images stubbornly stay at the top center of the screen. Why is this? Not even my friend knows... I suspect that it has to do with the fact that the JLabels are from a side program, but I don't know what can be done about it.

Does anyone here have experience with this situation? Anything that shows JLabels or ImageIcons being called from another program helps!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CuteCat is offline Offline
10 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: JAR works bue EXE doesn't
Next Thread in Java Forum Timeline: Need to set header and footer in all the pages while printing from a jsp





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC