answersLogoWhite

0

How can you display photos and images in Java Swings?

Updated: 10/24/2022
User Avatar

Anandvijayakumar

Lvl 1
12y ago

Best Answer

Java Swings has a lot of nice features and one of them is the ability to display images on its components. You can select any image that is available in the local system or somewhere on the internet and display it on Graphics objects. As of Java SE 1.4, reading an image is very simple.

If the image is stored in a local file:

String filename = "...";

Image image = ImageIO.read(new File(filename));

Otherwise, you can supply a URL:

String urlname = "...";

Image image = ImageIO.read(new URL(urlname));

The read method throws an IOException if the image is not available.

Now the variable image contains a reference to an object that encapsulates the image data. You can display the image with the drawImage method of the Graphics class.

public void paintComponent(Graphics g)

{

. . .

g.drawImage(image, x, y, null);

}

Sometimes, you may want to tile an image across a components canvas if it is much smaller than the component size. We can do the tiling in the paintComponent method. We first draw one copy of the image in the top-left corner and then use the copyArea call to copy it into the entire window using the code below:

for (int i = 0; i * imageWidth <= getWidth(); i++) for (int j = 0; j * imageHeight <= getHeight(); j++) if (i + j > 0)

g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j * imageHeight);

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you display photos and images in Java Swings?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

When Java Swings Introduced?

Java Swings was introduced in 1997.


What is the display device takes the electrical signals from the video card and forms an image using points of colored light on the screen?

java swings


What is the display device that takes the electrical signals from the video card and forms an image using points of colored light on the screen?

java swings


List out the version of java?

Running "java -version" will display the current version of Java.


What is javadoc?

write a java program to display "Welcome Java" and list its execution steps.


What does animation mean in java?

It means moving images.


How do you display text with a label in java?

button.getLabel(); button.setLabel("label");


How do you integrate swings in jsp?

Java Server Pages (JSP) and Java Swings are two entirely differently technologies. Integrating them both is not possible because JSP is a web based technology that runs on an Enterprise Web server and Swings is a stand-alone UI based technology that can run by itself without the need for a server. Hence integrating them both is not possible.


What is the quickiest way to upload photos?

l can use java run environment


What Program that will display the even numbers in data structures and algorithms?

JAVA


How do you display two right triangles in java?

two right triangles


In java .is a part of webpage?

Java is not part of a web page, but is needed to view images and documents found on most web pages.