| | |
Stretch Image in JLabel
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 53
Reputation:
Solved Threads: 2
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
here,
rstmp = Recordset contain the records data
LblPhoto = JLabel which displays the Image
please give me sample code so i can Fix it.
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);rstmp = Recordset contain the records data
LblPhoto = JLabel which displays the Image
please give me sample code so i can Fix it.
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.
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
----------------------------------------------
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
No one told you to override paintComponent.
Load the image into a BufferedImage, get the Graphics2D, modify it, then create a ImageIcon from that.
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
----------------------------------------------
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
•
•
Join Date: Apr 2008
Posts: 53
Reputation:
Solved Threads: 2
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.
in above code all methods execute when i execute program but No Display on JFrame or panel
Please HELP!!!!!!!!!
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 classin above code all methods execute when i execute program but No Display on JFrame or panel
Please HELP!!!!!!!!!
•
•
Join Date: Apr 2008
Posts: 53
Reputation:
Solved Threads: 2
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 classBut 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
![]() |
Other Threads in the Java Forum
- Previous Thread: Help with compiling a Servlet
- Next Thread: Mobile applications
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application apps arguments array arrays automation balls binary bluetooth card chat class classes client code component consumer database draw eclipse ee error event exception file fractal free game gameprogramming gis givemetehcodez graphics gui helpwithhomework html ide image input integer j2me j2seprojects java javaprojects jmf jni jpanel julia jvm key linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar netbeans newbie nextline nls notdisplaying number oracle output print problem program programming project recursion recursive scanner screen security server set size sms socket sort spamblocker sql sqlite string sun swing terminal test threads time tree trolltech windows






