How to use BuffereImage for a Chess Game - java

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.

Related

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

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.

Display animation in java?

I am very very new at java (as in, I took one beginner course) so please don't get super technical when answering me.
Earlier today I learned how you can load an image in java and display it in an applet.
I found a step-by-step youtube video that helped me do this.
Now I want to know how I can load an animation in java. Is that something that can be done? And can I get step-by-step instructions on how to do it? Basically, I already have a short .wmv file and I would like to display it when I run the program.
It also has audio that I would like to play along with it.
(i'm actually trying to make a video game with an animated character. java can be used to program games, so this should be doable, right? i don't care about making a game the "right" way or the way game developers usually do it, right now i just want to figure out the simple task of displaying an animation in java.)
thanks!
First of all, the wmv extension stands for video, and an animation is not a video. Its either a spritesheet, a gif image ect. In order to create a even simple animation, you must first learn some basic object oriented programming, become comfortable in creating and using classes, than you can advance to making your own class for taking care of sprites and cutting an image in a way that you can use it ect.
Example of a simple spriteshet:
This spritesheet is an simple image showing you one way to implement your 2d animation. You just have to cut the image parts from it using some rectangles. For 3d, you must use some other software and you must know that they use different kinds of extensions.
I wont dive into code, for the simple reason that I do not have time right now, but if you want further help, give my some contact like skype, facebook ect, I would love to help.
You could try the Java Media Framework javax.media.*. (caveat: rather old framework)
It appears you simply need to embed a media file such as video or audio into an applet. This can be achieved through a player embedded into an applet, similar to an individual frame or a .GIF file, that would be able to handle the .WMV extension.
Assuming awareness of the basic structure of a Java program, the main declaration to get an instance of a player is of the generic form, after:
player = Manager.createRealizedPlayer( mediaURL );
or
player = Manager.createPlayer( file.toURL() );
Good idea to go through the JMF documentation.

How to resize imageicon with same quality

I`m currently making an application to manage other application on windows.
when i resize the icon it change quality
i`m getting icon by this code
ShellFolder shell = ShellFolder.getShellFolder(new File(load1.getString("Path")));
image = shell.getIcon(true);
And when i resize it it change the quality.
My resize code is
sIMG = image.getScaledInstance(45, 45, Image.SCALE_AREA_AVERAGING);
What i want to do to keep the quality of the icon.
Please help.
I'm assuming you want to re-size your icon to look the same no matter what size you change it to. Regular images will are composed of pixels therefore lost of quality is inevitable.
When I did a web design project at my school I learned about Vector images(.svg).
Summary Of Vector Vs Bitmap http://etc.usf.edu/techease/win/images/what-is-the-difference-between-bitmap-and-vector-images/
Of course anyone please correct me if I'm wrong but I think vectors is the only way you can achieve your goal assuming that I'm understanding your question. Vector image quality does not change much or at all on re-size.
Java doesn't natively support Vector Images but follow this tutorial and you should achieve your goal. (It's not really a tutorial more like copy and paste)
http://plindenbaum.blogspot.com/2009/07/simple-java-based-svg-renderer.html
Also this link will let you convert your existing images to SVG
http://vectormagic.com/home
(you get 2 downloads when you sign up) There are other tools to convert to SVG but this was the quickest solution I could find. If you're good with Photoshop I think I saw some tutorials for it but don't quote me on that.
I hope this is what you were looking for best of luck to.
you can resize any image by the following code.
$thumb = new Imagick();
$thumb->readImage('myimage.gif'); $thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');
$thumb->clear();
$thumb->destroy();
For more information please go to the following link.
resize image
resize optimized image

JavaFX: get BufferedImage from path

Coming from AWT/Swing, I've started experimenting a bit with JavaFX the last few days. I realized that what I used to do in thousands lines of codes can now be done in a few hundred.
One problem I came across is, however, the following: I'm trying to develop a small painting app where the user can choose brush size and color for its strokes. For all the strokes the user makes, I use the JavaFX class Path and add these paths to a Group (which is added to a Pane) where they are - automagically - painted. Now I want to store the resulting image as a jpg and try to raster all the paths in a BufferedImage. However, I found no functions in the API that help me do that.
I tried to use Canvas and its GraphicsContext, but that did not help. How could I raster all the JavaFX Paths from a list on an image?
Take a snapshot of your Group to get a JavaFX Image.
Use SwingFXUtils to convert your JavaFX image snapshot to a buffered image.
Use ImageIO to convert your buffered image to a jpeg, png, etc

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