| | |
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 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!
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 actuate add ajax apple applet array automation bank binary birt button c++ class clear code compile compiler component coordinates crashcourse deploy design development eclipse error event file firefox fractal functiontesting game givemetehcodez google gui guitesting html ideas image images inheritance innodb int integer internet java javafx javascript jetbrains jpanel jtable julia keyword linux loan machine mediawiki mergers method microsoft mysql netbeans newbie news notdisplaying online pearl php picturebox printf problem programmer programming python random recursive screen search set software sort sourcelabs spring springsource staticcodeanalysis string subclass subdomain sun swf. swing threads time title tree upload web webservices websites windows






