BufferedImage Scaling

A

artem

Hello all,
i am trying to wright a simple program where i have an image in a
file. I load it in to a BufferedImage and scale it, and then i want to
be able to set the position from the source code.

it works like this. i have 2 BufferedImages i load an image in to them
in the constructor. in the paint method i want to scale img and save
the scaled img to offimg, and the set the position of offimg and draw
it.

here is the source:

BufferedImage img = null; // colored Square
BufferedImage offimg = null; // black Square

Test1Cont(){
try {
img = ImageIO.read(new File("wSquare.bmp"));
offimg = ImageIO.read(new File("bSquare.bmp"));
} catch (IOException e) {}
}

public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.fillRect(0, 0, getWidth(), getHeight());
AffineTransform tx = new AffineTransform();
tx.scale(5, 5);

offimg.createGraphics().drawImage(img, tx, null);
g2d.drawImage(offimg,x,y,null); //set the size with x and y
}

thanks all
 
K

Knute Johnson

artem said:
Hello all,
i am trying to wright a simple program where i have an image in a
file. I load it in to a BufferedImage and scale it, and then i want to
be able to set the position from the source code.

it works like this. i have 2 BufferedImages i load an image in to them
in the constructor. in the paint method i want to scale img and save
the scaled img to offimg, and the set the position of offimg and draw
it.

here is the source:

BufferedImage img = null; // colored Square
BufferedImage offimg = null; // black Square

Test1Cont(){
try {
img = ImageIO.read(new File("wSquare.bmp"));
offimg = ImageIO.read(new File("bSquare.bmp"));
} catch (IOException e) {}
}

public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.fillRect(0, 0, getWidth(), getHeight());
AffineTransform tx = new AffineTransform();
tx.scale(5, 5);

offimg.createGraphics().drawImage(img, tx, null);
g2d.drawImage(offimg,x,y,null); //set the size with x and y
}

thanks all

Do you want to save the offimg to a file? Or do you just want to draw
it at some scale? I suggest you do all of your scaling with the
drawImage() method.
 
A

artem

Do you want to save the offimg to a file? Or do you just want to draw
it at some scale? I suggest you do all of your scaling with the
drawImage() method.

i just want to draw it at a different scale. ok ill try using
drawImage
tnx
 
A

artem

i just want to draw it at a different scale. ok ill try using
drawImage
tnx

ok i got it to works with drawImage. didnt know you could do it with
that. i looked in the tutorial and they where using scaling so i tried
too. lol
 
R

Rupert Woodman

When would you use the AffineTransform class? I see that in this case it
can be done with the Graphics2D.drawImage() method, so why does the
AffineTransform class exist at all? I'm currently using the AffineTransform
class to rotate an image - is that also the wrong thing to do?

Many thanks
Rgds

Rupert
 
K

Knute Johnson

Rupert said:
When would you use the AffineTransform class? I see that in this case it
can be done with the Graphics2D.drawImage() method, so why does the
AffineTransform class exist at all? I'm currently using the AffineTransform
class to rotate an image - is that also the wrong thing to do?

Many thanks
Rgds

Rupert

artem was just drawing an image at some scale. In your case, rotating,
or if doing some more complicated transform, you need the Graphics2D
method with the AffineTransform argument. I was just trying to make it
less complicated for artem.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top