Using g.drawImage
ok i want to draw a Image in a subclass to an applet i have. The problem is even though i import the graphics i can draw the image in a method on that program or even load the image in that program. Is there a possible way for me to do it? import java.util.Io or something?
Example Code Snidbit:
MAIN CLASS CLASS
import java.awt.*;
import java.applet.*;
publicclass Editorextends Applet
{
Tester t =new Tester();
int x,y;
publicvoid init()
{
x = 199;
y = 199;
}
publicvoid paint(Graphics g)
{
t.drawMan(x,y);
}
}
Called Code
import java.awt.*;
publicclass Tester
{
Image man;
public Tester(){man = getImage(getDocumentBase(),"Man.gif");}
publicvoid drawMan(Graphics g,int x,int y)
{
g.drawImage(Man,x,y,this);
}
}
[1898 byte] By [
Wolfx128a] at [2007-11-26 13:13:30]

You're asking how to load an image?[url http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Toolkit.html]Toolkit[/url]Toolkit.getDefaultToolkit().getImage("your path name here");
Thats part of it and that worked like a charm thxBut how do i now display it?I keep getting a problem that saying that JAva cannot find g.drawImage(image_name,0,0,this);
[url http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics.html]Graphics[/url]
Look at what the method takes as parameters. I cant really tell you whats going wrong based on what you've provided. Try posting more (relevant) code, and dont forget to stick it between [code]...[/code] tags.
Ok the important code is
import java.awt.*;
public class Title
{
String ImageName;
int x,y,SY;
Image Title;
public Title(int ix,int iy)
{
x = ix;
y = iy;
SY = 0;
ImageName = null;
}
public void setImage(String ImageName)
{
this.ImageName = ImageName;
Title = Toolkit.getDefaultToolkit().getImage(ImageName);
}
public void drawImage(GuildPageEditor viewer)
{
Graphics.drawImage(Title,x,y,Color.black,viewer); // as you can see i tried but the probelm i get is that it says the method is a static Method
//and I am sure i did it right to get the correct viewer but i do not know
}
}
Code that is pulling on the data
import java.awt.*;
import java.applet.*;
public class GuildPageEditor extends Applet
{
Title t;
Graphics gb;
Image virtualMem;
int apH,apW;
int tx,ty;
public void init()
{
apH = getHeight();
apW = getWidth();
virtualMem = createImage(apH,apW);
gb = virtualMem.getGraphics();
t = new Title(tx,ty);
}
private void Title()
{
if(t.getImageName() == null)
drawNoImage(tx,ty);
else
t.drawImage(this);
}
private void drawNoImage(int x,int y)
{
gb.drawString("No Image",x+20,y+20);
}
public void paint(Graphics g)
{
Title();
g.drawImage(virtualMem,0,0,this);
}
}
Error Message:
non-static method drawImage(java.awt.Image,int,int,Color,java.awt.Image.ImageObserver) cannot be reference from a static context
You need to draw this inside some Swing or AWT component. You use the drawImage() method when you override the paintComponent() method.
public class SomeClass extends JPanel
{
//other code goes here...
protected void paintComponent(Graphics g)
{
d.drawImage(/* whatever goes in here */);
}
}
Get it?
So what your saying is I have to put it in the class that extends the applet
import java.awt.*;
import java.applet.*;
public class GuildPageEditor extends Applet
{
Title t;
Graphics gb;
Image virtualMem;
int apH,apW;
int tx,ty;
public void init()
{
apH = getHeight();
apW = getWidth();
virtualMem = createImage(apH,apW);
gb = virtualMem.getGraphics();
t = new Title(tx,ty);
}
private void Title()
{
if(t.getImageName() == null)
drawNoImage(tx,ty);
else
gb.drawImage(t.getImageName(),t.getX(),t.getY(),this);}
private void drawNoImage(int x,int y)
{
gb.drawString("No Image",x+20,y+20);
}
public void paint(Graphics g)
{
Title();
g.drawImage(virtualMem,0,0,this);
}
}
Graphics.drawImage(Title,x,y,Color.black,viewer);Its having a problem with that. Youre calling like its a static method, but its not.
private void Title()
{
if(t.getImageName() == null)
drawNoImage(tx,ty);
else
gb.drawImage(t.getImageName(),t.getX(),t.getY(),this);}
What is the point of this method? Youre having to create unnecessary object because of this method. Just put this code in your paintComponent() method:
if(t.getImageName() != null)
{
g.drawImage(/*your image*/,t.getX(),t.getY(),this);
}