Images in java

Ads

 
 

Share on Google+Share on Google+

Shahnawaz qadir
Images in java
2 Answer(s)      5 years and 2 months ago
Posted in : Java Beginners

how to handle images in java

Ads
View Answers

September 18, 2012 at 4:27 PM


Here is a simple example of displaying an image on frame.

import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class DisplayImage extends JPanel {
BufferedImage image;
DisplayImage(){
String f="c:/fruit.png";
File file = new File(f);
try{
image = ImageIO.read(file);
}
catch(Exception ex){}
repaint();
}
public void paint(Graphics g) {
g.drawImage( image, 0, 0, null);

}
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel p=new DisplayImage();
frame.add(p);
frame.setSize(300, 300);
frame.setVisible(true);
}
}


September 18, 2012 at 4:30 PM


Here is an example that allow the user to select any image file and display that image on frame.

import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class DisplayImage extends JPanel {
BufferedImage image;
Image img;

JButton browse;
File file = null;
public DisplayImage() {

browse = new JButton("Display");
this.add(browse);
browse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.addChoosableFileFilter(new ImageFileFilter());
int returnVal = chooser.showOpenDialog(null);

if(returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
try{
image = ImageIO.read(file);
}
catch(Exception ex){}
repaint();
}
browse.setVisible(false);
}
});
}

public void paint(Graphics g) {
g.drawImage( image, 0, 0, null);

}
public static void main(String[] args) {
JFrame frame = new JFrame("");
JPanel panel = new DisplayImage();
frame.getContentPane().add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}

}
class ImageFileFilter extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
if (file.isDirectory()) return false;
String name = file.getName().toLowerCase();
return (name.endsWith(".jpg") || name.endsWith(".png")|| name.endsWith(".gif"));
}
public String getDescription() { return "Images (*.gif,*.bmp, *.jpg, *.png )"; }
}

Ads









Related Tutorials/Questions & Answers:
Images in java
Images in java  how to handle images in java
java mail sending with images
java mail sending with images  I need to send images through java mail without giving content path(i.e. we don't want hard code the image path)can you tell me the idea?   Please visit the following links: http
Advertisements
images - Java Beginners
images  how to do image watermarking in java? or how to identify watermarked images.. tell something related to the watermarking concept implementation in java?  Hi Friend, Try the following code: import java.io.
images - Java Beginners
("Java"); content.add(button1); ImageIcon cup = new ImageIcon("/home...); content.add(button2); JButton button3 = new JButton("Java", cup..., http://www.roseindia.net/java/example/java/swing/ Thanks
Sending images through java mail
Sending images through java mail  Am trying to develop greeting application that having images..... in one jsp page i displayed all images and by clicking one image the control go to mail sending page in that the image should add
Collision Detection between two images in Java
Collision Detection between two images in Java  Collision Detection between two images in Java
Display Multiple Images in jscrollpane using Java Jpanel
Display Multiple Images in jscrollpane using Java Jpanel  Browse and Display multiple images in vertical view of java jscrollpane using jpanel
how to upload and download images in java?
how to upload and download images in java?  what is the code for uploading and downloading images in java? how do I make a photo gallery through JSP....   Upload and Download images: 1)page.jsp: <%@ page language="java
Version of com.applitools>eyes-images-java dependency
List of Version of com.applitools>eyes-images-java dependency
java collision detection between two images
java collision detection between two images  java collision detection between two images
images
images  code for displaying image in java swings
compareing images using java - Swing AWT
compareing images using java  hi can u please give me the code in java to compare imges..i have the program to get the pixel values using pixel... with another image,i.e,program to compare two images using the pixel values.. thank
Maven Dependency eyes-images-java >> 2.34
You should include the dependency code given in this page to add Maven Dependency of com.applitools >> eyes-images-java version2.34 in your project
Maven Dependency eyes-images-java >> 2.49
You should include the dependency code given in this page to add Maven Dependency of com.applitools >> eyes-images-java version2.49 in your project
Maven Dependency eyes-images-java >> 2.50
You should include the dependency code given in this page to add Maven Dependency of com.applitools >> eyes-images-java version2.50 in your project
Maven Dependency eyes-images-java >> 2.51
You should include the dependency code given in this page to add Maven Dependency of com.applitools >> eyes-images-java version2.51 in your project
Maven Dependency eyes-images-java >> 2.53
You should include the dependency code given in this page to add Maven Dependency of com.applitools >> eyes-images-java version2.53 in your project
Maven Dependency eyes-images-java >> 2.54
You should include the dependency code given in this page to add Maven Dependency of com.applitools >> eyes-images-java version2.54 in your project
Maven Dependency eyes-images-java >> 2.55
You should include the dependency code given in this page to add Maven Dependency of com.applitools >> eyes-images-java version2.55 in your project
Maven Dependency eyes-images-java >> 2.56
You should include the dependency code given in this page to add Maven Dependency of com.applitools >> eyes-images-java version2.56 in your project
how to show set of images continously using JScrollpane in java
how to show set of images continously using JScrollpane in java  how to show set of images continously using JScrollpane in java
how to show set of images continously using JScrollpane in java
how to show set of images continously using JScrollpane in java  how to show set of images continously using JScrollpane in java
displaying created images with java beans - Java Server Faces Questions
displaying created images with java beans  hello I am sorry I... am creating some images at "runtime" with an JSF application. without encountering any problem. But when I try to display these images during runtime, setURL
Animating Images in Java Application
Animating Images in Java Application   ... with multiple images. You can see how animation has been implemented in the following... images. Image are given below: In this program, images are displayed one
How to upload multiple images in java(struts) using jsp?
How to upload multiple images in java(struts) using jsp?  I have jsp...;COLLEGE INFORMATION :</legend> <pre> Gallery Images: <input type... to upload the multiple images in struts Total images storing folder in project
How to upload multiple images in java(struts) using jsp?
How to upload multiple images in java(struts) using jsp?  I have jsp...;COLLEGE INFORMATION :</legend> <pre> Gallery Images: <input type... to upload the multiple images in struts Total images storing folder in project
How to upload multiple images in java(struts) using jsp?
How to upload multiple images in java(struts) using jsp?  I have jsp...;COLLEGE INFORMATION :</legend> <pre> Gallery Images: <input type... to upload the multiple images in struts Total images storing folder in project
How to upload multiple images in java(struts) using jsp?
How to upload multiple images in java(struts) using jsp?  I have jsp...;COLLEGE INFORMATION :</legend> <pre> Gallery Images: <input type... to upload the multiple images in struts Total images storing folder in project
How to upload multiple images in java(struts) using jsp?
How to upload multiple images in java(struts) using jsp?  I have jsp...;COLLEGE INFORMATION :</legend> <pre> Gallery Images: <input type... to upload the multiple images in struts Total images storing folder in project
How to upload multiple images in java(struts) using jsp?
How to upload multiple images in java(struts) using jsp?  I have jsp...;COLLEGE INFORMATION :</legend> <pre> Gallery Images: <input type... to upload the multiple images in struts Total images storing folder in project
How to upload multiple images in java(struts) using jsp?
How to upload multiple images in java(struts) using jsp?  I have jsp...;COLLEGE INFORMATION :</legend> <pre> Gallery Images: <input type... to upload the multiple images in struts Total images storing folder in project

Ads

 
Advertisement null

Ads