Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


prathima
swings
1 Answer(s)      4 years and 10 months ago
Posted in : Java Beginners



Advertisement
View Answers

December 10, 2009 at 5:09 PM


Hi Friend,

Try the following code:

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

public class UploadImage extends JPanel {
static File file=null;
int index;
JButton button;
BufferedImage bi, bufferImage;
int w, h;

public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new UploadImage();
frame.getContentPane().add(panel);
frame.setSize(300, 100);
frame.setVisible(true);
}
public UploadImage(){
button=new JButton("Browse");
this.add(button);
button.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{
bi = ImageIO.read(file);
w = bi.getWidth(null);
h = bi.getHeight(null);
if (bi.getType() != BufferedImage.TYPE_INT_RGB) {
BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics big = bi2.getGraphics();
big.drawImage(bi, 0, 0, null);
bufferImage = bi = bi2;
button.setVisible(false);
}
}
catch(Exception ex){}
}
}
});
}
public Dimension getPreferredSize() {
return new Dimension(w, h);
}
public void paint(Graphics g) {
filterImage();
g.drawImage(bufferImage, 0, 0, null);
}
public void filterImage(){
BufferedImageOp op = null;
switch (index){
case 0:
bufferImage = bi;
return;
case 1:
}
bufferImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
op.filter(bi, bufferImage);
}
}
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 )"; }
}

Thanks


Related Tutorials/Questions & Answers:
SWINGS
SWINGS  WHAT ARE THE DIFFERENCES BETWEEN AWT AND SWINGS
JSP & Swings
JSP & Swings  How to integrate jsp and swings
Advertisements
swings question
swings question  how to change the background color with the help of color values by using swings
swings for webnms
swings for webnms  if i am expanding node of jtree then i want to collapse previous expanding node of jtree in swings how is it possible
Swings JTable
Swings JTable  add values to JTable with four coloums,two of them are comboboxes
Swings & JSP
Swings & JSP  Hai all, Can we use Swing components in a JSP?? Suppose if we enter details in a swing dialog box..and If we want the result in a JSP..is that possible??? Thank you
Java Swings
Java Swings  I am doing one project on java Swings, in this i have created one jframe where i defined some JButtons and Jcombobox's, here i need to insert one JTable with headers(IN CURRENT JFrame only).pls help me urgently
swings header
swings header   have a string1 it contains 3 parts,for example "123456 service hello" and string2 contains text "change request " like this. and the string3 should contain "123456 +(second string)+ hello" "second string can
swings
","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; for(i = 0
Swings and JDBC
Swings and JDBC  Hi.. I am vinay.. I am developing a small application using swings and mysql. I am sending part of the code here.. The problem is i need to update the mysql fields with values which are gettin from dynamiclly
Swings - Applet
Swings  Sir, I have developed an application in swings i want to call that class in applet. is it possible. or otherwise is there any way to deploy java class in browser. Give an example... Thanks in advance...  Hi
how to create frame in swings
how to create frame in swings  how to create frame in swings
java swings - Java Beginners
java swings   Do you want to add textfields on the JPanel
Image Movement using Swings
Image Movement using Swings  How to move image using Swings
Swings/awt - Swing AWT
Swings/awt  Hi, how to write action listeners to the Buttons in RichTextEditor tool bar.. thanks in advance...it's Urgent... i am very much new to Swings
java code using swings
java code using swings  code that should be able to enter data of student details using all swings into the access database using jdbc connectivity
java swings - Java Beginners
java swings   Hi, I need the code for click the refresh button then list values will be refresh.Please send the code ........... Thanks, Valarmathi
An application using swings and vector methods
An application using swings and vector methods   Hi, I want an application in Java swings which uses good selection of Vectors methods
about swings - Java Beginners
about swings   Dear sir,Good evening, i am doing mca sir,i am doing the project in swings,so plz provide the material about swings sir Thank you  Hi Friend, Please visit the following link
java swings - Java Beginners
java swings   Hi, I need the code for joptionpane with jcombobox. my requirement is click on add button,one joptionpane will come.from the option pane i need to select the combobox values. Please send the sample code
swings window header
swings window header  i have a string1 it contains 3 parts,for example "123456 service hello" and string2 contains "change request " like this. and the string3 should contain "123456 "........." hello" ".........." can
java swings - Swing AWT
java swings  I am doing a project for my company. I need a to show the performance of the employees by using bar bharts. Please give me how can we write the code for bar charts using java swings.  Hi friend, I am
another frame by using awt or swings
another frame by using awt or swings  how to connect one frame to another frame by using awt or swings
making of dynamic textfields using swings
making of dynamic textfields using swings  How to make dynamic textfields using java swings
how can we store the encrypted passwaord in swings?
how can we store the encrypted passwaord in swings?   how can we store the encrypted passwaord in swings
 

 

 

DMCA.com