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...

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.

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?

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...

Can yu pass it as an extra parameter when you call floorpanel1?

Extra parameter...? I'm sorry, I don't quite understand. How would that help? What would it do?

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.

I see... I haven't used ImageObserver, so I'll try "null", but if it doesn't work I'll try the IO. Thanks!

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?

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.