程序应该显示整个图像只显示其中的一部分

时间:2013-09-08 19:23:04

标签: java image swing java-2d

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;
        }*/
}

我只获得了我提供的图像的左上角8x8像素(“pic.jpg”)。 任何人都可以解释一下吗?

2 个答案:

答案 0 :(得分:3)

使用布局管理器,该布局管理器使用JPanel的完整区域而不是默认FlowLayout,它仅使用首选大小的组件。你可以使用

imagePanel.setLayout(new GridLayout());

在Swing中进行自定义绘制时,还要覆盖paintComponent而不是paint,并记得调用super.paintComponent(g)

答案 1 :(得分:3)

查看此代码可以纠正各种错误。大多数变化都有评论。否则,请检查文档。替代方法。

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);
    }
}