2011-08-13 6 views
3

나는 다른 어떤 것을보고 있었고 이미지 크기를 조정하면서 이미지의 가장자리를 따라 영역을 유지하면서 이미지의 둥근 모서리가 크기 조정 된 이미지의 길이에 상관없이 항상 똑같이 보입니다. 좀 더 인터넷 검색을 한 후 내가 기억하고있는 것을 발견 할 수 없으므로 이것이 가능한지 그리고 누구에게나 정보가 있는지 알고 싶습니다.이미지의 자바 크기 조정 센터

특히 내가 가지고있는 것은 사용자 정의 테두리이며 레이블이 달린 매달린 탭처럼 보이기위한 것입니다. 이미지는 상단이 사각형이며 하단 가장자리가 둥글다. 나는 라벨의 길이에 관계없이 하단의 둥근 모서리를 유지하고 싶지만, 큰 라벨을 수용하기 위해 센터를 90 % 정도 늘리십시오. 나는이 코드에서 어딘가에 어떤 인세 트를 입력 할 수 있다고 가정한다. 크기 조정 방법.

titleLabel = new JLabel(title){ 
      public void paint(Graphics g){ 
       g.drawImage(tabImg, 0, 0, this.getSize().width, this.getSize().height, null); 
       g.drawString(this.getText(), 1, this.getFont().getSize()); 
      } 
     }; 
+0

그것은 손으로 할 어려운 것이되지 않을 것 : 단지 중간 이미지의 픽셀의 열을 얻을하고 필요한 폭에 도달하기 위해 필요한만큼 여러 번을 가지고 복사합니다. – toto2

+0

[다음 예] (http://www.google.com/search?q=java+ magnifying+glass) 중에서 찾을 수 있습니다. – trashgod

답변

2

OK, 나는이 솔루션을 마련하고 유사한 일을하고 싶은 다른 사람의 코드를 게시하고이 작업을 수행하는 방법으로 인정의 부족을 부여. 이 코드는 이미지를 9 개의 섹션으로 나눕니다. 네 모서리는 홀로 남을 것입니다. 4 가장자리의 중간은 가장자리를 따라 늘어나거나 압축됩니다. 중앙 섹션은 양쪽 방향으로 늘어나거나 압축됩니다. 물론 나를위한이 수업의 요점은 둥근 모서리를 가진 더 큰 이미지를 압축하지만 이미지가 단순히 축소되었을 때 거의 사라진 둥근 모서리를 유지하는 것이 었습니다. 분명히 이것은 그림과 같은 이미지로는 거의 효과가 없을 것입니다. 그러나 사용자 정의 페인팅과 둥근 모서리가있는 구성 요소의 경우이 기능이 잘 작동하는 것 같습니다.

enter image description here

는 단지 변경된 이미지를 호출 할 수있는이 클래스의 생성자가 없습니다. 사용 될

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; 
} 

}

관련 문제