0

I am use this code for the slideshow, and it's working 100%, but the problem is the image resolution if the resolution was high no problem but if it was low the image is shown in very bad shape.

and the code is :

public class EasyView_V1 {
     public static Timer tm = null;

     public static File[] f = new File("C:/Ma7moud/my projects/Rami/NetBeans/Web applications/TheChatServerApp_3/web/images/brugges2006/big").listFiles();

     public static void main(String[] args) {
         JPanel panel5 = new JPanel();
         JLabel pic = new JLabel();

         pic.setSize(new Dimension(610, 695));

         //Call The Function SetImageSize
         try {
             SetImageSize(f.length - 1, f, pic);
         } catch (Exception e) {
             System.out.println(e.toString());
             JOptionPane.showMessageDialog(null, "Images directory not found", "Error", JOptionPane.ERROR_MESSAGE);
             JOptionPane.showMessageDialog(null, "Plz check the images directory and try again", "Attention", JOptionPane.INFORMATION_MESSAGE);
             System.exit(1);
         }

         try {
             //set a timer
             tm = new Timer(1000, (ActionEvent e) -> {
             SetImageSize(x, f, pic);
             x += 1;
             try {
                 if (x >= f.length) {
                     x = 0;
                 }
             } catch (Exception ex) {
                 System.out.println(ex.toString());
                 System.exit(1);
             }
         });
       } catch (Exception e) {
            System.out.println(e.toString());
       }

    panel5.add(pic);
    tm.start();
    JFrame f = new JFrame ()
    f.add(panel5);
    f.pack();
    f.setVisible(true);
}

Here is the code for image drawing:

public static void SetImageSize(int i, File[] f, JLabel pic) {
    try {
        ImageIcon icon = new ImageIcon(f[i].toString());
        Image img = icon.getImage();
        @SuppressWarnings("UnusedAssignment")
        Image newImg = null;

        newImg = img.getScaledInstance(pic.getWidth(), pic.getHeight(), Image.SCALE_SMOOTH);

        ImageIcon newImc = new ImageIcon(newImg);
        pic.setIcon(newImc);
    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 3) Image quality will always suffer when the image is scaled up. – Andrew Thompson Aug 31 '15 at 08:21
  • 1
    Instead of loading your image with `new ImageIcon`, use [ImageIO.read](https://docs.oracle.com/javase/8/docs/api/javax/imageio/ImageIO.html#read-java.io.File-). Compare the width and height of the returned BufferedImage with the dimensions to which your code is about to scale the image, and skip the scaling if it would enlarge the image. – VGR Sep 01 '15 at 02:13
  • okay thank u Andrew and VGR for the help, and i will consider your advice, and i will back to you with the solution in soon as possible – Mahmoud Aziz Sep 01 '15 at 07:37

1 Answers1

0

i have found the solution and it is about fixing the aspect ratio of each image and the position of the image into JPanel so the solution is as follow :

i change the set image size like this :

public static void SetImageSize(int i, File[] f, JLabel pic) {
        try {
            pic.setSize(new Dimension(610, 695));

            ImageIcon icon = new ImageIcon(f[i].toString());
            Image img = icon.getImage();

            ImageIcon finalIimage = new ImageIcon(ImgResize(img, img.getWidth(pic), img.getHeight(pic), pic));

            xy = ImgPosition(finalIimage.getIconWidth(), finalIimage.getIconHeight(), pic);

            pic.setSize(new Dimension(pic.getWidth(), img.getHeight(pic)));
            pic.setIcon(finalIimage);
        } catch (Exception ex) {
            System.out.println(ex.toString() + "set image size");
        }
    }

and also i add this 2 methods :

private static Image ImgResize(Image srcImg, double w, double h, JLabel pic) {
        @SuppressWarnings("UnusedAssignment")

        BufferedImage resizedImg = null;

        int W = pic.getWidth(), H = pic.getHeight();
        double AR = w / h, nh = 0, nw = 0;

        try {
            if (w > h) {
                nw = W;
                nh = nw / AR;
            } else {
                nh = H;
                nw = nh * AR;
            }

            resizedImg = new BufferedImage((int) nw, (int) nh, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = resizedImg.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2.drawImage(srcImg, 0, 0, (int) nw, (int) nh, null);
            g2.dispose();

            return resizedImg;
        } catch (Exception e) {
            System.out.println(e.toString() + " resize");
        }
        return resizedImg;
    }

    private static int[] ImgPosition(int w, int h, JLabel pic) {
        int W = pic.getWidth(), H = pic.getHeight();
        @SuppressWarnings({"UnusedAssignment", "LocalVariableHidesMemberVariable"})
        int x = 0, y = 0;

        int[] a = new int[2];

        try {
            if (w > h) {
                x = 5;
                y = ((H / 2) - (h / 2));
            } else {
                y = 0;
                x = ((W / 2) - (w / 2)) - 30;
            }

        } catch (Exception e) {
            System.out.println(e.toString() + " position");
        }
        a[0] = x;
        a[1] = y;

        return a;
    }
}

and i add this to the main method :

pic.addComponentListener(new ComponentListener() {

            @Override
            public void componentResized(ComponentEvent e) {
                //==================== Variables Panel ====================
                int panelWidthAndHight = (Integer) ((Math.round(window.getHeight() / 2)) - 2);

                int textWidth = (Integer) ((Math.round(window.getHeight() / 2)) - 75);
                int textHeight = 23;

                int location1 = (Integer) ((Math.round(window.getHeight() / 2)));
                int location2 = (Integer) (Math.round(window.getHeight()));

                int picWidth = (Integer) (Math.round(window.getWidth() - (window.getHeight()) - 2));
                int picHeight = (Integer) (Math.round(window.getHeight() - 3));
                //====================      End    ====================

                //==================== First Panel ====================
                panel.setSize(new Dimension(panelWidthAndHight, panelWidthAndHight));
                panel.setLocation(1, 1);

                amountText1.setSize(new Dimension(textWidth, textHeight));
                LittreText1.setSize(new Dimension(textWidth, textHeight));
                //====================      End    ====================

                //==================== Second Panel ====================
                panel2.setSize(new Dimension(panelWidthAndHight, panelWidthAndHight));
                panel2.setLocation(location1, 1);

                amountText2.setSize(new Dimension(textWidth, textHeight));
                LittreText2.setSize(new Dimension(textWidth, textHeight));
                //====================      End    ====================

                //==================== Third Panel ====================
                panel3.setSize(new Dimension(panelWidthAndHight, panelWidthAndHight - 40));
                panel3.setLocation(1, location1);

                amountText3.setSize(new Dimension(textWidth, textHeight));
                LittreText3.setSize(new Dimension(textWidth, textHeight));
                //====================      End    ====================

                //==================== Third Panel ====================
                panel4.setSize(new Dimension(panelWidthAndHight, panelWidthAndHight - 40));
                panel4.setLocation(location1, location1);

                amountText4.setSize(new Dimension(textWidth, textHeight));
                LittreText4.setSize(new Dimension(textWidth, textHeight));
                //====================      End    ====================

                panel5.setSize(new Dimension(picWidth, picHeight));

                pic.setLocation(xy[0], xy[1]);

                window.invalidate();
            }

            @Override
            public void componentMoved(ComponentEvent e) {
                //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void componentShown(ComponentEvent e) {
                //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void componentHidden(ComponentEvent e) {
                //To change body of generated methods, choose Tools | Templates.
            }
        });

and that's it ...

and i hope that this answer help others.