Typically it is not the policy of this site to provide code without first seeing a try on your part. However, I had this lying around and it would show you how to setup a picture from a file and put it on an application. So in the future, please provide code that you are working on and showing an attempt to do it and we will do our best to help you from there. Thanks!
CODE
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");
public picpanel() { }
// Fires whenever this panel is resized
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(),this);
}
}
The above class loads in a picture from the C:\ drive. In this case "mypic.jpg". It loads it using the java.awt toolkit using getToolkit's getImage method. It then takes over the panels painting to draw the image.
CODE
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);
}
}
This driver program sets up our JFrame, creates our picpanel class instance and puts it on the frame along with a button. The button then has an action listener attached to it to respond to clicking. It adds 20 pixels to the width of the picture causing the repaint to fire. This will allow you to change the size of the picture dynamically.
Enjoy!
"At DIC we be java picture stretching code ninjas!"