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());
            }
        };
4

1 に答える 1

2

OK、これを行うための受け入れられた方法がないことを考えると、私はこの解決策を考え出し、同様のことをしたい他の人のためにコードを投稿しています. このコードは画像を取得し、それを 9 つのセクションに分割します。四隅はそのままにします。4 つのエッジの中央がエッジに沿って引き伸ばされたり圧縮されたりします。中央部分は両方向に伸びたり縮んだりします。もちろん、私にとってこのクラスのポイントは、角の丸い大きな画像を圧縮することでしたが、画像を単純に縮小するとほとんど消えてしまった角の丸みを維持することでした。明らかに、これは写真のような画像ではほとんど効果がありませんが、カスタム ペイントと丸みを帯びたエッジを持つコンポーネントではうまく機能しているようです。

ここに画像の説明を入力

このクラスには、変更されたイメージを呼び出すだけのコンストラクターはありません。用途は

StretchedImage.stretch(image, new Insets(t,l,b,r), new Dimension(w,h), BufferedImage.TYPE_INT_ARGB); 

これは、コーナーが同じままで、側面が 1 次元でのみ変更された目的の寸法に引き伸ばされたイメージを返します。 insets パラメータを使用して、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;
}

}

于 2011-08-14T21:06:56.480 に答える