1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.image.ImageObserver;
public class AffImage implements ChangeListener {
private int size = 50;
JLabel picture;
Icon icon = new ImageIcon("Spongebob.gif");
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("Spongebob.gif");
public Component createComponents() {
JSlider slide = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
slide.addChangeListener(this);
slide.setAlignmentX(JLabel.CENTER);
picture = new JLabel(icon);
picture.setHorizontalAlignment(JLabel.CENTER);
picture.setAlignmentX(Component.CENTER_ALIGNMENT);
picture.setLabelFor(slide);
JPanel pane = new JPanel(new GridLayout(0, 2));
pane.add(slide);
pane.add(picture);
return pane;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("SwingApplication");
AffImage app = new AffImage();
Component contents = app.createComponents();
frame.getContentPane().add(contents, BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();
size=(int)source.getValue();
picture.imageUpdate(image, ImageObserver.WIDTH, 0, 0, icon.getIconWidth()*size/100, 1);
picture.imageUpdate(image, ImageObserver.HEIGHT, 0, 0, 1, icon.getIconHeight()*size/100);
}
} |
Partager