File chooser to buffered image

Hi all,

Sorry about the total newbie question; I'm trying to figure out how to convert a jpg into a buffered image, while using the file chooser to select it. I'd like to do all this in a scrollpane. Does anyone have some sample code for this, at least so I can play around? I have ideas, but I'm getting tons of errors while trying things like:

public class ImProc extends JComponent{

private BufferedImage source, destination;

private JComboBox options;

public ImProc( BufferedImage image){

source = destination = image;

setBackground(Color.white);

setLayout( new BorderLayout());

JPanel controls = new JPanel();

options = new JComboBox(

new String[] {"[source]", "brighten", "darken", "rotate", "scale" }

);

options.addItemListener(new ItemListener(){

public void itemStateChanged( ItemEvent ie){

String option = (String)options.getSelectedItem();

BufferedImageOp op = null;

if(option.equals("[source]"))

destination = source;

else if(option.equals("brighten"))

op = new RescaleOp(1.5f, 0, null);

else if(option.equals("darken"))

op = new RescaleOp(0.5f, 0, null);

else if (option.equals("rotate"))

op = new AffineTransformOp(

AffineTransform.getRotateInstance(Math.PI / 6), null);

else if (option.equals("scale"))

op = new AffineTransformOp(

AffineTransform.getScaleInstance(.5, .5), null);

if(op != null) destination = op.filter(source, null);

repaint();

}

});

controls.add(options);

add(controls, BorderLayout.SOUTH);

}

public void paintComponent(Graphics g){

int imageWidth = destination.getWidth();

int imageHeight = destination.getHeight();

int width = getSize().width;

int height = getSize().height;

g.drawImage(destination,

(width - imageWidth) / 2, (height - imageHeight) / 2, null);

}

public static void main(String[] args){

JFileChooser chooser = new JFileChooser();

String filename = chooser.getName();

ImageIcon icon = new ImageIcon(filename);

Image i = icon.getImage();

int w = i.getWidth(null), h = i.getHeight(null);

BufferedImage buffImage = new BufferedImage(w, h,

BufferedImage.TYPE_INT_RGB);

Graphics2D imageGraphics = buffImage.createGraphics();

imageGraphics.drawImage(i, 0, 0, null);

JFrame frame = new JFrame("Image");

frame.getContentPane().add(new ImProc(buffImage));

frame.setSize(buffImage.getWidth(), buffImage.getHeight());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

So, I'm stuck. Any help from anyone is appreciated.

Thanks,

Joe

[2792 byte] By [jmarottaa] at [2007-11-26 12:21:16]
# 1

I'll give you the pseudocode because I really dont feel like looking it up. As far as I know, there is no really ergonomical approach to make a buffered image from a file. So it isn't real clean, but it works and that's what counts. Plus, since you are just loading the image, any small amount of delay involved in this is unnoticable. Here it is:

JFileChooser jfc = new JFileChooser()

jfc.showOpenDialog();

String fn = jfc.getSelectedFile().getName();

Image img = (new ImageIcon(fn)).getImage();

BufferedImage bi = new BufferedImage(width, height);

Graphics2D g2d = bi.createGraphics();

g2d.drawImage(img,0,0,null);

Thats it, you should now have your bufferedimage

What happened is, you took the file from the filechooser, got the Image from the ImageIcon from the Filename. And made a bufferedimage, got it's Graphics, and used the graphics to draw the image on the buffered image.

Whatever you do, dont do this every frame of your app.

FortisVenalitera at 2007-7-7 15:12:37 > top of Java-index,Archived Forums,Socket Programming...
# 2
> As far as I know, there is> no really ergonomical approach to make a buffered> image from a file.Yes there is. Look into the [url http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html]ImageIO[/url] class.
CaptainMorgan08a at 2007-7-7 15:12:37 > top of Java-index,Archived Forums,Socket Programming...
# 3
Have a look at static helper method - ImageIO.read().
neigora at 2007-7-7 15:12:37 > top of Java-index,Archived Forums,Socket Programming...
# 4

Now, with:

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.awt.image.*;

import javax.swing.*;

import java.io.*;

import javax.imageio.*;

public class ImProc extends JComponent{

private BufferedImage source, destination;

private JComboBox options;

public ImProc( BufferedImage image){

source = destination = image;

setBackground(Color.white);

setLayout( new BorderLayout());

JPanel controls = new JPanel();

options = new JComboBox(

new String[] {"[source]", "brighten", "darken", "rotate", "scale" }

);

options.addItemListener(new ItemListener(){

public void itemStateChanged( ItemEvent ie){

String option = (String)options.getSelectedItem();

BufferedImageOp op = null;

if(option.equals("[source]"))

destination = source;

else if(option.equals("brighten"))

op = new RescaleOp(1.5f, 0, null);

else if(option.equals("darken"))

op = new RescaleOp(0.5f, 0, null);

else if (option.equals("rotate"))

op = new AffineTransformOp(

AffineTransform.getRotateInstance(Math.PI / 6), null);

else if (option.equals("scale"))

op = new AffineTransformOp(

AffineTransform.getScaleInstance(.5, .5), null);

if(op != null) destination = op.filter(source, null);

repaint();

}

});

controls.add(options);

add(controls, BorderLayout.SOUTH);

}

public void paintComponent(Graphics g){

int imageWidth = destination.getWidth();

int imageHeight = destination.getHeight();

int width = getSize().width;

int height = getSize().height;

g.drawImage(destination,

(width - imageWidth) / 2, (height - imageHeight) / 2, null);

}

public static void main(String[] args){

JFrame frame = new JFrame("Image");

frame.setSize(300, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

JFileChooser chooser = new JFileChooser();

//String filename = chooser.getName();

File f = chooser.getSelectedFile();

//String filename = f.getName();

//ImageIcon icon = new ImageIcon(filename);

//Image i = ImageIO.read(f);

//int w = i.getWidth(null), h = i.getHeight(null);

//BufferedImage buffImage = null;

try{

BufferedImage buffImage = ImageIO.read(f);

}

catch (IOException e){

System.out.println("Error: " + e.getMessage());

}

frame.getContentPane().add(new ImProc(buffImage));

Graphics2D imageGraphics = buffImage.createGraphics();

imageGraphics.drawImage(buffImage, 0, 0, null);

}

}

With this, it doesn't seem to find my variable buffImage. However, it complains if I don't use the try{} catch{}. Am I not declaring something properly?

jmarottaa at 2007-7-7 15:12:37 > top of Java-index,Archived Forums,Socket Programming...
# 5
Never mind, I didn't initialize it properly. Thanks for all your help.
jmarottaa at 2007-7-7 15:12:37 > top of Java-index,Archived Forums,Socket Programming...
# 6

Sorry, one more stupid newbie question. I have the JFrame exactly how I want it; I'm looking to open the file, and I think it does. I just don't know where to put the Graphics2D drawImage(); commands properly to actually display the image in the frame. See below:

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import javax.imageio.*;

import javax.swing.*;

import java.awt.image.*;

public class ImFrame extends JFrame implements ActionListener

{

private JPanel editframe = new JPanel();

public ImFrame(){

super("Image Display Frame");

BufferedImage buffImage = null;

Container content = getContentPane();

content.add(new JScrollPane(editframe), BorderLayout.CENTER);

//JFrame frame = new JFrame("Image Display Frame");

JMenu menu = new JMenu("File");

menu.add(makeMenuItem("Open"));

menu.add(makeMenuItem("Quit"));

JMenuBar menuBar = new JMenuBar();

menuBar.add(menu);

setJMenuBar(menuBar);

setSize(300, 300);

setVisible(true);

setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);

Graphics2D imGraph = buffImage.createGraphics();

imGraph.drawImage(buffImage,0,0,null);

}

public void actionPerformed(ActionEvent e){

String command = e.getActionCommand();

if(command.equals("Quit")) System.exit(0);

else if (command.equals("Open")) loadFile();

}

private void loadFile(){

BufferedImage buffImage = null;

JFileChooser chooser = new JFileChooser();

int result = chooser.showOpenDialog(this);

if (result == JFileChooser.CANCEL_OPTION) return;

try{

File file = chooser.getSelectedFile();

buffImage = ImageIO.read(file);

}

catch(Exception e){

System.out.println("File error:" + e);

}

}

private JMenuItem makeMenuItem(String name){

JMenuItem m = new JMenuItem(name);

m.addActionListener(this);

return m;

}

public static void main(String[] s){

new ImFrame().setVisible(true);

}

}

Thanks!

Joe

jmarottaa at 2007-7-7 15:12:37 > top of Java-index,Archived Forums,Socket Programming...
# 7

Get rid of

Graphics2D imGraph = buffImage.createGraphics();

imGraph.drawImage(buffImage,0,0,null);

and replace it with

buffImage=loadFile();

and finally, make the loadFile method's return type BufferedImage so that

private BufferedImage loadFile()

and at the end of the loadFile method, return buffImage.

FortisVenalitera at 2007-7-7 15:12:37 > top of Java-index,Archived Forums,Socket Programming...
# 8

Still no luck. I think there's something that's simply not being painted here. Setting the Graphics2D stuff to buffImage = loadFile(); simply brought up the JFileChooser without any prompt.

Do I need to paint the object onto a panel first, and then embed the panel into the frame? If so, how do I do that?

Thanks!

jmarottaa at 2007-7-7 15:12:37 > top of Java-index,Archived Forums,Socket Programming...