Witam,
Robię program do edycji obrazków. Natrafiłem na problem przy skalowaniu. Rozmiar obrazka jest poprawny przy zmianach ale sam obrazek jest źle wyświetlany na panelu i nie wiem jak to naprawić... Wszelkie propozycje mile widziane :)
moj panel imgPanel:
public class ImgPanel extends JPanel {
private BufferedImage image;
public ImgPanel (BufferedImage image) {
this.image = image;
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
try {
mt.waitForID(0);
}
catch (Exception e) {
e.printStackTrace();
}
}
public void scale(int percent) {
int w = image.getWidth();
int h = image.getHeight();
int newW = w+(w*percent/100);
int newH = h+(h*percent/100);
AffineTransform scale = AffineTransform.getScaleInstance(w /2, h /2);
BufferedImageOp op = new AffineTransformOp(scale, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage filteredImage = new BufferedImage(newW, newH, image.getType());
op.filter(image, filteredImage);
image = filteredImage;
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
int x = (getWidth() - image.getWidth(this))/2;
int y = (getHeight() - image.getHeight(this))/2;
g2d.drawImage(image, x, y, null);
revalidate();
}
}