M
Me
Hi,
I'm having a problem with drawImage.
What I'm doing is making a simple class that is extends a Panel (did it
with Canvas with same result) and I call this class Picture.
An Image object can be passed into the Picture object and then on paint
the Picture object will draw the Image.
So, the problem is that the painting behavior is weird...
It will paint fine, then if you drag the applet so the Picture gui object
goes off the screen and then you drag it back on (causing redrawing), it
only seems to paint on the first paint event it receives... and
successive draws don't draw anything!
Now, if you again bring the Picture gui object FULLY off screen and then
back on it will again do the same thing (painting the first paint event),
so it seems to work for a full object paint, but not when only part of it
needs to paint.
Note that I've done testing to ensure the paint events are being
sent/received (they are), and that the Image is valid (it is), and that
drawImage method is returning true (it is).
Also Note that a drawString call in the same place as the drawImage call
works fine.
Finally, if I stretch the Image
ie) drawImage(img, 0, 0, 90, 90, obs) instead of
drawImage(img, 0, 0, 100, 100, obs)... assuming the image is 100x100
then it works fine (except that the image is stretched/squished)???
So, why would this behavior happen?
Hopefully I explained it well enough...
Picture class code follows:
-----------------------------------------------------------
package MyPackage;
import java.awt.Panel;
import java.awt.MediaTracker;
import java.awt.Image;
import java.awt.Graphics;
import java.lang.InterruptedException;
public class Picture extends Panel
{
private MediaTracker tracker = null;
private Image image;
public Picture(Image img)
{
tracker = new MediaTracker(this);
setPicture(img);
}
public void setPicture(Image img)
{
image = img;
if(img != null)
{
tracker.addImage(img, 0);
tracker.checkID(0, true);
}
repaint();
}
public void paint(Graphics g)
{
System.out.println("Picture Paint - Start");
try
{
if(!tracker.checkID(0))
{
tracker.waitForID(0);
}
if(!tracker.isErrorID(0))
{
if(g.drawImage(image, 0, 0, this))
System.out.println("Draw=T");
else
System.out.println("Drawn=F");
}
}
catch(InterruptedException ex)
{
System.out.println("Picture Paint - Int.Ex.");
}
super.paint(g);
}
}
I'm having a problem with drawImage.
What I'm doing is making a simple class that is extends a Panel (did it
with Canvas with same result) and I call this class Picture.
An Image object can be passed into the Picture object and then on paint
the Picture object will draw the Image.
So, the problem is that the painting behavior is weird...
It will paint fine, then if you drag the applet so the Picture gui object
goes off the screen and then you drag it back on (causing redrawing), it
only seems to paint on the first paint event it receives... and
successive draws don't draw anything!
Now, if you again bring the Picture gui object FULLY off screen and then
back on it will again do the same thing (painting the first paint event),
so it seems to work for a full object paint, but not when only part of it
needs to paint.
Note that I've done testing to ensure the paint events are being
sent/received (they are), and that the Image is valid (it is), and that
drawImage method is returning true (it is).
Also Note that a drawString call in the same place as the drawImage call
works fine.
Finally, if I stretch the Image
ie) drawImage(img, 0, 0, 90, 90, obs) instead of
drawImage(img, 0, 0, 100, 100, obs)... assuming the image is 100x100
then it works fine (except that the image is stretched/squished)???
So, why would this behavior happen?
Hopefully I explained it well enough...
Picture class code follows:
-----------------------------------------------------------
package MyPackage;
import java.awt.Panel;
import java.awt.MediaTracker;
import java.awt.Image;
import java.awt.Graphics;
import java.lang.InterruptedException;
public class Picture extends Panel
{
private MediaTracker tracker = null;
private Image image;
public Picture(Image img)
{
tracker = new MediaTracker(this);
setPicture(img);
}
public void setPicture(Image img)
{
image = img;
if(img != null)
{
tracker.addImage(img, 0);
tracker.checkID(0, true);
}
repaint();
}
public void paint(Graphics g)
{
System.out.println("Picture Paint - Start");
try
{
if(!tracker.checkID(0))
{
tracker.waitForID(0);
}
if(!tracker.isErrorID(0))
{
if(g.drawImage(image, 0, 0, this))
System.out.println("Draw=T");
else
System.out.println("Drawn=F");
}
}
catch(InterruptedException ex)
{
System.out.println("Picture Paint - Int.Ex.");
}
super.paint(g);
}
}