Setting Size of the Image

sample code to make the size of image being displayed fixed irrespecti

Page 1 of 1

7 Replies - 25328 Views - Last Post: 26 June 2008 - 10:04 PM Rate Topic: ----- 1 Votes

#1 sailajaensure  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-April 08

Setting Size of the Image

Posted 18 June 2008 - 02:26 AM

hi...could any one help me out in this .I need to make the size of image being displayed fixed irrespective of original size of the image using swings
Is This A Good Question/Topic? 0
  • +

Replies To: Setting Size of the Image

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8029
  • View blog
  • Posts: 31,164
  • Joined: 06-March 08

Re: Setting Size of the Image

Posted 18 June 2008 - 04:38 AM

View Postsailajaensure, on 18 Jun, 2008 - 02:26 AM, said:

hi...could any one help me out in this .I need to make the size of image being displayed fixed irrespective of original size of the image using swings


[rules][/rules]

And we will need more details..... how do you display your image ? By hand, in a JLabel ?
If it is in a JLabel it might be done automatically.
Was This Post Helpful? 0
  • +
  • -

#3 sailajaensure  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-April 08

Re: Setting Size of the Image

Posted 21 June 2008 - 05:17 AM

hi..I have taken a JLayeredPane in that iam adding JLabel.On to this JLabel iam able to retrieve the image from database but the thing is if the size of the image is so large thn i need to set the size of the image of same size of my JLayeredPane (250x250)[widthxheight]
In Mysql i hve created database name---skybirds,tablename--photo,column names---image1 LONGBLOB,image2 LONGBLOB,image3 LONGBLOB.so in short my requirement is any size of the image should be set manually while retrieving from database.
in my code i am retreiving image from database on to a label which is placed in JLayeredPane.so the image should be set in to that JLayeredPane if that image size is very large also.
I am sending my code also.jst look at the JLayeredPane(lp2).for remaining part i didnt write code.jst for retrieving image only i hve written code.if u want to run this code jst create database name---skybirds,tablename--photo,column names---image1 LONGBLOB,image2 LONGBLOB,image3 LONGBLOB.
i hope u hve understood my requirement and iam eagerly waiting for solution from you.thank u for responding quickly.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.BorderFactory.*;
import javax.swing.border.Border.*;
import javax.swing.border.TitledBorder.*;
import java.text.SimpleDateFormat;
import java.sql.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.io.InputStream;
//import javax.sound.sampled.*;
public class Views extends JFrame  implements ActionListener
{
JLabel lph,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15,lb1,lb2,lb3,l16,l17,l18,l19,lphoto1,lphoto2,lphoto3,laudio;
JLayeredPane lp1,lp2,lp3,lp4,lp5,lp6;
JPanel np,cp,sp,p,p1,p2,p3,ph,pp;
JButton b1,b2,b3,bph,baudio1,baudio2,baudio3,bvideo1,bvideo2,bvideo3,bphoto1,bphoto2,bphoto3,baudio;
JTextField tx1,tx2,tx3,tx4,tx5,tx6,tx7,tx8,tx9;
 BufferedImage bimg,bimg1,bimg2;
 InputStream in,in1,in2;
 Cursor csrhand = new Cursor(Cursor.HAND_CURSOR);
public Views()
{
setSize(750,550);
l1=new JLabel("Name:");
l2=new JLabel("Location:");
l3=new JLabel("City:");
l4=new JLabel("State:");
l5=new JLabel("Talent:");
l6=new JLabel("Type:");
l7=new JLabel("Age:");
l8=new JLabel("No. of Shows:");
l9=new JLabel("Awards:");
laudio=new JLabel("View Audio");

lphoto1=new JLabel("Photo1");
lphoto2=new JLabel("Photo2");
lphoto3=new JLabel("Photo3");
lph=new JLabel();

tx1=new JTextField(5);
tx2=new JTextField(5);

tx3=new JTextField(5);
tx4=new JTextField(5);
tx5=new JTextField(5);
tx6=new JTextField(5);
tx7=new JTextField(5);
tx8=new JTextField(5);
tx9=new JTextField(5);
lb1=new JLabel();
lb2=new JLabel();
lb3=new JLabel();
bph=new JButton("Photo1");
lp1=new JLayeredPane();
lp2=new JLayeredPane();
lp3=new JLayeredPane();
lp4=new JLayeredPane();
lp5=new JLayeredPane();
lp6=new JLayeredPane();

p=new JPanel();
np=new JPanel();
cp=new JPanel();
sp=new JPanel();
p=new JPanel();
p1=new JPanel();

bvideo1=new JButton("Video1");
bvideo1.setCursor(csrhand);
bvideo1.addActionListener(this);
bvideo2=new JButton("Video2");
bvideo2.setCursor(csrhand);
bvideo2.addActionListener(this);
bvideo3=new JButton("Video3");
bvideo3.setCursor(csrhand);
bvideo3.addActionListener(this);


baudio1=new JButton("Audio1");
baudio1.addActionListener(this);
baudio1.setCursor(csrhand);
baudio2=new JButton("Audio2");
baudio2.addActionListener(this);
baudio2.setCursor(csrhand);
baudio3=new JButton("Audio3");
baudio3.addActionListener(this);
baudio3.setCursor(csrhand);
//LayeredPane1
pp=new JPanel();
np.setLayout(new GridLayout(1,2));
lp1.setBorder(BorderFactory.createTitledBorder(""));
lp1.setPreferredSize(new Dimension(350,350));
lp1.setLayout(new GridLayout(9,1,4,6));
lp1.add(l1);
lp1.add(tx1);
lp1.add(l2);
lp1.add(tx2);
lp1.add(l3);
lp1.add(tx3);
lp1.add(l4);
lp1.add(tx4);
lp1.add(l5);
lp1.add(tx5);
lp1.add(l6);
lp1.add(tx6);
lp1.add(l7);
lp1.add(tx7);
lp1.add(l8);
lp1.add(tx8);
lp1.add(l9);
lp1.add(tx9);
//bph.addActionListener(this);

Font g=new Font("serif",1,20);
np.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Customer Details",javax.swing.border.TitledBorder.CENTER,javax.swing.border.TitledBorder.ABOVE_TOP,g,Color.red));
np.add(lp1);
//np.setBackground(Color.cyan);


//ph=new JPanel();
lp2.setBorder(BorderFactory.createTitledBorder("Photos"));
lp2.setPreferredSize(new Dimension(250,250));
lp2.setLayout(new GridLayout(1,0));
lp2.add(lph);

/*lb1=new JLabel("1|");
lb2=new JLabel("2|");
lb3=new JLabel("3|");
lp3.setPreferredSize(new Dimension(150,50));
lp3.setLayout(new GridLayout(1,3));
lp3.add(lb1);
lp3.add(lb2);
lp3.add(lb3);*/

b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b1.setCursor(csrhand);
b2.setCursor(csrhand);
b3.setCursor(csrhand);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
lp3.setPreferredSize(new Dimension(150,30));
lp3.setLayout(new GridLayout(1,3,4,6));
lp3.add(b1);
lp3.add(b2);
lp3.add(b3);


pp.add(lp2);
pp.add(lp3);
np.add(pp);

getContentPane().add(np,"North");




cp.setLayout(new GridLayout(2,1));
lp4=new JLayeredPane();
lp4.setBorder(BorderFactory.createTitledBorder("Videos"));
lp4.setPreferredSize(new Dimension(30,10));
lp4.setLayout(new GridLayout(1,3,4,6));
lp4.add(bvideo1);
lp4.add(bvideo2);
lp4.add(bvideo3);
cp.add(lp4);
//cp.setBackground(Color.cyan);



lp5=new JLayeredPane();
lp5.setBorder(BorderFactory.createTitledBorder("Audio"));
lp5.setPreferredSize(new Dimension(30,10));
lp5.setLayout(new GridLayout(1,3,4,6));
lp5.add(baudio1);
lp5.add(baudio2);
lp5.add(baudio3);
cp.add(lp5);
//cp.setBackground(Color.cyan);
getContentPane().add(cp,"Center");
//lph.setIcon(new javax.swing.ImageIcon("birds.jpg"));
try
{

//loadin of the Driver
Class.forName( "com.mysql.jdbc.Driver" );
Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/skybirds", "root", "root" );

Statement st1=conn.createStatement();
//PreparedStatement ps=conn.prepareStatement(connect);
ResultSet rs=st1.executeQuery("select *  from  photo ");

while(rs.next())
{
Blob blob = rs.getBlob(1);
in = blob.getBinaryStream();
//bimg1 =ImageIO.read(in);
//lph.setIcon(new javax.swing.ImageIcon(bimg1));
//lph.setVisible(true);
//System.out.println(blob);
//System.out.println(in);
Blob blob1 = rs.getBlob(2);
in1 = blob1.getBinaryStream();
//lph.setIcon(new javax.swing.ImageIcon(bimg1));
//lph.setVisible(true);
//System.out.println(blob1);
Blob blob2 = rs.getBlob(3);
in2 = blob2.getBinaryStream();
//lph.setIcon(new javax.swing.ImageIcon(bimg));
//lph.setVisible(true);
//System.out.println(blob2);

}


rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
 
addWindowListener(new WindowAdapter()
{
public void WindowClosing(WindowEvent we)
{
System.exit(0);
}
});
show();
}

public void actionPerformed(ActionEvent ae)
{
	
	try
{
	String str1= ae.getActionCommand();
if(str1.equals("1"))
{
System.out.println("action1");
 bimg =ImageIO.read(in);
 
 
 //System.out.println(bimg);
lph.setIcon(new javax.swing.ImageIcon(bimg));
lph.setVisible(true);
}
 
else
if(str1.equals("2"))
{
System.out.println("action2");
bimg1 =ImageIO.read(in1);
lph.setIcon(new javax.swing.ImageIcon(bimg1));
lph.setVisible(true);
}
else
if(str1.equals("3"))
{
System.out.println("action3");
 bimg2 =ImageIO.read(in2);
lph.setIcon(new javax.swing.ImageIcon(bimg2));
lph.setVisible(true);
}
}
catch (Exception e)
	{
}
}

public static void main(String args[])
{
new Views();
}

}



Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8029
  • View blog
  • Posts: 31,164
  • Joined: 06-March 08

Re: Setting Size of the Image

Posted 21 June 2008 - 11:00 AM

Build a class that extends JLabel
a constructor that receives the file to display or an Icon
add a setIcon() method that overload setIcon() of JLabel
overload the paint() method of that class
and redraw yourself the image


class ImageLabel extends JLabel {
   Image image;
   ImageObserver imageObserver; 

   // constructor with filename		
   ImageLabel(String filename) {
	  ImageIcon icon = new ImageIcon(filename);
	  image = icon.getImage();
	  imageObserver = icon.getImageObserver();
   }

   // constructor with icon
   ImageLabel(ImageIcon icon) {
	  image = icon.getImage();
	  imageObserver = icon.getImageObserver();
   }

   // overload setIcon method
   void setIcon(ImageIcon icon) {
	  image = icon.getImage();
	  imageObserver = icon.getImageObserver();
   }

   // overload paint()
   public void paint( Graphics g ) {
	   super.paint( g );
	   g.drawImage(image,  0 , 0 , getWidth() , getHeight() , imageObserver);
   }
}


This post has been edited by pbl: 21 June 2008 - 12:05 PM

Was This Post Helpful? 0
  • +
  • -

#5 sailajaensure  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-April 08

Re: Setting Size of the Image

Posted 26 June 2008 - 04:05 AM

i have tried that code but iam unable to resize that image.so help me out in detailed..
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8029
  • View blog
  • Posts: 31,164
  • Joined: 06-March 08

Re: Setting Size of the Image

Posted 26 June 2008 - 04:18 AM

View Postsailajaensure, on 26 Jun, 2008 - 04:05 AM, said:

i have tried that code but i am unable to resize that image.so help me out in detailed..


You don't have to resize the image, the code does it for you.
I have tested it .. it works

For the Labels that display image replace "new JLabel()" by "new ImageLabel()"

This post has been edited by pbl: 26 June 2008 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

#7 RautRupali  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 07-June 07

Re: Setting Size of the Image

Posted 26 June 2008 - 09:53 PM

View Postpbl, on 26 Jun, 2008 - 04:18 AM, said:

View Postsailajaensure, on 26 Jun, 2008 - 04:05 AM, said:

i have tried that code but i am unable to resize that image.so help me out in detailed..


You don't have to resize the image, the code does it for you.
I have tested it .. it works

For the Labels that display image replace "new JLabel()" by "new ImageLabel()"

<quote>
Try this if it helps...
</quote>

<code>
ImageIcon tinyPicture= new ImageIcon(filename);
Image image= tinyPicture.getImage();
image = image.getScaledInstance(200, -1, image.SCALE_SMOOTH);
tinyPicture.setImage(image);
imageLabel1.setIcon(tinyPicture);
</code>

Thanks,

Rupali
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8029
  • View blog
  • Posts: 31,164
  • Joined: 06-March 08

Re: Setting Size of the Image

Posted 26 June 2008 - 10:04 PM

			imageLabel1 = new ImageLabel(filename);



Nothing more complicated
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1