Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Graphics.drawImage function not working, please help.

Reply
Thread Tools

Graphics.drawImage function not working, please help.

 
 
Me
Guest
Posts: n/a
 
      06-17-2005
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);
}
}

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



Thanks for any and all help.
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      06-17-2005
On Fri, 17 Jun 2005 21:32:26 GMT, Me wrote:

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

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Reply With Quote
 
 
 
 
Me
Guest
Posts: n/a
 
      06-20-2005
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...




Me <(E-Mail Removed)> wrote in news:KjHse.69010$9A2.3420@edtnps89:

[EDITED]

> Hi,
> I'm having a problem with drawImage.
>
> So, the problem is that the painting behavior is weird...
> ...Picture gui object goes off the screen and then you drag it
> back on... it only seems to paint on the first paint...
> ...and successive draws don't draw anything!
>
> So, why would this behavior happen?
>
> Thanks for any and all help.


 
Reply With Quote
 
Me
Guest
Posts: n/a
 
      06-20-2005
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);
}
}
}

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

EOM
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      06-20-2005
On Mon, 20 Jun 2005 21:48:17 GMT, Me wrote:

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

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please please please help this guy with his open source java app casioculture@gmail.com Java 4 05-05-2005 08:24 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Computer problems please please please help Nick Computer Support 0 06-04-2004 08:49 PM
HELP! HELP! PLEASE, PLEASE, PLEASE tpg comcntr Computer Support 11 02-15-2004 06:22 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments