hi!
I’m facing a strange bug with resize/scale ops using Java Graphics2D. I have a small Display objects that paints a background image and adds the desired picture over with transparency. I load the display with a specified size, well this works perfectly. But if I resize the component I should have a scaled picture displayed. That works well for the background but the picture over it simply disapears when it is scaled down and reappears as the scaling comes bigger. I use a Gif format as a background and the picture logo over it is a Png, both with transparent backgrounds.
Gif’s seem to accept the scale transform in real time while the png’s don’t. Is this a know issue with the Java Graphics?
See the paintComponent method:
/** overriden to draw the image on the component graphics
* @param g1 the Graphics instance*/
public void paintComponent(Graphics g1) {
Graphics2D g = (Graphics2D)g1;
originalBox = new Rectangle(0, 0, display.getWidth(this), display.getHeight(this));
Rectangle box = this.tx.createTransformedShape(new Rectangle(0, 0, getWidth(), getHeight())).getBounds();
AffineTransform resizeTX = AffineTransform.getScaleInstance(box.width/originalBox.width, box.height/originalBox.height);
g.setClip(box);
g.setBackground(background);
g.clearRect(box.x, box.y, box.width, box.height);
Composite cps = g.getComposite();
AffineTransform tx = AffineTransform.getScaleInstance(box.width/bgImg.getWidth(this), box.height/bgImg.getWidth(this));
mt.addImage(bgImg, bgImg.hashCode(), box.width, box.height);
mt.addImage(display, hashCode(), box.width, box.height);
try {
mt.waitForAll();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.33f));
g.drawImage(bgImg, tx, this);
g.setComposite(cps);
g.drawImage(display, resizeTX, this);
super.paintComponent(g);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}