Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get Image from Canvas

 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I issue draw() on the Canvas.



I need later to get the image in that Canvas.

How do I get the image from the Canvas?

I will appeciate it.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of object is "image" - can't you use that directly?
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Now I need to get the Image in the Canvas.


 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
re:"You can't draw a byte[] directly."

It is an Image. I mistakenly mentioned that it is a byte[].

Thanks to your reply on both threads. Yes, it is the same issue.

Ill try it out.
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, Thanks. It worked well.
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic