This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Ulf Dittmer wrote:What kind of object is "image" - can't you use that directly?
(image is a jpg byte [])
Actually, I have only 1 target: resize a photo to a larger size, keeping the ratio.
but if the original dimension is not proportional to the target dimension, i want to fill extra area to make it be exact dimension as the target dimension.
this is why i am putting the image first into a Canvas, with the background color set, and the target dimension set for the Canvas.
I see so many things wrong with that method...
1) you are missing the call to super.paint(g). Without it the previously painted contents will not be thrown away.
2) unless the Canvas is not part of a user interface or it's parent container has no layout manager the call to setSize does nothing.
3) setBackground shouldn't be called from the paint method but before it's ever called. This call could (it doesn't know for as far as I know but it could in the future) cause a repaint, which in your case would lead to a StackOverflowError.
In fact, only the if statement with contents is right.
Jesus Angeles wrote:
Ulf Dittmer wrote:What kind of object is "image" - can't you use that directly?
(image is a jpg byte [])
You can't draw a byte[] directly. You can only draw an Image. You can use ImageIO.read to convert a byte[] (through a ByteArrayInputStream[]) into a BufferedImage.
Actually, I have only 1 target: resize a photo to a larger size, keeping the ratio.
but if the original dimension is not proportional to the target dimension, i want to fill extra area to make it be exact dimension as the target dimension.
this is why i am putting the image first into a Canvas, with the background color set, and the target dimension set for the Canvas.
Now I need to get the Image in the Canvas.
If I get it clear you just want to resize an existing image, right? Then drop the Canvas - use BufferedImage instead: