Stretch Image in JLabel

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 53
Reputation: hell_tej is an unknown quantity at this point 
Solved Threads: 2
hell_tej hell_tej is offline Offline
Junior Poster in Training

Stretch Image in JLabel

 
0
  #1
Feb 2nd, 2009
I ma creating a Project on Java Uaing editor NetBeans 6.0

In this project, i want to put Dynemic image on JLabel which gets the Image Path from Database. It works Properly BUT....

The size of JLabel autometicaly adjust
If the Image size is 1024x672 then JLabel covers whole Swing JInnerFrame.

How to fix this problem.how to stretch the image to fit in the size of JLabel.

Code for Image is as Follows
int i = jTable1.getSelectedRow();
rstmp.absolute(i+1);
ImageIcon img = new ImageIcon(rstmp.getString("photo"));
LblPhoto.setIcon(img);
here,
rstmp = Recordset contain the records data
LblPhoto = JLabel which displays the Image

please give me sample code so i can Fix it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 266
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Stretch Image in JLabel

 
0
  #2
Feb 3rd, 2009
Well, that's what you're telling to do (stay the original size, that is).

Try looking at the API docs for BufferedImage (and Graphics2D) and see if there is something there that can help you.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 53
Reputation: hell_tej is an unknown quantity at this point 
Solved Threads: 2
hell_tej hell_tej is offline Offline
Junior Poster in Training

Re: Stretch Image in JLabel

 
0
  #3
Feb 3rd, 2009
I tryed the following Code but it can't work

JblPhoto.add(new JLabel(new ImageIcon("Image Path")) {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(((ImageIcon)getIcon()).getImage(), 0, 0, getWidth(), getHeight(), null);
}
});
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 266
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Stretch Image in JLabel

 
0
  #4
Feb 3rd, 2009
No one told you to override paintComponent.

Load the image into a BufferedImage, get the Graphics2D, modify it, then create a ImageIcon from that.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 53
Reputation: hell_tej is an unknown quantity at this point 
Solved Threads: 2
hell_tej hell_tej is offline Offline
Junior Poster in Training

Re: Stretch Image in JLabel

 
0
  #5
Feb 4th, 2009
I tryed following code to get image
image gathered into image object but can't displayed on panel (Now i am using JPanel instad on JLabel)
the JPanel class' paintComponent can't invock when i run the program in NetBeans.

public class NewJFrame extends javax.swing.JFrame {
    NewClass imgobj;
    Container con;
    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
        //Pan1 obj=new Pan1();]
        BufferedImage bimg = null;
        try
        {
                // Track the image to completion.
            bimg = ImageIO.read(new File("D:\\Books\\splash.png"));
            
       imgobj = new NewClass(bimg);
       imgobj.setImage(bimg);
      Image img =    imgobj.getImage(bimg);
      imgobj.prepareImage(img, this.getHeight(),this.getWidth(), null);
               
       getContentPane().add(imgobj,BorderLayout.CENTER);
        
       System.out.println("Image Readed...");
                 
      }
      catch(Exception e)
      {
            System.out.println("The error in Frame..."+e);
        }
        
    }
     
     
              
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify
    private javax.swing.JPanel NewClass;
    // End of variables declaration
    
}
class NewClass extends JPanel implements Serializable {

    Image image = null;

    public NewClass() {      }

    public NewClass(Image image) {
        this.image = image;
        System.out.println("Class Panel Constructed...");
    }
   

    public void setImage(Image image){
        this.image = image;
      
        System.out.println("Image set...");
    }
    
    public Image getImage(Image image){
        return image;
    }
    
    @Override
    public void paint(Graphics g) {
        super.paintComponent(g); //paint background
        if (image != null) { //there is a picture: draw it
            int height = this.getSize().height;
            int width = this.getSize().width;         
            g.drawImage(image,0,0, width, height, this);
            System.out.println("paint Component method ...");
               //g.drawImage(image, 0, 0, this); //original image size 
         }  //end if
    } //end paint
} //end class

in above code all methods execute when i execute program but No Display on JFrame or panel

Please HELP!!!!!!!!!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,506
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 521
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Stretch Image in JLabel

 
0
  #6
Feb 4th, 2009
You should be overriding paintComponent(), not paint(), in your "NewClass" class.
(And find a new name for that class because NewClass is an awful name for a component)
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 53
Reputation: hell_tej is an unknown quantity at this point 
Solved Threads: 2
hell_tej hell_tej is offline Offline
Junior Poster in Training

Re: Stretch Image in JLabel

 
0
  #7
Feb 4th, 2009
hay man It solved by following code it not works because Paint method not initialized

public class NewJFrame extends javax.swing.JFrame {
    LoadImg imgobj;
    Container con;
    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
        //Pan1 obj=new Pan1();]
        BufferedImage bimg = null;
        try
        {
       
        // Track the image to completion.
            bimg = ImageIO.read(new File("D:\\Books\\1.jpg"));
            JPanel cont = (JPanel) getContentPane();
            //cont.setLayout(new BorderLayout());
            imgobj = new LoadImg(bimg);
            imgobj.setBounds(50,100, 200, 200);
            cont = new NewClass(bimg);
            imgobj.setImage(bimg);
            Image img = imgobj.getImage(bimg);
            imgobj.prepareImage(imgobj.getImage(bimg), this.getHeight(),this.getWidth(), null);
            System.out.println("Image Readed...");
            getContentPane().add(imgobj,BorderLayout.CENTER);
            
        }
        catch(Exception e)
        {
            System.out.println("the error in frame..."+e);
        }
        
    }
  }

class LoadImg extends JPanel implements Serializable {
    Image image = null;
    
    public LoadImg() {    
    }

    public LoadImg(Image image) {
        this.image = image;
        System.out.println("Class Panel Constructed...");
    }
   

    public void LoadImg(Image image){
        this.image = image;
      
        System.out.println("Image set...");
    }
    
    public Image LoadImg(Image image){
        return image;
    }
    
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g); //paint background
        if (image != null) { //there is a picture: draw it
            int height = this.getSize().height;
            int width = this.getSize().width;         
            g.drawImage(image,0,0, width, height, this);
            System.out.println("paint Component method ...");
               //g.drawImage(image, 0, 0, this); //original image size 
         }  //end if
    } //end paint
} //end class

But now i want to chang Image at runtime
fetching record to database at runtime. but the image can't drown
can i use method like update() is there please tell me the method name


And Yes Thanks for Replay
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 53
Reputation: hell_tej is an unknown quantity at this point 
Solved Threads: 2
hell_tej hell_tej is offline Offline
Junior Poster in Training

Re: Stretch Image in JLabel

 
0
  #8
Mar 10th, 2009
Sorry but now when i test that code on my fully Application i realize that it Doesen't Work. can you give me another code or idea ?which i use for displaying Image.

Sorry but above code is wrong and doesen't work. don't look at that junk code it's a brain eater code.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 1
Reputation: adhya2009 is an unknown quantity at this point 
Solved Threads: 0
adhya2009 adhya2009 is offline Offline
Newbie Poster

Re: Stretch Image in JLabel

 
0
  #9
Jun 26th, 2009
Hi,

In an attempt to paint multiple icons between text in a jlabel...
for eg., <text1> <icon1> <text2> <icon2> a sequence like this in a single jlabel.
kindly assist me in going about doing this.

Thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC