Zmiana wymiarów zdjecia

0

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;


public class Panel extends JPanel {
 private BufferedImage netImage;
 private BufferedImage image;
 private int panelHeigth;
 private int panelWidth;
 
 Panel(){
	 super();
	 File imageFile=new File("mar.jpg");
	 
	 BufferedImage image = Scalr.resize(imageFile, Scalr.Method.BALANCED, 353, 354);
	 

	 try {
		URL imageURL=new URL("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRPB_jV7oz6nE2I-hFpPquMwcJIqDM9rPtQc3Seqmefo4ldDTRS_A");
		netImage=ImageIO.read(imageURL);
		image=ImageIO.read(imageFile); 
	
	 } catch (IOException e) {
		System.err.println("Blad odczytu");
		e.printStackTrace();
	}
	 panelHeigth=netImage.getHeight()+image.getHeight();
	 panelWidth=netImage.getWidth()+image.getWidth();
	 Dimension dimension=new Dimension(panelWidth,panelHeigth);
	 setPreferredSize(dimension);
 }

 
 

@Override
 public void paintComponent(Graphics g){
	 Graphics2D g2d=(Graphics2D)g;
	 g2d.drawImage(netImage,0,0,null);
	 g2d.drawImage(image,netImage.getWidth(),0,null);
 }
 }

Witam, mam oto taki kod, czy ktoś mógłby mi powiedzieć jak mogę zmienić wymiary zdjęcia ? Gdy startuje program zdjęcie pobrane z internetu jest okej, ale te pobrane z dysku jest tak duże , że nie mieści się w ekranie. Jak mógłbym to uczynić ? Chciałbym zmienić wymiary zdjęcia z dysku na takie same wymiary jak ma zdjęcie z neta.
Pozdrawiam.

1

Użyj metody getScaledInstance z klasy BufferedImage.
Przykład (width i height to maksymalny rozmiar):

        BufferdImage bi = ImageIO.read(file);
        ImageIcon imgIcon = new ImageIcon(bi);
        if(imgIcon.getIconHeight() > width || imgIcon.getIconWidth() > height)
        {
            int a = imgIcon.getIconHeight()/width;
            int b = imgIcon.getIconWidth()/height;
            if(a>b)
            {
                imgIcon = new ImageIcon(bi.getScaledInstance(imgIcon.getIconWidth()/a, imgIcon.getIconHeight()/a, java.awt.Image.SCALE_FAST));
            }
            else
            {
                imgIcon = new ImageIcon(bi.getScaledInstance(imgIcon.getIconWidth()/b, imgIcon.getIconHeight()/b, java.awt.Image.SCALE_FAST));
            }
        }
1

Nie zadawaj pytań w komentarzach.
Nie musisz tworzyć JLabela:

g2.drawImage(imgIcon.getImage(),...);

1 użytkowników online, w tym zalogowanych: 0, gości: 1