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

How to create a new image file?

 
Luciano Queiroz
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
Do you know how can I create a new image file (.jpeg) using Swing or Java 2D API?
I would like to draw a colorful rectangle and inside this rectangle write a sequence of 4 characters (the kind of font does not matter). But nothing will be rendered in a JFrame or JDialog nor an Applet.
What I want is create a new physical file (.jpeg or .bmp) containing this image.
I'll appreciate any good idea.
Thanks!
 
Joe Ess
Bartender
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Drawing images without a GUI is not easy. From what I understand, you have to start the GUI with this flag: -Djava.awt.headless=true That lets the VM know you don't have a GUI. Next, create your BufferedImage instance and call getGraphics() on it to get the Graphics context. The Graphics object is what you use to draw with. That done, you can use javax.imageio.ImageIO or com.sun.image.codec.jpeg.JPEGImageEncoder to write the image to file (I've only used the latter). If you need more help, the
Java Tutorial is there for you.
Good Luck!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic