Results 1 to 5 of 5
  1. #1
    Panchitopro is offline Member
    Join Date
    May 2008
    Posts
    11
    Rep Power
    0

    Default Scale 2 or more pictures using a JSlider

    Hi everybody,

    I posted this message in general Java, I'am not sure if it is right or not.

    I would like to know how I can display 2 or more images and then make scale in these images on the same time.

    Thank you.

    Francisco

    I found this code here in Java Forums this is my reference:

    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 MapScale extends JPanel {
    BufferedImage image;
    double scale = 1.0;

    public MapScale(BufferedImage image) {
    this.image = image;
    }

    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATI ON,
    RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    double x = (getWidth() - scale*image.getWidth())/2;
    double y = (getHeight() - scale*image.getHeight())/2;
    AffineTransform at = AffineTransform.getTranslateInstance(x,y);
    at.scale(scale, scale);
    g2.drawRenderedImage(image, at);
    }

    public Dimension getPreferredSize() {
    int w = (int)(scale*image.getWidth());
    int h = (int)(scale*image.getHeight());
    return new Dimension(w, h);
    }

    private JSlider getSlider() {
    int min = 1, max = 36, inc = 5;
    final 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(new ChangeListener() {
    public void stateChanged(ChangeEvent e) {
    int value = slider.getValue();
    scale = (value+4)/20.0;
    revalidate();
    repaint();
    }
    });
    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 path = "images/GIRL.JPG";
    BufferedImage image = ImageIO.read(new File(path));
    MapScale test = new MapScale(image);
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(test));
    f.getContentPane().add(test.getSlider(), "Last");
    f.setSize(400,400);
    f.setLocation(200,200);
    f.setVisible(true);
    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default scaling multiple images

    There are two general ways to do this:
    1 — drawing the images in a JPanel
    2 — mounting scaled images in JLabels.
    Here's an example showing both ways:
    Java Code:
    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);
        }
    }
    Images from Using Swing Components Examples, down low.

  3. #3
    Panchitopro is offline Member
    Join Date
    May 2008
    Posts
    11
    Rep Power
    0

    Default

    Morning,

    I checked the code and I took the images from the project CheckBoxDemo (Using Swing Components: Examples (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)) and it works.

    Just I would like to comment that I found 3 things when I put the code in Eclipse:

    1. in this part I had an error:
    ...
    Hashtable<Integer,JLabel> table = new Hashtable<Integer,JLabel>();
    ...
    It display a Syntax error, parameterized types are only available if source level is 5.0.

    I just changed by:
    Hashtable table = new Hashtable();

    2. In this line:
    ...
    String s = String.format("%.2f", (j+4)/20.0);
    ...
    and the message in Eclipse said:
    The method format(String, Object[]) in the type String is not applicable for the arguments (String, double)

    Here I wrote String s = "x"; instead.

    I don't understand why method "format" does not work.


    3. It has appeared a warning message in Eclipse when I tryed to do a debug, something about deprecated functions, and Eclipse gave me 3 options (Continue, Cancel and Restart), I chose Restart and now it works correctly. I tryed to put the original code in a new project to see the text message but I couldn't do it, this is rare. Anyway.

    Thank you very much hardwired.

    Now I would like to know how can I put an image with a dimension 2400 x 1800 like a thumbnail into this project. I will check that, however if you can help me with a reference please, I will apprecite it.

    Thank you again.

    Panchitopro

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    parameterized types are only available if source level is 5.0
    Generics was introduced in j2se 1.5. You can check the version of java at the prompt with >java -version
    The method format(String, Object[]) in the type String is not applicable for the arguments (String, double)
    The String.format method was also new in j2se 1.5
    Looks like your ide might have a format method used for writing out arrays.
    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class Thumbnail {
        private JLabel getContent(BufferedImage image) {
            BufferedImage thumb = scaleImage(image);
            ImageIcon icon = new ImageIcon(thumb);
            JLabel label = new JLabel(icon, JLabel.CENTER);
            return label;
        }
    
        private BufferedImage scaleImage(BufferedImage src) {
            int w = 75;   // thumbnail
            int h = 100;  // dimensions
            int type = BufferedImage.TYPE_INT_RGB;
            BufferedImage dst = new BufferedImage(w, h, type);
            Graphics2D g2 = dst.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g2.setBackground(UIManager.getColor("Panel.background"));
            g2.clearRect(0,0,w,h);
            double xScale = (double)w/src.getWidth();
            double yScale = (double)h/src.getHeight();
            double scale = Math.min(xScale, yScale);    // fit
                           //Math.max(xScale, yScale);  // fill
            double x = (w - scale*src.getWidth())/2;
            double y = (h - scale*src.getHeight())/2;
            AffineTransform at = AffineTransform.getTranslateInstance(x,y);
            at.scale(scale, scale);
            g2.drawRenderedImage(src, at);
            g2.dispose();
            return dst;
        }
    
        public static void main(String[] args) throws IOException {
            String path = "http://imgsrc.hubblesite.org/hu/db/" +
                          "1999/12/images/a/formats/full_jpg.jpg";
            URL url = new URL(path);
            URLConnection uc = url.openConnection();
            uc.connect();
            InputStream is = uc.getInputStream();
            BufferedImage image = ImageIO.read(is);
            System.out.printf("imageWidth = %d  imageHeight = %d%n",
                               image.getWidth(), image.getHeight());
            is.close();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new Thumbnail().getContent(image));
            f.setSize(300,175);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  5. #5
    Panchitopro is offline Member
    Join Date
    May 2008
    Posts
    11
    Rep Power
    0

    Default

    Hi,

    I came back, I have had some problems with the version, it seems that I can not use the swing controls into my project because of the versions with MS VM and Sun.

    Thank you for your answer and I will write later.

    Panchitopro

Similar Threads

  1. Replies: 22
    Last Post: 07-25-2012, 09:51 PM
  2. How can we zoom a map using JSlider
    By barney in forum AWT / Swing
    Replies: 5
    Last Post: 02-23-2009, 01:48 PM
  3. Scale 2 or more pictures using a JSlider
    By Panchitopro in forum New To Java
    Replies: 0
    Last Post: 05-05-2008, 05:22 PM
  4. Small scale Java Editor
    By Greenfrog99 in forum AWT / Swing
    Replies: 0
    Last Post: 01-27-2008, 08:46 PM
  5. How To:Use a JSlider to adjust Text size in a JPanel
    By louiebagz in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2007, 07:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •