Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How do I put an image on a canvas?

 
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.
 
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.
 
author
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
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic