Help! Adding an Image through a method.

Reply

Join Date: Oct 2009
Posts: 8
Reputation: CuteCat is an unknown quantity at this point 
Solved Threads: 0
CuteCat CuteCat is offline Offline
Newbie Poster

Help! Adding an Image through a method.

 
0
  #1
Oct 9th, 2009
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...

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark
 
0
  #2
Oct 9th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: CuteCat is an unknown quantity at this point 
Solved Threads: 0
CuteCat CuteCat is offline Offline
Newbie Poster
 
0
  #3
Oct 9th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark
 
0
  #4
Oct 9th, 2009
Can yu pass it as an extra parameter when you call floorpanel1?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: CuteCat is an unknown quantity at this point 
Solved Threads: 0
CuteCat CuteCat is offline Offline
Newbie Poster
 
0
  #5
Oct 9th, 2009
Extra parameter...? I'm sorry, I don't quite understand. How would that help? What would it do?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark
 
0
  #6
Oct 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: CuteCat is an unknown quantity at this point 
Solved Threads: 0
CuteCat CuteCat is offline Offline
Newbie Poster
 
0
  #7
Oct 9th, 2009
I see... I haven't used ImageObserver, so I'll try "null", but if it doesn't work I'll try the IO. Thanks!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: CuteCat is an unknown quantity at this point 
Solved Threads: 0
CuteCat CuteCat is offline Offline
Newbie Poster
 
0
  #8
Oct 15th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: CuteCat is an unknown quantity at this point 
Solved Threads: 0
CuteCat CuteCat is offline Offline
Newbie Poster
 
0
  #9
25 Days Ago
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!
Reply With Quote Quick reply to this message  
Reply

Tags
image, java, jpanel, method, swing

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC