Display and resize images in Java tutorial.
 
RoadDust's Java Image Display Tutorial
Tutorial: How to display an image in Java
 
Java image display code example.
 

Welcome to RoadDust's Java image display tutorial. Here you will learn how to display an image using Java. As you will see in this Java tutorial, it is very easy to display an image with Java.

Display an image in Java syntax.

Java image display code example.

The following code example shows you how to display an image with Java. This is the entire code taken from a small java applet that displays an image.

As you can see in the above code example, it is very simple to add an image in Java. I will now explain a few lines from the above Java code example. The first step is to declare your image. An image is declared just like any other variable with the following syntax.

The next step is to give a value to your image. In this case the value will be the image you wish to display. You must do this in the "init" method. The following is an example that would retrieve an image named "picture.jpg" located in the "classes" folder.

Finally, you must put one line of code in the paint method in order to display the image. The following code example is the paint method that displays the image on your Java Applet.

In the above example, the "drawImage" function takes six arguments.

  1. The image to be displayed.
  2. The X coordinate of the top left corner of the image.
  3. The Y coordinate of the top left corner of the image.
  4. The width of the image.
  5. The height of the image.
  6. Where the image is to be displayed.

An this is how an image can be displayed using Java. For this example I used a Java Applet, however it is just as simple when creating any other type of Java application. You can also change the size of an image during execution by replacing the numbers with variables. See how you can zoom-in an image using a tread. Thank you for visiting RoadDust's Java Image display tutorial.