Search code examples
javaimageswingjava-2d

Program supposed to display whole image only displaying part of it


public class Main extends JPanel{
        private static final long serialVersionUID = 1L;

        static BufferedImage image;
        private static JPanel imagePanel;
        private static JFrame frame;

        private static String dir = "image\\pic.jpg";

        public Main(){
                try{
                        image = ImageIO.read(new File(dir));
                }catch (IOException e){
                        // If it cannot read from the directory inform the client
                        System.out.println("Error reading from directory: " + e.getMessage());
                }
        }
        //We set our preffered size if we succeed in loading the image
        public Dimension getPrefferedSize(){
                if(image == null){
                        return new Dimension(100, 100);
                }else{
                        return new Dimension(image.getWidth(null), image.getHeight(null));
                }
        }
        //Draw our image on the screen with Graphic's "drawImage()" method
        public void paint(Graphics g){
                g.drawImage(image, 0, 0, null);
        }

        public static void main(String[] args){
                frame = new JFrame("Loading image from file example");
                imagePanel = new JPanel();
                //Release the resource window handle as we close the frame
                frame.addWindowListener(new WindowAdapter(){
                                public void windowClosing(WindowEvent e){
                                        System.exit(0);
                                }
                        });
                try{
                        image = ImageIO.read(new File(dir));
                        if(image.getWidth(null) != 0){
                                System.out.println(image.getWidth(null));
                                System.out.println(image.getHeight(null));
                        }
                }catch(Exception e){
                        e.printStackTrace();
                }
                imagePanel.add(new Main());
                frame.add(imagePanel);
                //frame.pack();
                frame.setSize(200, 300);
                frame.setVisible(true);
        }
        /*
        public static BufferedImage enlarge(BufferedImage image, int n){
                int w = n * image.getWidth();
                int h = n * image.getHeight();

                BufferedImage enlargedImage = new BufferedImage(w, h, image.getType());
                for (int y=0;y<h;y++){
                        for(int x=0;x<w;x++){
                                enlargedImage.setRGB(x, y, image.getRGB(x/n,  y/n));
                        }
                }

                return enlargedImage;
        }*/
}

I'm only getting the top-left 8x8 pixels of the image I provided ("pic.jpg"). Could anyone explain this?


Solution

  • Have a look over this code that corrects a variety of mistakes. Most of the changes have comments. Otherwise, check the docs. for the alternate methods.

    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Main extends JPanel{
    
        static BufferedImage image;
        private static JPanel imagePanel;
        private static JFrame frame;
    
        private static String dir = "image\\pic.jpg";
    
        public Main(){
            image = new BufferedImage(400,140,BufferedImage.TYPE_INT_RGB);
        }
    
        //We set our preffered size if we succeed in loading the image
        @Override  // TO DETECT WRONG SPELLING!
        public Dimension getPreferredSize(){
        /*
        public Dimension getPrefferedSize(){
        */
            if(image == null){
                return new Dimension(100, 100);
            }else{
                // A JPanel is an ImageObserver
                return new Dimension(image.getWidth(this), image.getHeight(this));
            }
        }
    
        //Draw our image on the screen with Graphic's "drawImage()" method
        // For Swing components.
        @Override
        public void paintComponent(Graphics g){
        // public void paint(Graphics g){
            super.paintComponent(g);
            // A JPanel is an ImageObserver
            g.drawImage(image, 0, 0, this);
        }
    
        public static void main(String[] args){
            frame = new JFrame("Loading image from file example");
            imagePanel = new JPanel();
            //Release the resource window handle as we close the frame
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            /*frame.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(0);
                }
            });*/
            imagePanel.add(new Main());
            frame.add(imagePanel);
            frame.pack();
            frame.setVisible(true);
        }
    }