• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

creating an image file with the 2d context of a componet

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been asked to produce an GUI that will let the user open annotate and then save an image from and to file.
currently i am using a jlabel as a canvas to open the image using a draw image method like so

Graphics2D g1 = (Graphics2D) jLabel1.getGraphics();
g1.drawImage(scaledBI, 0, 0, scaledWidth, scaledHeight, null);
g1.dispose();

i am then letting users add shapes to this using a simalar method. First jLabel.getGraphics to get an graphics2d variable and then this is used for drawing lines etc. to demostrate here is an exampe from the code

Graphics2D g = (Graphics2D) jLabel1.getGraphics();
g.drawRect(a,b,c,d);
g.dispose();

all this has been fine but saving the changed contents of the jLabel has caused problems. here is the code as is

scaledHeight=500 ;
scaledWidth= 500;
int imageType = BufferedImage.TYPE_INT_RGB;

BufferedImage image = new BufferedImage(scaledWidth, scaledHeight, imageType);
Graphics2D g2d = image.createGraphics();
g2d.setComposite(AlphaComposite.Src);
jLabel1.paint( g2d );
g2d.dispose();

this image when saved to file and opened externally has been giving me a mostly black page with the original text that is on the jLabel on starting the application.

how do i go about creating an image file that contains the context of an component???
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic