Problem with Graphics 2D scale and manual scale on multilayer. please help!

public void paintComponent(Graphics g){

Graphics2D g2d = (Graphics2D) g;

if(image != null){

g2d.scale(scaleFactor,scaleFactor);

g2d.setComposite(AlphaComposite.getInstance (AlphaComposite.SRC_OVER, 1.0f));

g2d.drawImage(image,0,0,null);

}else{

g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));

g2d.setColor(Color.WHITE);

g2d.fillRect(0,0,getWidth(),getHeight());

}

g2d.setComposite(AlphaComposite.getInstance (AlphaComposite.SRC_OVER, FLOAT_ALPHA));

g2d.drawImage(overlapLayer,0,0,null);

}

Basically i have 2 layers - one background that has an image and one is the one transparent on top of it. The problem is this - the background layer I want to use the Graphics 2D to autoscale the size BUT the layer on top of it I want only the SIZE INCREASE but not the scaling - I have a manual scaling mechanism for the foreground. The reason for the manual scaling is if i dont have the background on (image == null) then I am not going to be able to autoscale much.

Is there anything I can do to change the size of both layers, use the graphics 2D to scale the bottom layer and use my own scaling for the top layer or I HAVE TO stick to one scaling mechanism?

Thanks!

[1293 byte] By [shaselaia] at [2007-11-27 7:34:22]
# 1

Unsolicited suggestion: If you are always going to have an alpha value of 1.0f for the

background you can eliminate the setComposite call.

About your question: I can think of three ways to do the multiple scaling:

1 — reset the graphics transform after each use

if(image != null){

g2d.scale(scaleFactor,scaleFactor);

g2d.drawImage(image,0,0,null);

// Reset scale to 1.0

g2d.scale(1.0/scaleFactor, 1.0/scaleFactor);

}else{

g2d.setColor(Color.WHITE);

g2d.fillRect(0,0,getWidth(),getHeight());

}

g2d.setComposite(AlphaComposite.getInstance (AlphaComposite.SRC_OVER, FLOAT_ALPHA));

g2d.drawImage(overlapLayer,0,0,null);

2 — use a copy of the graphics context and dispose it after use

if(image != null){

Graphics2D copy = (Graphics2D)g.create();

copy.scale(scaleFactor,scaleFactor);

copy.drawImage(image,0,0,null);

copy.dispose();

// Carry on with g2d

3 — use AffineTransform

if(image != null){

double x = // sometimes a translation

double y = // is needed sometimes not

AffineTransform at = AffineTransform.getTranslateInstance(x, y);

at.scale(scaleFactor,scaleFactor);

g2d.drawRenderedImage(image,at);

}else{

g2d.setColor(Color.WHITE);

g2d.fillRect(0,0,getWidth(),getHeight());

}

x = // as needed to locate

y = // scaled ovelay image

at.setToTranslation(x, y);

at.scale(...

g2d.setComposite(AlphaComposite.getInstance (AlphaComposite.SRC_OVER, FLOAT_ALPHA));

g2d.drawRenderedImage(overlapLayer,at);

Using a RenderingHint value of INTERPOLATION_BICUBIC often helps in scaling ops.

crwooda at 2007-7-12 19:14:51 > top of Java-index,Security,Cryptography...