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

How do I put an image on a canvas?

 
Carlos Failde
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have put a canvas on an applet and want to add a gif image to the canvas.
I know to put an image on an applet you'd use something like
Image I= getImage(getCodeBase(), "image.gif");
g.drawImage(I, x,y,width, height, this);
but I can't figure out how to add an image to my canvas.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread may help with your question. Basically, you will have to subclass the canvas and override its paint() method. You should be careful if you are mixing a canvas with any Swing components because you can get wierd results, as mentioned in the above thread. In that case, you can use a JPanel for drawing instead.
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You simply have to get the Graphics instance from the Canvas and draw using that. It should look something like this

Hope this helps.
Sean
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic