11 Replies - 5978 Views - Last Post: 24 August 2007 - 10:22 AM Rate Topic: -----

#1 maillaksh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 16-August 07

Saving and Resizing Images in Swing

Post icon  Posted 18 August 2007 - 12:32 PM

I am creating a form using swing and JDBC and I am facing some problems in handling Images in swings:
1: I am able to display Image in JFrame through a JFileChooser but not able to resize the image if a big image is selected. I need the image of passport photo size.
2: now i need to save image (or anything which may help) in MsAccess as database for retrieval when ever needed.
please help!
Is This A Good Question/Topic? 0
  • +

Replies To: Saving and Resizing Images in Swing

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4313
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Re: Saving and Resizing Images in Swing

Posted 18 August 2007 - 12:59 PM

The trick here is to repaint the image using drawImage() from the Graphics object in the paintComponent() method. When the frame is resized, you can repaint and pass it the getHeight and getWidth of the frame.

public void paintComponent(Graphics g) 
{
 
	g.drawImage(picname,0,0, getHeight(),getWidth(),this);
 
}



The code above would be used in a class that extends something like a JPanel or what not. Of course you can then fire this with a repaint() call or whenever the program itself needs to resize and repaint.

As for your MS access question I have found a great link where they talk about that very thing. It includes code that you can use and seems to do the trick.

Storing and retrieving image data from MS Access through JDBC

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 maillaksh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 16-August 07

Re: Saving and Resizing Images in Swing

Posted 21 August 2007 - 11:08 AM

View PostMartyr2, on 18 Aug, 2007 - 12:59 PM, said:

The trick here is to repaint the image using drawImage() from the Graphics object in the paintComponent() method. When the frame is resized, you can repaint and pass it the getHeight and getWidth of the frame.

public void paintComponent(Graphics g) 
{
 
	g.drawImage(picname,0,0, getHeight(),getWidth(),this);
 
}


I have created a bordered panel for image and I want to fix every image I select into that fixed sized bordered panel only.If I select a big picture , it shows only a part of it and does not resize it in the panel.
Please tell me how to use paintComponent() method in my code. Here's the problem part of the code.
panel16=new JPanel();
  panel16.setLayout(new BorderLayout());
  gbc.gridx=2;
  gbc.gridy=0;
  gbc.gridheight=3;
  gbc.gridwidth=3;
  gbc.insets=new Insets(10,60,20,40);
  Border compound,raisedbevel, loweredbevel;
  raisedbevel = BorderFactory.createRaisedBevelBorder();
  loweredbevel = BorderFactory.createLoweredBevelBorder();
  compound = BorderFactory.createCompoundBorder(
			  raisedbevel, loweredbevel);
  panel16.setBorder(compound);
  panel16.setPreferredSize(new Dimension(100, 140));
  panel6.add(panel16,gbc);

  JButton jb16=new JButton("Browse",open);	   //adding "browse" and "save" buttons
  jb16.addActionListener(new ActionListener()
					   {
						public void actionPerformed(ActionEvent ae)
						{
						 JFileChooser fileChooser = new JFileChooser();
						 int returnValue;
						 returnValue = fileChooser.showOpenDialog(Design.this);
						 if (returnValue == JFileChooser.APPROVE_OPTION)
						 {
						  File file;
						  file = fileChooser.getSelectedFile();
						  String st=file.toString();
						   
						   
						  JLabel a = new JLabel();
						  iicon=new ImageIcon(file.getPath());
						  a.setIcon(iicon);
						  a.setPreferredSize(new Dimension(150, 100));
						  panel16.add(a,BorderLayout.CENTER);
						  
						  
						 }
						 
						}
					   });


Was This Post Helpful? 0
  • +
  • -

#4 maillaksh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 16-August 07

Re: Saving and Resizing Images in Swing

Posted 22 August 2007 - 07:58 AM

I urgently need to configure this out please help.
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4313
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Re: Saving and Resizing Images in Swing

Posted 22 August 2007 - 04:30 PM

I am SUCH a nice guy! I wrote a custom example for you to show you how it can work.

First of all, we define our custom JPanel and put in the paintComponent method. This component fires whenever we (or the program) needs to repaint an image. It repaints whenever the image is sized or a window covers the top of it etc. We can trigger the repaint using the repaint() method here. But for our example, it will trigger a repaint whenever we adjust the size of the panel.

import javax.swing.*;
import java.awt.*;

public class picpanel extends JPanel {
	// This is a custom image we are loading. You would need to change this or
	// come up with a dynamic location... most likely from a filechooser.

	Image img = getToolkit().getImage("c:\\mypic.jpg");

	// Fires whenever this panel is resized
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(img, 0, 0, getWidth(), getHeight(),this);
	}
}



Now that we got our custom panel with the image in it, we will create a custom program to show you how to implement the class and how we can manipulate it.

import javax.swing.*;
import java.awt.event.*;

public class mypic extends JFrame {
	// Get an instance of our class

	picpanel p = new picpanel();

	public mypic() {
		super("Picture Example");
		setLayout(null);

		// Set our panel's inital size (thus changing our picture's size)
		p.setBounds(10, 10, 100, 150);
		
		add(p);

		JButton changeSize = new JButton("Change");
		changeSize.setBounds(10, 170, 100, 30);

		// Add a listener to our change size button
		changeSize.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent ae) {
							// Change the size of the panel by adding 20 px to its width each time
							p.setSize(p.getWidth() + 20, 150);
						}
						});

		add(changeSize);
	}

	public static void main(String args[]) {
		mypic frame = new mypic();

		frame.setSize(300,300);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
}



Here we create a standard JFrame and instantiate a custom panel (our class) and a button to change the panel's size. We add a listener to the button so that when we click it, we adjust the width of the panel by adding 20 pixels to it. Each time we adjust the width, the paintComponent method of our class is going to be fired again and redraws it to the width of the panel.

Below is a screenshot of this program and how it looks.

Posted Image


Feel free to use the code for the panel and modify it as you like. This code is in the public domain.

I hope this all makes sense.

:snap:

This post has been edited by Martyr2: 22 August 2007 - 04:35 PM

Was This Post Helpful? 0
  • +
  • -

#6 maillaksh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 16-August 07

Re: Saving and Resizing Images in Swing

Posted 23 August 2007 - 08:09 AM

hey thanx...I got it working for me!!
Was This Post Helpful? 0
  • +
  • -

#7 maillaksh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 16-August 07

Re: Saving and Resizing Images in Swing

Posted 23 August 2007 - 08:38 AM

View Postmaillaksh, on 23 Aug, 2007 - 08:09 AM, said:

hey thanx...I got it working for me!!

sorry.. I still have a problem!!
I have coded actionlistener for BROWSE button like this

JButton jb16=new JButton("Browse",open);	   //adding "browse" and "save" buttons
  jb16.addActionListener(new ActionListener()
					   {
						public void actionPerformed(ActionEvent ae)
						{
						 JFileChooser fileChooser = new JFileChooser();
						 int returnValue;
						 returnValue = fileChooser.showOpenDialog(Design.this);
						 if (returnValue == JFileChooser.APPROVE_OPTION)
						 {
						  file = fileChooser.getSelectedFile();
						  img = getToolkit().getImage(file.getPath());
						  p=new picpanel(img); 
						 }
						 repaint();
						  panel16.add(p);
							
						}
							  
					   });



and changes the picpanel class like this

class picpanel extends JPanel
{
 Image imgp;
 picpanel(Image img)
 {
  this.imgp = img;
 }
						// Fires whenever this panel is resized
 public void paintComponent(Graphics g) 
 {
  super.paintComponent(g);
  g.drawImage(imgp, 0, 0, getWidth(), getHeight(),this);
 }
}



so now what happens is that ( all other things are going well ) but when I select an image after selecting one and minimize the frame window then the first image appears again replacing the newly selected image.
Now what to do!!
Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4313
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Re: Saving and Resizing Images in Swing

Posted 23 August 2007 - 09:32 AM

Well first of all you don't need to do an add in your actionlistener. The idea is that your panel is already added to the form and is sitting there, so your browse button should open the file chooser, they select the image, you set the current panel (our custom object) that is already on the form.

Without a full test (don't have real time today since it is a field trip at work.. yippeeee) I think you are just having a scoping problem. Try moving your repaint into the constructor of picpanel, that way you set the image and then force it to redraw itself. That and removing the panel6.add() call may help. You should have one picpnel on the form there and your browse changes it, not adding another one. This is at least what I understood you wanted to do... just change the image.

But my first step would be to look at the scope of your picpanel... you might be going out of scope and it is repainting the old value back in.

Let me know how it goes.
Was This Post Helpful? 0
  • +
  • -

#9 maillaksh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 16-August 07

Re: Saving and Resizing Images in Swing

Posted 23 August 2007 - 10:19 AM

I have added panel16 for the border around the image and was trying to add picpanel with image to panel16 so that image comes inside a border.
When I select an image after selecting one (without closing the application) , and minimize the frame window the first image appears again replacing the newly selected image.It also happens when I move to another tab and come back again.
Sir wanted to check the scope of picpanel ,so I am posting here the code for the image part and browse button only.
  import javax.swing.border.*;
import javax.swing.table.*;
import javax.swing.ImageIcon.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;

class Design extends JFrame 
{
 GridBagConstraints gbc=new GridBagConstraints();
 JPanel panel6;
  JTextField jtf16,jtf26,jtf36,jtf46;
  JPanel panel16;
  Image img;
  File file;
  picpanel p;
  ImageIcon iicon;
 public Design()
 {
  super("Tenant Form");
  setLayout(new BorderLayout());
  JTabbedPane tabbedPane=new JTabbedPane(); 

  gbc.insets=new Insets(2,2,2,2);
  gbc.anchor=GridBagConstraints.WEST;	
  gbc.fill = GridBagConstraints.HORIZONTAL;
  
  panel6 = new JPanel(); 
  tabbedPane.addTab("Related To Office",null,panel6,"sixth tab");
  panel6.setLayout(new GridBagLayout());
											//adding 4 labels and textfields vertically
  
 
  panel16=new JPanel();
  panel16.setLayout(new BorderLayout());
  gbc.gridx=2;
  gbc.gridy=0;
  gbc.gridheight=3;
  gbc.gridwidth=3;
  gbc.insets=new Insets(10,60,20,40);
  Border compound,raisedbevel, loweredbevel;
  raisedbevel = BorderFactory.createRaisedBevelBorder();
  loweredbevel = BorderFactory.createLoweredBevelBorder();
  compound = BorderFactory.createCompoundBorder(
			  raisedbevel, loweredbevel);
  panel16.setBorder(compound);
  panel16.setPreferredSize(new Dimension(100,140));
  panel6.add(panel16,gbc);

  JButton jb16=new JButton("Browse");	   //adding "browse" and "save" buttons
  jb16.addActionListener(new ActionListener()
					   {
						public void actionPerformed(ActionEvent ae)
						{
						 JFileChooser fileChooser = new JFileChooser();
						 int returnValue;
						 returnValue = fileChooser.showOpenDialog(Design.this);
						 if (returnValue == JFileChooser.APPROVE_OPTION)
						 {
						  file = fileChooser.getSelectedFile();
						  img = getToolkit().getImage(file.getPath());
						  p=new picpanel(img); 
						 }
						  panel16.add(p);
							
						}
							  
					   });
  gbc.insets=new Insets(10,60,20,40);
  gbc.gridx=2;
  gbc.gridy=3;
  gbc.gridheight=1;
  gbc.gridwidth=1;
  panel6.add(jb16,gbc);
  add(tabbedPane);
 }
}
class picpanel extends JPanel
{
 Image imgp;
 picpanel(Image img)
 {
  this.imgp = img;
  repaint();
 }
						// Fires whenever this panel is resized
 public void paintComponent(Graphics g) 
 {
  super.paintComponent(g);
  g.drawImage(imgp, 0, 0, getWidth(), getHeight(),this);
 }
}

public class Project
{
 public static void main(String args[])			   //main method for the program
 {
  Design tabbedwin= new Design();
  tabbedwin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  tabbedwin.setSize(900,700);
  tabbedwin.setVisible(true);
 }
}


Was This Post Helpful? 0
  • +
  • -

#10 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4313
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Re: Saving and Resizing Images in Swing

Posted 23 August 2007 - 11:28 AM

If this doesn't get answered by tonight I will address it around 8 or 9pm PST tonight hopefully. I simply don't have the time to test this just yet. Sorry.

:(
Was This Post Helpful? 0
  • +
  • -

#11 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4313
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Re: Saving and Resizing Images in Swing

Posted 24 August 2007 - 09:49 AM

Sorry I didn't get back to this sooner. As usual, whenever you mention a time something has to come along to ruin it. Oh well. Here is the solution to the problem. Every time you hit browse and choose a file you are creating a new image, passing the image to the class as you construct it. Like I had mentioned before, you should only create the panel once and once created, change the existing object with a new picture. So how do we do this? Well, it is a two step process. The first part is to create a public method for picpanel so we can get access to the image member variable. This is also known as an accessor method. So this would be added to picpanel's class...

public void setSourcePic(Image sourcepic) {
	this.imgp = sourcepic;
	repaint();
}



As you can see, this simply sets the variable and then repaints itself. The second part is in the actionlistener for the button where instead of constructing a new picpanel each time, we only construct it once (on the initial browse) and then change that panel for each browse after that. Here is what the code might look like...

if (p != null) {
		p.setSourcePic(img);
}
else { 
		p = new picpanel(img); 
		panel16.add(p);
}



If the panel exists, simply change its member variable and repaint. Otherwise you create a new panel and put it on the form. Originally what the problem you had was that if you put it on a form and forced a repaint()... through minimize and then maximize again... you were asking the original panel to repaint itself, not the new one you added through your browse. Actually I am glad this error happened because you wouldn't want to keep creating a new object each time they hit browse, this could be a potential memory leak and eats up resources each time you hit the button and created a new one.

The idea is to always put one up there and then modify it rather than getting a new image.

I hope this all makes sense. Good luck.

:snap:
Was This Post Helpful? 0
  • +
  • -

#12 maillaksh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 16-August 07

Re: Saving and Resizing Images in Swing

Posted 24 August 2007 - 10:22 AM

yahoooo! i am done with this part in my project.
thanx a lot Martyr2!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1