Graphics.drawImage function not working, please help.

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);
}
}
 
A

Andrew Thompson

I'm having a problem with drawImage.

You'll get more people willing to help if you post an SSCCE..
<http://www.physci.org/codes/sscce.jsp>

OTOH..
public void paint(Graphics g)

(please replace tabs with 2 or 3 spaces before posting)
{
System.out.println("Picture Paint - Start"); ......
super.paint(g);
}

...that is an odd place to be making a call to super.paint(Graphics).
It would, as I understand, be interfering with the paint process.

Note - Follow-Ups set to comp.lang.gui only.
 
M

Me

So not sure what was wrong... If I forced the clip region full size on
each paint then it would work, but the origianl clip region values seemed
like they should be fine... anyway, forcing the values is no good.

So I continued to try and figure stuff out... I noticed that the problem
only happens in Java 1.5 (I tested with 1.4.1 & 1.4.2 and they were
fine), also using the Applet Viewer (v1.5) also works fine, so its just
the Java 1.5 browser plugin (tested with IE & Firefox).

I realized that I was extending Panel rather than JPanel, (and since most
my stuff is in swing anyway) I decided to change it to JPanel instead and
now it works (but in JPanel the super.paint cant be called as I had it, I
will instead need to call paintComponents I guess).

So in the end it is now working fine for me... BUT that doesn't solve the
problem that I have found, ie. my diagnosis is that 1.5 has introduced
something in Component (Panel inherits paint from it) that causes this
problem. I suppose with JPanel the underlying JContainer's paint is fine,
even though the same Component class is inherited from (via Container),
but I don't belive it inherits paint directly.

Anyway... not sure if this is a desired or known and ignored behavior or
if its a bug that needs fixing...





[EDITED]
 
M

Me

Here is an executable version of the source, chopped it down to just what
is necessary... should run, and should produce the problem.


------------------------------------


package JavaTest;

import java.awt.*;
import javax.swing.*;
import java.net.*;

public class JavaTest extends JApplet
{
Picture picTest = new Picture();

public void init()
{
try
{
URL basepath = getCodeBase();
picTest.image = getImage(new URL
(basepath+"testpic.gif"));
}
catch(MalformedURLException ex){}

picTest.setBounds(new Rectangle(10, 10, 50, 50));
this.setLayout(null);
this.add(picTest, null);
}

public class Picture extends Panel
{
public Image image;

public void paint(Graphics g)
{
g.drawImage(image, 0, 0, this);
}
}
}
 
A

Andrew Thompson

public class JavaTest extends JApplet

Change to..

public class JavaTest extends java.applet.Applet
....

There are probably other ways to fix this source,
but for those, perhaps you had better ask someone
that you have not just sworn at..

Note: Follow-ups set to c.l.j.gui.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top