OK, 나는이 솔루션을 마련하고 유사한 일을하고 싶은 다른 사람의 코드를 게시하고이 작업을 수행하는 방법으로 인정의 부족을 부여. 이 코드는 이미지를 9 개의 섹션으로 나눕니다. 네 모서리는 홀로 남을 것입니다. 4 가장자리의 중간은 가장자리를 따라 늘어나거나 압축됩니다. 중앙 섹션은 양쪽 방향으로 늘어나거나 압축됩니다. 물론 나를위한이 수업의 요점은 둥근 모서리를 가진 더 큰 이미지를 압축하지만 이미지가 단순히 축소되었을 때 거의 사라진 둥근 모서리를 유지하는 것이 었습니다. 분명히 이것은 그림과 같은 이미지로는 거의 효과가 없을 것입니다. 그러나 사용자 정의 페인팅과 둥근 모서리가있는 구성 요소의 경우이 기능이 잘 작동하는 것 같습니다.
는 단지 변경된 이미지를 호출 할 수있는이 클래스의 생성자가 없습니다. 사용 될
StretchedImage.stretch(image, new Insets(t,l,b,r), new Dimension(w,h), BufferedImage.TYPE_INT_ARGB);
이 이미지를 반환 모서리가 동일한 나머지과 측면 만 수정 가장자리 주위 량을 측정 세트 파라미터를 사용하여 1 개 차원으로 변형되는 것을 이용하여 원하는 치수로 연신 단 하나 차원에서. 아마도 이미지 목록을 반복하는 까다로운 방법이있을 수 있습니다. 그러나 이런 식으로 진행되는 것을 더 잘 볼 수 있습니다.
public class StretchedImage{
public static Image stretch(Image image, Insets ins, Dimension dim, int hints){
//debugcode
//System.out.println(dim);
//load image into bufferedImage
BufferedImage bi = toBufferedImage(image, hints);
//create 2d bufferedImage array to hold the 9 images
Image[][] img = new Image[3][3];
//split Image into 9 subsections
img[0][0] = bi.getSubimage(0, 0, ins.left, ins.top);
img[0][1] = bi.getSubimage(ins.left, 0, bi.getWidth() - ins.left - ins.right, ins.top);
img[0][2] = bi.getSubimage(bi.getWidth() - ins.right, 0, ins.right, ins.top);
img[1][0] = bi.getSubimage(0, ins.top, ins.left, bi.getHeight() - ins.top - ins.bottom);
img[1][1] = bi.getSubimage(ins.left, ins.top, bi.getWidth() - ins.left - ins.right, bi.getHeight() - ins.top - ins.bottom);
img[1][2] = bi.getSubimage(bi.getWidth() - ins.right, ins.top, ins.right, bi.getHeight() - ins.top - ins.bottom);
img[2][0] = bi.getSubimage(0, bi.getHeight() - ins.bottom, ins.left, ins.bottom);
img[2][1] = bi.getSubimage(ins.left, bi.getHeight() - ins.bottom, bi.getWidth() - ins.left - ins.right, ins.bottom);
img[2][2] = bi.getSubimage(bi.getWidth() - ins.right, bi.getHeight() - ins.bottom, ins.right, ins.bottom);
//determine the width and height of the sections that will be stretched
//only the center section will have both dimensions changed
int w = dim.width - ins.left - ins.right;
int h = dim.height - ins.top - ins.bottom;
//Stretch the proper sections
img[0][1] = img[0][1].getScaledInstance(w, img[0][1].getHeight(null), hints);
img[1][0] = img[1][0].getScaledInstance(img[1][0].getWidth(null), h, hints);
img[1][1] = img[1][1].getScaledInstance(w, h, hints);
img[1][2] = img[1][2].getScaledInstance(img[1][2].getWidth(null), h, hints);
img[2][1] = img[2][1].getScaledInstance(w, img[2][1].getHeight(null), hints);
//for loop is debug code
//for(int i = 0; i < 3; i++){
// for(int j = 0; j < 3; j++){
// System.out.println(i + " " + j + " " + img[i][j].getWidth() + "," + img[i][j].getHeight());
// }
//}
//create a new bufferedImage to hold the final image
BufferedImage finalImage = new BufferedImage(dim.width, dim.height, hints);
Graphics g = finalImage.getGraphics();
//draw the peices to the final image
g.drawImage(img[0][0], 0, 0, null);
g.drawImage(img[0][1], ins.left, 0, null);
g.drawImage(img[0][2], dim.width - ins.right, 0, null);
g.drawImage(img[1][0], 0, ins.top, null);
g.drawImage(img[1][1], ins.left, ins.top, null);
g.drawImage(img[1][2], dim.width - ins.right, ins.top, null);
g.drawImage(img[2][0], 0, dim.height - ins.bottom, null);
g.drawImage(img[2][1], ins.left, dim.height - ins.bottom, null);
g.drawImage(img[2][2], dim.width - ins.right, dim.height - ins.bottom, null);
return (Image)finalImage;
}
// This method returns a buffered image with the contents of an image
public static BufferedImage toBufferedImage(Image image, int hints) {
BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), hints);
bi.getGraphics().drawImage(image, 0, 0, null);
return bi;
}
}
그것은 손으로 할 어려운 것이되지 않을 것 : 단지 중간 이미지의 픽셀의 열을 얻을하고 필요한 폭에 도달하기 위해 필요한만큼 여러 번을 가지고 복사합니다. – toto2
[다음 예] (http://www.google.com/search?q=java+ magnifying+glass) 중에서 찾을 수 있습니다. – trashgod