0

I am attempting to resize an ImageIcon and use it in my GridBagLayout JPanel. It does resize... but it still also displays at full size. So you have a resized image inside of the larger image. Anyone see anything glaringly wrong with the code below? Thank you!

public CharacterPage(WikiDB db) throws IOException {
    super("Character Page");

    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    setSize(new Dimension(1000,1000));

    rootPanel.setLayout(new GridBagLayout());
    final GridBagConstraints c = new GridBagConstraints();
    rootPanel.setSize(new Dimension(500,500));

   searchButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            BufferedImage img1 = null;

                for (int x = 0; x < characterImages.size(); x++){
                    String imageURL = "http://cosplayidol.otakuhouse.com/wp- 
                           content/uploads/2012/06/s-1-1.jpg"

                    try
                    {
                        URL url1 = new URL(imageURL);

                        URLConnection conn1 = url1.openConnection();
                        conn1.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
                        InputStream in1 = conn1.getInputStream();

                        img1 = ImageIO.read(in1);
                    } catch (IOException ioe)
                    {
                        ioe.printStackTrace();
                    }


                }

                Graphics2D g2 = img1.createGraphics();
                g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g2.drawImage(img1, 0,0,100, 100, null);
                g2.dispose();

                ImageIcon newIcon = new ImageIcon(img1);

                c.fill = GridBagConstraints.HORIZONTAL;

                JLabel cIcon = new JLabel(newIcon);
                c.gridx = 0;
                c.gridy = 6;
                rootPanel.add(cIcon,c);
               }
          });

       setContentPane(rootPanel);
    pack();
    setVisible(true);
      }
Michelle
  • 365
  • 1
  • 4
  • 18

2 Answers2

3
Graphics2D g2 = img1.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img1, 0, 0, 100, 100, null);
g2.dispose();

Should be something like:

int w = 100;
int h = 100;
// create an image of the appropriate size!
BufferedImage img2 = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = img2.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img1, 0, 0, w, h, null);
g2.dispose();
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • That does get rid of the double Image issue, but no matter what values I set w and h to, the picture never re-sizes. – Michelle May 08 '15 at 01:50
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 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). – Andrew Thompson May 08 '15 at 04:56
0

These lines draw the resized image back over the original:

Graphics2D g2 = img1.createGraphics();
g2.drawImage(img1, 0,0,100, 100, null);

Drawing onto a graphics created from the rootPanel itself.

Barett
  • 5,826
  • 6
  • 51
  • 55