Java Swing - Graphics LESSON NOTE SUPPORTED IMAGES Java
supports JPG, GIF and PNG formats.
There are apparently ways to add support for other formats but this
will not be covered in this lesson. IMAGE LOCATION The
biggest frustration related to displaying images is figuring out where to
place the image on the hard drive so that Java and find it. The
answer is that you must place it in your project folder. However, the definition of project folder
depends on the IDE that you are using. If
you are using JCreator or another basic setup, then
you have a folder that contains all your java and class files. That is the folder. If
you are using Eclipse, you need to use the folder that contains the
subfolders named src and bin. Eclipse itself refers to it as the project
folder. TRY In
Java, when we want to access a file on the hard drive, we have to place that
code inside of a try/catch statement.
The try section is executed. If
there is an error (or exception), then the program immediately switches to
the catch section. DISPLAYING AN IMAGE
WRITING AN IMAGE TO FILE This
process is very simple. It only
requires a few lines of code. However,
for the example, we will also read and file and display it to screen. Notice the comments in the code that make it
clear what is new code and what was already there before.
We
can use AffineTransform objects to apply
transformations to images before they are drawn. This works in the exact way as it did
before.
|
||||||
|
||||||
|