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")));
}
}
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.











