I've been working on an assignment, and have all the requirements completed. The project is to compare the differences in runtime between a linear search algorithm and a binary search. I have a graph class that puts out the results of those searches in a xy graph.
The graph object is a Turtle class that extends JFrame. Is there any way I can convert that graph object to a bitmap and save it for future printing?
The professor requires printouts of the results. Since I don't want a printout of every time the program is run, I would prefer to save the graphics results in a designated folder, rather than using screen-grab.
Unfortunately, I haven't come up with any answers on Google or here. Is something like this even possible?
Another approach is to tell your Component to paint() itself into a BufferedImage, as seen in this complete example. The Container returned by a JFrame's getContentPane() method is suitable. In summary:
Component component = f.getContentPane();
BufferedImage image = new BufferedImage(…);
component.paint(image.getGraphics());
ImageIO.write(image,…);
You can pass the bounds of the area into the Robot.createScreenCapture(Rectangle) method to create a BufferedImage of the area. The easiest way to save the screenshot as an image file is to use the ImageIO class.
Related
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.
I would like to resize multiple imageIcons that I have attached to certain panels, but I'm not sure how to do that. After much research I've attempted to use .getScaledInstance but have had no luck, is that what I'm supposed to use? Since I define my panels outside of the main class but need to use the scaled instances of the images in multiple methods, would I add a line such as
image.getImage().getScaledInstance(30,30,whatever);
or would I need to do a different action?
getScaledInstance doesn't effect the original image, but instead returns a new instance of the image scaled to meet your parameters, make sure you are assigning the result to a variable, for example
image = image.getImage().getScaledInstance(30,30,whatever);
Then pass this to your what ever needs to use it
Beware, the quality of the operation is pretty poor, especially over a large range.
Take a look at
Quality of Image after resize very low -- Java
Java: maintaining aspect ratio of JPanel background image
The Perils of Image.getScaledInstance()
For details and ideas.
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
I am wondering is there a way to convert Image to BufferedImage without code like a
new BufferedImage(...)
because every new init makes app run slower , moreover, if it is in paint() method :(
Please advise the most optimal conversion way.
Thanks
No. Not unless the original Image happens to be a BufferedImage already. Then you can just do a cast:
BufferedImage bufImg = null;
if (origImage instanceof BufferedImage) {
bufImg = (BufferedImage) origImage;
else {
bugImg = new BufferedImage(...);
// proper initialization
}
If it's not a BufferedImage it may very well be for instance a VolatileImage (the other concrete subclass in the API).
From the docs on volatile image:
VolatileImage is an image which can lose its contents at any time due to circumstances beyond the control of the application (e.g., situations caused by the operating system or by other applications).
As you may understand, such image can not provide the same interface as a BufferedImage, thus the only way to get hold of a BufferedImage is to create one, and draw the original image on top of it.
because every new init makes app run slower
Cache one BufferedImage, then only create a new image if the required size changes. Otherwise clear the Graphics object of the current instance and do whatever new drawing is needed.
Is there a way to draw a BufferedImage to JLabel with the paint() method?
One convenient approach is to implement the Icon interface. In this example, Histogram simply draws itself when the label is told to repaint().
If the source of the image requires a time-consuming operation such as scaling, pre-render the image as shown in the static factory, GradientImage.
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.