Graphics.drawImage function not working, please help.

Discussion in 'Java' started by Me, Jun 17, 2005.

  1. Me

    Me Guest

    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.
    Me, Jun 17, 2005
    #1
    1. Advertising

  2. 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
    Andrew Thompson, Jun 17, 2005
    #2
    1. Advertising

  3. Me

    Me Guest

    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 <> 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.
    Me, Jun 20, 2005
    #3
  4. Me

    Me Guest

    Here is an full applet version of the source

    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
    Me, Jun 20, 2005
    #4
  5. Re: Here is an full applet version of the source

    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
    Andrew Thompson, Jun 20, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michele Puccini
    Replies:
    0
    Views:
    394
    Michele Puccini
    Nov 9, 2003
  2. Wolfgang
    Replies:
    3
    Views:
    6,074
    Knute Johnson
    Feb 10, 2004
  3. OtisUsenet
    Replies:
    3
    Views:
    12,506
    OtisUsenet
    Feb 24, 2004
  4. Lash Rambo
    Replies:
    5
    Views:
    639
    Lash Rambo
    Jul 15, 2004
  5. Rene
    Replies:
    5
    Views:
    901
    Larry Barowski
    Aug 26, 2004
Loading...

Share This Page