Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,694 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,307 people online right now. Registration is fast and FREE... Join Now!




Setting Size of the Image

 
Reply to this topicStart new topic

Setting Size of the Image, sample code to make the size of image being displayed fixed irrespecti

sailajaensure
post 18 Jun, 2008 - 01:26 AM
Post #1


New D.I.C Head

*
Joined: 17 Apr, 2008
Posts: 7

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
User is offlineProfile CardPM

Go to the top of the page

pbl
post 18 Jun, 2008 - 03:38 AM
Post #2


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,982



Thanked 190 times

Dream Kudos: 75
My Contributions


QUOTE(sailajaensure @ 18 Jun, 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


Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.

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.
User is offlineProfile CardPM

Go to the top of the page

sailajaensure
post 21 Jun, 2008 - 04:17 AM
Post #3


New D.I.C Head

*
Joined: 17 Apr, 2008
Posts: 7

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.

CODE

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,l
19,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,bph
oto3,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();
}

}

User is offlineProfile CardPM

Go to the top of the page

pbl
post 21 Jun, 2008 - 10:00 AM
Post #4


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,982



Thanked 190 times

Dream Kudos: 75
My Contributions


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


CODE

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 Jun, 2008 - 11:05 AM
User is offlineProfile CardPM

Go to the top of the page

sailajaensure
post 26 Jun, 2008 - 03:05 AM
Post #5


New D.I.C Head

*
Joined: 17 Apr, 2008
Posts: 7

i have tried that code but iam unable to resize that image.so help me out in detailed..
User is offlineProfile CardPM

Go to the top of the page

pbl
post 26 Jun, 2008 - 03:18 AM
Post #6


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,982



Thanked 190 times

Dream Kudos: 75
My Contributions


QUOTE(sailajaensure @ 26 Jun, 2008 - 04:05 AM) *

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 Jun, 2008 - 02:11 PM
User is offlineProfile CardPM

Go to the top of the page

RautRupali
post 26 Jun, 2008 - 08:53 PM
Post #7


New D.I.C Head

*
Joined: 7 Jun, 2007
Posts: 44


My Contributions


QUOTE(pbl @ 26 Jun, 2008 - 04:18 AM) *

QUOTE(sailajaensure @ 26 Jun, 2008 - 04:05 AM) *

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
User is offlineProfile CardPM

Go to the top of the page

pbl
post 26 Jun, 2008 - 09:04 PM
Post #8


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,982



Thanked 190 times

Dream Kudos: 75
My Contributions


CODE

            imageLabel1 = new ImageLabel(filename);


Nothing more complicated
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:47AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month