简体   繁体   English

Java 调整图像中心

[英]Java resizing center of image

I was looking something else up the other day and feel like I remember running across a way to resize an image but keeping an area along the sides of the image unaltered so that the rounded edges of the images will always look the same no matter the length of the scaled image.前几天我在看别的东西,感觉我记得跑过一种方法来调整图像的大小,但保持图像两侧的区域不变,这样无论长度如何,图像的圆形边缘看起来总是一样的的缩放图像。 After some more googling I cannot find what I remember seeing so I would like to know if this is possible and if anyone has and info.经过一番谷歌搜索后,我找不到我记得看到的东西,所以我想知道这是否可能以及是否有人有信息。

Specifically what I have is a custom border with what is meant to look like a hanging tab with a label on it.具体来说,我所拥有的是一个自定义边框,它看起来像一个带有 label 的悬挂标签。 The image is square on top and rounded on the bottom edges.图像顶部为方形,底部边缘为圆形。 I would like to keep the bottom rounded corners no matter the length of the labels on it but stretch the center ~90% to accommodate larger labels.无论标签的长度如何,我都想保持底部圆角,但将中心拉伸约 90% 以容纳更大的标签。 I assume it would be in this code somewhere where i can enter some insets?我假设它会在这段代码中我可以输入一些插图的地方? in the resizing method.在调整大小的方法中。

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

OK, given the lack of accepted method to do this I have come up with this solution and am posting the code for anyone else who would like to do something similar.好的,鉴于缺乏可接受的方法来做到这一点,我想出了这个解决方案,并将代码发布给任何想要做类似事情的人。 This code will take an image and divide it into 9 sections.此代码将拍摄一张图像并将其分为 9 个部分。 The 4 corners will be left alone. 4个角将被单独留下。 the middles of the 4 edges will be stretched or compressed along the edge. 4 条边的中间将沿边拉伸或压缩。 The center section will be stretched or compressed in both directions.中心部分将在两个方向上拉伸或压缩。 Of course the point of this class for me was to compress a larger image with rounded corners but keep the rounded corners which nearly disappeared when the image was simply scaled down.当然,对我来说,这个 class 的目的是压缩带有圆角的较大图像,但保留圆角,当图像被简单地缩小时,圆角几乎消失了。 Obviously this will do little good with an image like a picture but for components with custom painting and rounded edges this seems to be working good.显然,这对于像图片这样的图像没有什么好处,但对于具有自定义绘画和圆角边缘的组件,这似乎工作得很好。

在此处输入图像描述

There is no constructor for this class you can just simply call for an altered image.此 class 没有构造函数,您只需调用更改后的图像即可。 the use would be用途是

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

This will return an image stretched to the desired dimension with the corners remaining the same and the sides only being modified in 1 dimension using the insets parameter to determine the amount around the edge that is modified in a single dimension.这将返回一个拉伸到所需尺寸的图像,其中角保持不变,并且仅使用 insets 参数在一维中修改边以确定在单个维度中修改的边缘周围的量。 There is probably a tricky way to iterate through the lists of images but this way I could better see what was going on.遍历图像列表可能有一种棘手的方法,但这样我可以更好地了解发生了什么。

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM