import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
public class MultiScale implements ChangeListener {
BufferedImage[] images;
ImageScalePanel imagePanel;
JLabel[] labels;
double scale = 1.0;
public MultiScale(BufferedImage[] images) {
this.images = images;
}
public void stateChanged(ChangeEvent e) {
int value = ((JSlider)e.getSource()).getValue();
scale = (value+4)/20.0;
imagePanel.setScale(scale);
setImages();
}
private JPanel getContent() {
imagePanel = new ImageScalePanel(images);
imagePanel.setBorder(BorderFactory.createTitledBorder("graphic"));
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 1.0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(imagePanel, gbc);
panel.add(getLabelPanel(), gbc);
return panel;
}
private JPanel getLabelPanel() {
JPanel panel = new JPanel(new GridLayout(1,0));
panel.setBorder(BorderFactory.createTitledBorder("labels"));
labels = new JLabel[3];
for(int i = 0; i < labels.length; i++) {
labels[i] = new JLabel((ImageIcon)null, JLabel.CENTER);
panel.add(labels[i]);
}
setImages();
return panel;
}
private void setImages() {
for(int i = 0; i < labels.length; i++) {
labels[i].setIcon(new ImageIcon(getScaledImage(i)));
}
labels[0].getParent().validate();
}
private BufferedImage getScaledImage(int index) {
int w = (int)(scale*images[index].getWidth());
int h = (int)(scale*images[index].getHeight());
int type = images[index].getType();
BufferedImage image = new BufferedImage(w, h, type);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2.drawImage(images[index], 0, 0, w, h, labels[index]);
g2.dispose();
return image;
}
private JSlider getSlider() {
int min = 1, max = 36, inc = 5;
JSlider slider = new JSlider(min, max, 16);
slider.setMajorTickSpacing(5);
slider.setMinorTickSpacing(1);
slider.setPaintTicks(true);
slider.setSnapToTicks(true);
slider.setLabelTable(getLabelTable(min, max, inc));
slider.setPaintLabels(true);
slider.addChangeListener(this);
return slider;
}
private Hashtable getLabelTable(int min, int max, int inc) {
Hashtable<Integer,JLabel> table = new Hashtable<Integer,JLabel>();
for(int j = min; j <= max; j += inc) {
String s = String.format("%.2f", (j+4)/20.0);
table.put(Integer.valueOf(j), new JLabel(s));
}
return table;
}
public static void main(String[] args) throws IOException {
String prefix = "images/geek/geek";
String ext = ".gif";
String[] ids = { "-c---", "--g--", "---h-" };
BufferedImage[] images = new BufferedImage[ids.length];
for(int i = 0; i < images.length; i++) {
String path = prefix + ids[i] + ext;
images[i] = ImageIO.read(new File(path));
}
MultiScale test = new MultiScale(images);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(test.getContent()));
f.getContentPane().add(test.getSlider(), "Last");
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
class ImageScalePanel extends JPanel {
BufferedImage[] images;
double scale = 1.0;
int totalWidth;
public ImageScalePanel(BufferedImage[] images) {
this.images = images;
for(int i = 0; i < images.length; i++)
totalWidth += images[i].getWidth();
}
public void setScale(double scale) {
this.scale = scale;
revalidate();
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
double x = getInsets().left;
double y = getInsets().top;
for(int i = 0; i < images.length; i++) {
AffineTransform at = AffineTransform.getTranslateInstance(x,y);
at.scale(scale, scale);
g2.drawRenderedImage(images[i], at);
x += scale*images[i].getWidth();
}
}
public Dimension getPreferredSize() {
Insets insets = getInsets();
int vi = insets.top + insets.bottom;
int hi = insets.left + insets.right;
int w = (int)(scale*totalWidth) + hi;
int h = (int)(scale*images[0].getHeight()) + vi;
return new Dimension(w, h);
}
}