Jump to content


Createimage + Getgraphics() = Error?...


  • Please log in to reply
7 replies to this topic

#1 Nicholas Adri Chandoke

Nicholas Adri Chandoke

    Advanced Member

  • Members
  • PipPipPip
  • 48 posts
  • Gender:Not Telling

Posted 31 May 2009 - 12:21 AM

Here's the exception:
java.lang.UnsupportedOperationException: getGraphics() not valid for images created with createImage(producer)
I looked it up on the internet, but I couldn't get much from it.

Just for convenience, here's my code:
CODE
public class Letter {

    private Image image;
    private int x, y;
    private Graphics2D imageGraphics;
    private boolean tilted = false;
    private Component parent;

    public Letter(Component parent, Image image) {
        this.parent = parent;
        this.image = image;
        imageGraphics = (Graphics2D) image.getGraphics();
    }

    public void waddle(int direction) {
        switch (direction) {
            case SwingConstants.LEFT:
                if (!tilted) {
                    imageGraphics.rotate(-20);
                    tilted = true;
                } else {
                    imageGraphics.rotate(20);
                    tilted = false;
                } if (x - 2 >= parent.getX())
                    x -= 2;
                break;
            case SwingConstants.RIGHT:
                if (!tilted) {
                    imageGraphics.rotate(20);
                    tilted = true;
                } else {
                    imageGraphics.rotate(-20);
                    tilted = false;
                } if (x + 2 + getWidth() < parent.getWidth())
                    x += 2;
                break;
        }
    }

    public void jump() {
        Timer t = new Timer(60, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
            }
        });
        t.start();
    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image newImage) {
        image = newImage;
    }

    public int getX() {return x;}

    public int getY() {return y;}

    public int getWidth() {return image.getWidth(parent);}

    public int getHeight() {return image.getHeight(parent);}

    public Rectangle getBounds() {
        return new Rectangle(x, y, getWidth(), getHeight());
    }

    public void setLocation(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void scale(float factor) {
        image = image.getScaledInstance((int) (getWidth() * factor), (int) (getWidth() * factor), Image.SCALE_DEFAULT);
    }

    public void die() {
        Timer deathTimer = new Timer(20, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (getWidth() < 2 || getHeight() < 2) {
                    Timer t = (Timer) e.getSource();
                    t.stop();
                    try {
                        finalize();
                    } catch (Throwable ex) {
                        Logger.getLogger(Letter.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    y = parent.getHeight() / 2;
                    imageGraphics.rotate(-1);
                    scale(.85f);
                }
            }
        });
        deathTimer.start();
    }

    public final static Letter K(Component parent) {
        return new Letter(parent, (Image) Toolkit.getDefaultToolkit().getImage(Letter.class.getResource("/org/K.png")));
    }

    public final static Letter Z(Component parent) {
        return new Letter(parent, (Image) Toolkit.getDefaultToolkit().getImage(Letter.class.getResource("/org/Z.png")));
    }
}

If you're wondering about the Letter thing, it's an incredibly useless video game that I'm writing for kicks; you play in a non-side-scrolling two dimensional environment as a capital letter 'K'. At the opposite end
of the window is a 'Z'. When you touch the 'Z', then the 'K' object (used in the main class (not provided)) envokes the "die()" method. 'K' and 'Z' are Letters. tongue.gif

#2 Kraicheck

Kraicheck

    Advanced Member

  • Members
  • PipPipPip
  • 884 posts
  • Gender:Male
  • Location:Belgium

Posted 31 May 2009 - 08:51 AM

The exception is pretty clear.
The Image object you provide to the constructor of the Letter class is created with the createImage method of the Toolkit class.
You can't use the getGraphics method on such an object.
Change the way you obtain to Image object and it should work.

#3 Nicholas Adri Chandoke

Nicholas Adri Chandoke

    Advanced Member

  • Members
  • PipPipPip
  • 48 posts
  • Gender:Not Telling

Posted 01 June 2009 - 04:56 PM

... do you know how else to create an Image? I have tried using
CODE
new javax.swing.ImageIcon(Letter.class.getResource("URL Here")).getImage().getGraphics()...
but it still gives the same error.

#4 Captain Pierce

Captain Pierce

    Advanced Member

  • Moderator
  • PipPipPip
  • 877 posts
  • Gender:Male
  • Location:Georgia

Posted 01 June 2009 - 11:45 PM

Drop the call to getGraphics(). The purpose of a Graphics context is to render to an image, not get it for rendering elsewhere.

#5 Kraicheck

Kraicheck

    Advanced Member

  • Members
  • PipPipPip
  • 884 posts
  • Gender:Male
  • Location:Belgium

Posted 03 June 2009 - 07:14 AM

You'll have to use an AffineTransform.
More info can be found here: Transforming Shapes, Text, and Images.

#6 Nicholas Adri Chandoke

Nicholas Adri Chandoke

    Advanced Member

  • Members
  • PipPipPip
  • 48 posts
  • Gender:Not Telling

Posted 06 February 2010 - 04:57 PM

Just to close-up this topic, I've figured out how to get around this: the ImageIO class.

#7 fbimage

fbimage

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 14 June 2013 - 03:48 AM

i have some thoughts about c# creating images , you don't need to use getgraphics method to achieve this. you can try other methods to obtain images.

#8 arronlee

arronlee

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 25 June 2013 - 08:04 AM

I agree with Kraicheck, you should change the way you obtain to Image object.
Some third party tools are also of great help.
Best Regards,
Arron



I am testing about SDKs dealing with images, barcodes, and documents.
Do you have any ideas?


Next Tomorrow is Another Day.