| | |
Help! Adding an Image through a method.
![]() |
•
•
Join Date: Oct 2009
Posts: 8
Reputation:
Solved Threads: 0
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...
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.
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)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Panels { static void floorpanel1(int x, int y, Graphics g) { Image fp1 = Toolkit.getDefaultToolkit().getImage("graphics/Scrapfloor1.gif"); Rectangle hitground = new Rectangle(x, y, 60, 1); g.drawImage(fp1,x,y, 60, 60,this); } }
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.
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 146
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.
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.
•
•
Join Date: Oct 2009
Posts: 8
Reputation:
Solved Threads: 0
0
#9 24 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!
Does anyone here have experience with this situation? Anything that shows JLabels or ImageIcons being called from another program helps!
![]() |
Similar Threads
- Adding an Image to a JPanel (Java)
- adding an image to a java code (Java)
Other Threads in the Java Forum
- Previous Thread: JAR works bue EXE doesn't
- Next Thread: Final Year Project :(Bsc(Hons) in Computing)
| Thread Tools | Search this Thread |
.net 6 addressbook ajax apple applet application applications array automation back binary blackberry bluetooth business button c++ class clone code collections component convert database design desktop development dice disk eclipse eclipsedevelopment error file firefox forms fractal froglogic g2one game google gui gwt html hyper idea image images integer intellijidea8 java javafx javascript jetbrains jpanel julia linux loops mac macosx method methods microsoft mobile multimedia myregfun mysql netbeans newbie nonstatic notdisplaying openjavafx oriented php platform problem programming python rails random recursion recursive ria search service set software sort sorting ssl string sun swing swt threads tree ubuntu update upload web windows






