Display an image in java?

Started by
9 comments, last by Madmonky1 21 years, 6 months ago
I did this in an applet, but it wont work in my java application. my project is a java application, and here is the code for my class:
  
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class SpellAnimations
{
	public int p1x = 0;

	public int p1y = 0;

	public int ticks = 0;
	public int framerate = 5;
	public Image img1;

	
	public void draw_sword(Graphics g){
		g.drawImage(img1,p1x, p1y);
	}
 
 
 
}
  
and the errors: --------------------Configuration: rpg - j2sdk1.4.1 -------------------- C:\Program Files\Xinox Software\JCreator LE\MyProjects\rpging\test\SpellAnimations.java:61: cannot resolve symbol symbol : method drawImage (java.awt.Image,int,int) location: class java.awt.Graphics g.drawImage(img1,p1x, p1y); ^ 1 error Process completed. I know I need to add a path for img1, but is that nessesary for it to compile? [edited by - madmonky1 on October 13, 2002 8:01:32 PM]
the monky has no place in the circle of confusion. Now the mosspit of chaos...
Advertisement
1. There is a Java forum where this really should go, maybe a moderator will move it...

2. When you write an applet, the applet is the component. When you write a non-applet, you have to supply the component. In your code snippet, you don't have a component to draw on and Graphics g doesn't refer to anything. So you must declare a component then you can override its update() repaint() and paint() methods all you want.

3. There are some really good tutorials over at java.sun.com.

[edited by - nonnus29 on October 13, 2002 8:59:13 PM]
If what Nonnus stated is the problem (but I dont think it is), then your second problem is going to be that there is no such method in the standard Graphics class. Try drawImage(img1 , p1x , p1y , null).
drawImage() has to have a reference to the component to be drawn on NULL won''t work either.

If there is a component declared outside of this class somewhere then we really need some more info to be able to help.
thnx, ill try it out.
the monky has no place in the circle of confusion. Now the mosspit of chaos...
quote:Posted by nonnus29 :
drawImage() has to have a reference to the component to be drawn on NULL won''t work either.
No it doesn''t. He passed a graphics object in, which implies that it is created somewhere else. Whether it''s created from an awt component or not is irrelevant - all graphics objects can draw images. His error was compile-time, not run-time so it isn''t a problem with a null graphics object.
quote:Original post by Argus
Posted by nonnus29 :
drawImage() has to have a reference to the component to be drawn on NULL won't work either.


No it doesn't. He passed a graphics object in, which implies that it is created somewhere else. Whether it's created from an awt component or not is irrelevant - all graphics objects can draw images. His error was compile-time, not run-time so it isn't a problem with a null graphics object.


My bad, I guess you have a different implementation of Java than everyone else on the planet, in yours there *IS* a method drawImage that doesn't require a Image Observer and hence won't give a compile error when the wrong number and type of parameters are passed.

[edited by - nonnus29 on October 13, 2002 11:40:24 PM]
Oh so you finally figured out what was really wrong then.

Obviously since I posted the correct method form I must have known about it. Presumably you figured this out after posting, and had to edit your post which now just looks stupid.
Yes Null does work! (now if I can just get it to reference my image...)
the monky has no place in the circle of confusion. Now the mosspit of chaos...
I was wrong, Argus was right. I''m surprised you don''t get a NULL pointer exception when you run it though.

This topic is closed to new replies.

Advertisement