5 Replies - 1266 Views - Last Post: 24 April 2010 - 04:35 PM Rate Topic: -----

#1 Guest_Tripwire*


Reputation:

Picture troubles

Posted 22 April 2010 - 04:03 PM

Hi Guys,

I got a problem with my program, i want to write a bufferedimage to my JFrame but it won't show up. I tryed a few things but nothing seemed to work.
Can someone fill me in with the code i need to add my image to my JFrame for image processing?

here's my code:
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;



public class Frame extends JFrame implements ActionListener {

	JFrame frame = new JFrame("Graphics");
	JMenuBar mb = new JMenuBar();
	JFileChooser fileChooser = new JFileChooser();
	File file;
	BufferedImage image = null;
	JLabel label;

	
	public Frame()
	{
		//filechooser settings
		FileNameExtensionFilter filter = new FileNameExtensionFilter("Image files", "jpg", "gif", "jpeg", "psd");
		fileChooser.setFileFilter(filter);
		
		//frame settings
		frame.setSize(1000, 1000);
		frame.setResizable(false);
		
		//Setting Menubar
		frame.setJMenuBar(mb);
		
		//fileMenu
		JMenu fileMenu = new JMenu("File");
		mb.add(fileMenu);
		
		//JMenuItems
		JMenuItem open = new JMenuItem("Open image");
		JMenuItem close = new JMenuItem("Close image");
		JMenuItem exit = new JMenuItem("Exit");
		
		//Adding items to fileMenu
		fileMenu.add(open);
		fileMenu.add(close);
		fileMenu.add(exit);
		
		//Adding actionlisteners
		open.addActionListener(this);
		close.addActionListener(this);
		exit.addActionListener(this);
		
		
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("Open image"))
		{
			int ret = fileChooser.showDialog(null, "Open image");
			if(ret == JFileChooser.APPROVE_OPTION)
			{
				loadImage();	
			}	
		}
		if(e.getActionCommand().equals("Close image"))
		{
			JOptionPane.showMessageDialog(null, "This button isn't doing anything right now :D/>", "Error", JOptionPane.ERROR_MESSAGE);
		}
		if(e.getActionCommand().equals("Exit"))
		{
			System.exit(0);
		}
		
	}
	
	public void loadImage() 
	{
		try
		{
			file = fileChooser.getSelectedFile();
			image = ImageIO.read(file);
		;
		
		}catch (Exception e1) {
			JOptionPane.showMessageDialog(null, "Please open an image file", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
}



Is This A Good Question/Topic? 0

Replies To: Picture troubles

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Picture troubles

Posted 22 April 2010 - 07:47 PM

A few things. First, you have a rogue semi-colon instead of a brace at the end of your try-block.
try { 
     file = fileChooser.getSelectedFile(); 
     image = ImageIO.read(file); 
     ; //this should be an end brace 



Next, there are a few things with these two lines. First, you import everything from the java.awt package, including java.awt.Frame. You also name your class Frame. I would avoid doing this, as the naming is misleading. I would instead name your class something like MyFrame. Also, why do you subclass JFrame if you want to create a JFrame instance variable in your class? The whole point of subclassing JFrame is so that you can treat the class as a JFrame.
public class Frame extends JFrame implements ActionListener { 
 
        JFrame frame = new JFrame("Graphics"); 



As for displaying your image in the JFrame, you need to override the paint() method JFrame inherits from JComponent. It has a Graphics param which you can use to do your painting, like so:
public void paint(Graphics g){
   super.paint(g);
   //use the Graphics drawImage() method to paint the Image
   //to the JFrame
}



For more information on the Graphics class, check out the API:
http://java.sun.com/...t/Graphics.html
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Tripwire*


Reputation:

Re: Picture troubles

Posted 23 April 2010 - 01:33 AM

thx for pointing out the few faults i had ! overlooked them i guess. I edited the code a bit but still there is no picture showing up in my frame :(. here's my code:

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



public class MyFrame extends JFrame implements ActionListener {

	JMenuBar mb = new JMenuBar();
	JFileChooser fileChooser = new JFileChooser();
	File file;
	BufferedImage image = null;
	int height ,width;

	
	public MyFrame()
	{
		fileChooserSettings(); 		//applies various fileChooser settings
		addFileMenu();				//Adding the fileMenu
		frameSettings();			//applies various frame settings
		
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("Open image"))
		{
			int ret = fileChooser.showDialog(null, "Open image");
			if(ret == JFileChooser.APPROVE_OPTION)
			{
				loadImage();	
			}	
		}
		if(e.getActionCommand().equals("Close image"))
		{
			JOptionPane.showMessageDialog(null, "This button isn't doing anything right now :D/>", "Error", JOptionPane.ERROR_MESSAGE);
		}
		if(e.getActionCommand().equals("Exit"))
		{
			System.exit(0);
		}
		
	}
	public void loadImage() 
	{
		try
		{
			file = fileChooser.getSelectedFile();
			image = ImageIO.read(file);
			height = image.getHeight();
			width = image.getHeight();
			
			
			
		}catch (Exception e1) {
			JOptionPane.showMessageDialog(null, "Please open an image file", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		g.drawImage(image, width, height, null);
		
	}
	
	
	private void addFileMenu()
	{
		//fileMenu
		JMenu fileMenu = new JMenu("File");
		mb.add(fileMenu);
		
		//JMenuItems
		JMenuItem open = new JMenuItem("Open image");
		JMenuItem close = new JMenuItem("Close image");
		JMenuItem exit = new JMenuItem("Exit");
		
		//Adding items to fileMenu
		fileMenu.add(open);
		fileMenu.add(close);
		fileMenu.add(exit);
		
		//Adding actionlisteners
		open.addActionListener(this);
		close.addActionListener(this);
		exit.addActionListener(this);
	}
	
	private void fileChooserSettings()
	{
		FileNameExtensionFilter filter = new FileNameExtensionFilter("Image files", "jpg", "gif", "jpeg", "psd");
		fileChooser.setFileFilter(filter);
	}
	
	private void frameSettings()
	{
		getContentPane();
		setTitle("Graphics");
		setSize(1000, 1000);
		setResizable(false);
		setJMenuBar(mb);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
	}
	
	
}


Was This Post Helpful? 0

#4 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Picture troubles

Posted 23 April 2010 - 03:44 AM

You need to override a JPanel and use it to paint (JFrame can do it, just a little complex. I used an internal class. See?
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.Graphics;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;



public class MyFrame extends JFrame implements ActionListener {

        JMenuBar mb = new JMenuBar();
        JFileChooser fileChooser = new JFileChooser();
        File file;
        BufferedImage image = null;
        int height ,width;
        
        PicPanel pan = new PicPanel();

        
        public MyFrame()
        {
                fileChooserSettings();          //applies various fileChooser settings
                addFileMenu();                          //Adding the fileMenu
                frameSettings();                        //applies various frame settings
                
        }
        
        public void actionPerformed(ActionEvent e) {
                if(e.getActionCommand().equals("Open image"))
                {
                        int ret = fileChooser.showDialog(null, "Open image");
                        if(ret == JFileChooser.APPROVE_OPTION)
                        {
                                loadImage();    
                        }       
                }
                if(e.getActionCommand().equals("Close image"))
                {
                        JOptionPane.showMessageDialog(null, "This button isn't doing anything right now :D/>", "Error", JOptionPane.ERROR_MESSAGE);
                }
                if(e.getActionCommand().equals("Exit"))
                {
                        System.exit(0);
                }
                
        }
        public void loadImage() 
        {
                try
                {
                       pan.setPic();
                       pan.repaint();
                        
                }catch (Exception e1) {
                        JOptionPane.showMessageDialog(null, "Please open an image file", "Error", JOptionPane.ERROR_MESSAGE);
                }
        }
        
        private class PicPanel extends JPanel {
        	BufferedImage img = null;
        	
        	public void setPic() {
        		try {
					img = ImageIO.read(fileChooser.getSelectedFile());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	}
        	
        	public void paint(Graphics g) {
        		super.paint(g);
        		if (img != null)
        			g.drawImage(img, 0, 0, null);
        	}
        }
        
        
        private void addFileMenu()
        {
                //fileMenu
                JMenu fileMenu = new JMenu("File");
                mb.add(fileMenu);
                
                //JMenuItems
                JMenuItem open = new JMenuItem("Open image");
                JMenuItem close = new JMenuItem("Close image");
                JMenuItem exit = new JMenuItem("Exit");
                
                //Adding items to fileMenu
                fileMenu.add(open);
                fileMenu.add(close);
                fileMenu.add(exit);
                
                //Adding actionlisteners
                open.addActionListener(this);
                close.addActionListener(this);
                exit.addActionListener(this);
        }
        
        private void fileChooserSettings()
        {
                FileNameExtensionFilter filter = new FileNameExtensionFilter("Image files", "jpg", "gif", "jpeg", "psd");
                fileChooser.setFileFilter(filter);
        }
        
        private void frameSettings()
        {
                getContentPane();
                setTitle("Graphics");
                setSize(1000, 1000);
                setResizable(false);
                setJMenuBar(mb);
                add(pan);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setVisible(true);
                
        }
        
        public static void main(String [] args) {
        	new MyFrame();
        }
        
        
}



You might want to tidy it up as I was in a hurry to finish it.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Tripwire*


Reputation:

Re: Picture troubles

Posted 24 April 2010 - 02:42 AM

thx for the help but it still isn't working the code i got now:

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


public class MyFrame extends JFrame implements ActionListener {

	JMenuBar mb = new JMenuBar();
	JFileChooser fileChooser = new JFileChooser();
	File file;
	PicPanel pan = new PicPanel();

	
	public MyFrame()
	{
		fileChooserSettings(); 		        //applies various fileChooser settings
		addFileMenu();				//Adding the fileMenu
		frameSettings();			//applies various frame settings
		
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("Open image"))
		{
			int ret = fileChooser.showDialog(null, "Open image");
			if(ret == JFileChooser.APPROVE_OPTION)
			{
				loadImage();	
			}	
		}
		if(e.getActionCommand().equals("Close image"))
		{
			JOptionPane.showMessageDialog(null, "This button isn't doing anything right now :D/>", "Error", JOptionPane.ERROR_MESSAGE);
		}
		if(e.getActionCommand().equals("Exit"))
		{
			System.exit(0);
		}
		
	}
	public void loadImage() 
	{
			pan.setPic();
			pan.repaint();
	}
	
	private class PicPanel extends JPanel
	{
		BufferedImage img = null;
	
		public void setPic()
		{
			try
			{
				img = ImageIO.read(fileChooser.getSelectedFile());
				
			}catch(IOException e)
			{
				JOptionPane.showMessageDialog(null, "Please open an image file", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		
		public void paint(Graphics g)
		{
			super.paint(g);
			if(img != null)
				g.drawImage(img, 0, 0, null);
		}
	}
	
	
	private void addFileMenu()
	{
		//fileMenu
		JMenu fileMenu = new JMenu("File");
		mb.add(fileMenu);
		
		//JMenuItems
		JMenuItem open = new JMenuItem("Open image");
		JMenuItem close = new JMenuItem("Close image");
		JMenuItem exit = new JMenuItem("Exit");
		
		//Adding items to fileMenu
		fileMenu.add(open);
		fileMenu.add(close);
		fileMenu.add(exit);
		
		//Adding actionlisteners
		open.addActionListener(this);
		close.addActionListener(this);
		exit.addActionListener(this);
	}
	
	private void fileChooserSettings()
	{
		FileNameExtensionFilter filter = new FileNameExtensionFilter("Image files", "jpg", "gif", "jpeg", "psd");
		fileChooser.setFileFilter(filter);
	}
	
	private void frameSettings()
	{
		getContentPane();
		setTitle("Graphics");
		setSize(1000, 1000);
		setResizable(false);
		setJMenuBar(mb);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
	}
	
	
}


Was This Post Helpful? 0

#6 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Picture troubles

Posted 24 April 2010 - 04:35 PM

You simply forgot to add the picture panel to the frame! :P

private void frameSettings()
        {
                getContentPane();
                setTitle("Graphics");
                setSize(1000, 1000);
                setResizable(false);
                setJMenuBar(mb);
                add(pan); // Add the picture panel!
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setVisible(true);
                
        }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1