Java applet - saving an image in a png format -


I am creating a simple applet for avatar creation. You can select faces, hair, eyes etc. and then save it as a png file in a disc. Simple version (without interface for simplicity purposes) looks like:

  import java.awt. *; Import java.applet. *; Import Java.net *; Public class example increases the applet (image my_gif; image my_gif2; URL base; media tracker MT; public zero init {mount = new media tracker (try it); try {base = getDocumentBase ();} hold (exception e) {} My_gif = getImage (base, "1.gif"); My_gif2 = getImage (base, "2.gif"); mt.addImage (my_gif, 1); mt.addImage (my_gif2,2); Try {mt.waitForAll ( );} Grip (Interrupted e) {}} Public Zero Color (Graphics G) {g.drawImage (my_gif, 0,0, this); g.drawImage (my_gif2,0,0, this);}}  

There are two files in this example after running the right way Now I want to save it to disk - I can save an image using a buffer image, but I want to "level up" two or more images and want to save it. I also believe that maybe my attitude is not correct and would be grateful for any improvement.

Quickly written and careful code careful!

The basic concept is this: You load the images from which you add the avatar, then you create a new blank image and attract each part to the avatar on it Just created a new image.

Important note: getPath () method will fail due to unsigned appts due to an entry dispute. I think a FileChooser would be a better way here

  import java.awt.Graphics2D; Import java.awt.GraphicsConfiguration; Import java.awt.GraphicsEnvironment; Import java.awt.Transparency; Import java.awt.image.BufferedImage; Import java.io.file; Import java.io.IOException; Import java.net.urISyntaxException; Import java.net.URL; Import javax.imageio.ImageIO; Public Class Avatar {// Graphics Private Graphics Configuration Configuration = Graphics Environment .getLocalGraphicsEnvironment (). GetDefaultScreenDevice () .getDefaultConfiguration (); Private BufferedImage faceImage; Private BufferedImage Hair Image; Private Backward Images Movie Image; Public incarnation (Last String Face, Last String Hair, Last String Mouth, Last String Out) {// Load Image Parts Image = Load (Face); Hair image = load (hair); MouthImage = Load (Mouth); // mix images BufferedImage outImage = combine (); // Save new image {imageIO.write (outImage, "png", new file (getPath (+ + "screenshot.png")); } Hold (IOE EXPRESS E) {}} // Personal Combat Alliance Alliance () {// Create an empty image BufferedImage buffer = Create (200, 400, true); // Get Graphics Reference Graphics2D G = Buffer. Creative Graphics (); // Insert all 3 images empty on a g.drawImage (face image, 0, 0, empty); G.drawImage (hair image, 0, 0, empty); G.drawImage (mouth image, 0, 0, zero); // Get rid of g.dispose () graphics reference; Return buffer; } // Image Private URL getURL (last string filename) {URL URL = Avatar. Class. Mets (filename); Return url; } Private buffer image load (final string file) {URL file name = (file); If (file name == zero) {return tap; } Else {try {return imageIO.read (filename); } Hold (IOException e) {return tap; }}} Create private buffard image (final full width, final full height, last Boolean alpha) {BufferedImage buffer = config.createCompatibleImage (width, height, alpha? Transparency. Transparency: transparency opac); Return buffer; } // path private last string getPath () {string path = currentPath (); If (currentPath (). ToLowerCase (). EndsWith (".jar")) {path = path.substring (0, path.lastIndexOf ("/") + 1); }         return path; } Private string presentpath () Return {{this.getClass ()}. GetProtectionDomain () GetCodeSource () .getLocation (). ToURI () GetPath (); } Hold (URIINXXEpension E) {return ""; }}}  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -