0

I want to resize my picture, but there is always an error with displaying it.

How can I do it?

public class Panel extends JPanel {

    private ImageIcon imgIcon; 

    public Panel(){
        super();
        File imageFile=new File("mar.jpg"); 
        try {
            BufferedImage bi = ImageIO.read(imageFile);
            ImageIcon imgIcon = new ImageIcon(bi);
            imgIcon = new ImageIcon(bi.getScaledInstance(400,400, Image.SCALE_DEFAULT));        
        } catch (IOException e) {
            System.err.println("error");
            e.printStackTrace();
        }

        Dimension dimension=new Dimension(1400,1000);
        setPreferredSize(dimension);
    }
    @Override
    public void paintComponent(Graphics g){
        Graphics2D g2d=(Graphics2D)g;
        g2d.drawImage(imgIcon.getImage(),0,0,this);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Maryn
  • 1
  • 2
  • 5
    What's the error? – CraigR8806 Jul 31 '17 at 11:42
  • 1
    Why would you set the preferred size of the panel to (1400, 1000) when the size of the scaled image is only (400, 400)? Also, don't call you class "Panel", that is an AWT component so the name is confusing. You custom classes should have a more descriptive name. – camickr Jul 31 '17 at 21:22
  • @CraigR8806 This is the error. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Panel.paintComponent(Panel.java:49) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JLayeredPane.paint(Unknown Source) – Maryn Jul 31 '17 at 22:00
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Aug 01 '17 at 12:44

1 Answers1

0

This is how I have done it in the past:

public class ImagePanel extends JPanel{

    private BufferedImage image;

    public PanelImage(){}


    public void setImage(BufferedImage image){
        this.image = image;
    }

    private Image scaleImage(){
        if(image==null)return null;
        int width = getWidth();
        int height = (int)((getWidth()/(double)image.getWidth())*image.getHeight());
        return image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
    }

    @Override protected void paintComponent(Graphics g){
        super.paintComponent(g);
        Image scaledImage = scaleImage();
        if(scaledImage != null){
            int x = 0;
            int y = (int)((getHeight()/2d)-(scaledImage.getHeight(null)/2d));
            g.drawImage(scaledImage, x, y, null);
        }
    }
}

Also, please don't name your class Panel as there already is a awt.Panel and will cause ambiguity issues.

CraigR8806
  • 1,584
  • 13
  • 21