java applet is it possible to make an applet into png file - java

Hi im new to programming and im starting to program with java im creating a program that will paint in the applet and everything seems good.
The thing is i want to save the paint that i have done and i was wondering it is possible to convert that applet or the paint method to png file. If not i was working on that everytime the user paint it will save the coordinates, color and size and save it in txt then i have to create a reader or a display for it.
Is it possible to make an applet to png file ?

You can convert an image into a png file by using ImageIO.write(...). Hopefully it should be fairly easy to extract the image from your painting, but this depends on much that has not yet been shown to us. Please show us your pertinent code so that I can improve this answer with concrete code.
Assuming that you're drawing within a JPanel's paintComponent method or an Applet's paint method, then you can instead paint into a BufferedImage, and then use that Image to convert to your png file.
The other issue is writing from the applet itself to the file on the disk, and there may be permission issues with this. Myself, I avoid programming applets like the plague, since they're really not used much any more, and in fact some browsers don't allow use of them.

Related

What is the difference between the ways to read an Image file in Java?

There are various ways of reading an image file in java such as BufferedImage and ImageIcon to name a few. I want to know what is the difference between these cases? Are they context dependent that in a particular case only one of them can be used?
What would be the best way of reading a image selected by JFileChooser by the user and separating the color channels of an image?
A good way is to use the different ImageIO.read methods, which return BufferedImage objects.
Image is an abstract class, so I think the real question is which subclass is more efficient for your program. Use VolatileImage if you need hardware acceleration. More on that here.
ImageIcon (and Toolkit#createImage/Toolkit#getImage) use a background loading process. That is, after you call these methods, they will return immediately, having created a background thread to actually load the image data.
These were/are used when loading large images across slow connections, like ye old 28k modems (ah, how I remember the days). This means that your application can continue running while the images are been downloaded.
You'll find in the Graphics class the drawImage methods accept an ImageObserver interface and that java.awt.Component implements this interface, this allows components the ability to automatically update themselves once the image has actually finished loading.
ImageIO on the other hand will not return until the image is fully loaded. It also makes it easier to introduce new readers/writers, making the API far more flexible then the original API. ImageIO also supports a wider range of images out of the box.
BufferedImage is also a far more flexible image class, especially when it comes to apply effects to the image.
Now, I, personally, prefer ImageIO. If I know I'm loading large images or images over a potentially slow connection, I will create my own background thread to load them. While a little more complicated, the trade offs greatly out weight the small amount of extra work -IMHO
What would be the best way of reading a image selected by JFileChooser by the user and separating the color channels of an image?
ImageIO without a doubt. In order to do any serious manipulation of an image loaded using something ImageIcon, you'd have to convert that image to a BufferedImage anyway

Toolkit.getDefaultToolkit().createImage() vs ImageIO.read()

I'm creating a UI using Swing and I want to display an image in a JLabel. The code I use is the following:
JLabel label = new JLabel(new ImageIcon(ImageIO.read(new File("img.jpg"))));
This works fine if I use png images but when it comes to jpg (only some of them), I get a redish image (a different one than the one I see in Paint.NET).
The image I used is this one: img.jpg
So I tried (as an alternative):
Toolkit.getDefaultToolkit().createImage(new File("img.jpg").getAbsolutePath());
Does anyone have an idea of why this happening? Is it a special JPEG format which is not supported?
I've read on this forum that most people recommend to use ImageIO (here for example). Why?
Thanks a lot
As discussed here, your JPEG image may contain spurious transparency information. One simple expedient is to render the image in a buffer having a compatible color model, as shown here.
It looks like you have found a bug in ImageIO.read... (I can reproduce the red tint, and it is definitely not how it should look like).
You can try to
save the JPEG files with other settings
open/re-save the file with other programs (hoping to get a more common JPEG-encoding)
or use the Toolkit method (if you don't control the images).
The only problem with the Toolkit method is that the getImage() method returns immediately after it is invoked and the loading is happening on a background thread, so you cannot start working with the Image object immediately.

Java2d thumbnails. Can I get thumbnail from OS

I develop an application in which I want to display a grid with a list of images. For each image I create an instance of a class myImage. MyImage class, extends JCompoment and create an thumbnail and after draw it with overide thepaintCompoment(Graphics g).
All is ok, but in big size images I have a lot of delay to create the thumbnail.
Now I think to when I scan the folders for image(to create the list I said above to create an thumbanail of each image and save it to disc. For each image I will have a database record to save the image path and thumbnail path.So is this a good solution of the problem?
Is there a way to get the thumbnails of the system creates for each image, in file manager. Or a more effective solution than I try.
Thank you!!
Your best bet is to use something like imagemagick to convert the image and create the thumbnail. There's a project called JMagick which provides JNI hooks into Imagemagick, but running a process work too.
Imagemagick is heavily optimized C code for manipulating images. It will also be able to handle images that Java won't and with much less memory usage.
I work for a website where we let users upload art and create thumbnails on the fly, and it absolutely needs to be fast, so that's what we use.
The following is Groovy code, but it can modified to Java code pretty easily:
public boolean createThumbnail(InputStream input, OutputStream output){
def cmd = "convert -colorspace RGB -auto-orient -thumbnail 125x125 -[0] jpg:-"
Process p = cmd.execute()
p.consumeProcessErrorStream(System.out)
p.consumeProcessOutputStream(output)
p.out << input
p.out.close()
p.waitForOrKill(8000)
return p.exitValue()==0
}
This creates a thumbnail using pipes without actually writing any data to disk. The outputStream can be to a file if you wanted to immediately write it as well.
One way to avoid OS dependance is to use getScaledInstance(), as shown in this example. See the cited articles for certain limitations. If it's taking too long, use a SwingWorker to do the load and scale in the background.
I haven't used it for the creation of thumbnails, but you may also want to take a look at the ImageIO API.
ImageIO

How to use BuffereImage for a Chess Game

I am trying to create a chess game for java using images my classmate and I are working on. We are using a created image to serve as the board and pieces to place about that the user can drag and place. Unfortunately we do not currently know how to import images from a file directory into the program itself. Not only that, but we are completely clueless as to why this would occur, as the Java API guide does not clearly explain this process.
The portion of the JDK API that provides image input is the package javax.imageio. The easiest way to load an image (into a BufferedImage object) is:
BufferedImage img = ImageIO.read( new File(pathname) );
Don't forget to catch the appropriate exceptions.
Read the section from the Swing tutorial on How to Use Icons for working examples of different ways to read images and display them on a label. Its easier to drag a label around the screen then just and image.

Java Image Editor which renders output as source code?

Alex explained what I'm looking for much better than I have:
You want an existing program that
allows you to draw a picture, captures
what you do as you draw, and writes
each action as a Java command. When
you click the "Drawl Oval" tool and
click at 0,0 and then at 50,50, it
would generate the line
g.drawOval(0,0,50,50).
If anybody knows of a program such as this, let me know. Thanks.
Original question:
I've been working with Java and custom drawing using the java.awt.Graphics library lately, but find it is taking too much time to write manually. Is there any simple graphics editor (like mspaint) which generates source code?
Example:
Drawing this:
Would generate:
public void update(Graphics g) {
g.translate(0, 0);
g.drawOval(0, 0, 50, 50);
}
Thanks.
If they are vectors, you could use an SVG Editor (eg, Inkscape) along with Kirill's SVG to Java2D Transcoder to simplify this. It isn't perfect, but Kirill is very responsive in responding to requests for enhancement.
It's unclear what you are asking. Two guesses:
You want an existing program that allows you to draw a picture, captures what you do as you draw, and writes each action as a Java command. When you click the "Drawl Oval" tool and click at 0,0 and then at 50,50, it would generate the line g.drawOval(0,0,50,50).
I do not know of any such tool. But the above might help you reword your question so that others can share their knowledge.
You want a program that takes an existing bitmap and converts it into a series of commands that will replicate the bitmap. Other than simply outputting pixels, such a tool is nearly impossible to write; attempting to decompose an arbitrary picture into simple drawing commands is very hard.
In this case, I would recommend simply importing the bitmap as a JPG, PNG, whatever, and using drawImage() instead of using Graphics calls.
While not what you were looking for, I should mention that XPM (X Pixmap) format is basically a subset of C programming language. XPM2 simplified it more by removing the trappings of C syntax. XPM3 brought them back again.
In a sense XPM image converters are source code generators and translators.
You are looking for something similar to output Java AWT, but for many real images or photographs it would be complicated to do analysis on the image to find oval,etc and create the code for drawing them with lines and shapes (well unless the image had filters applied to simplify it, or was an SVG as someone pointed out). It would probably have to convert to a bitmap of some form and keep it in an array in the generated Java source.

Categories