简体   繁体   English

如何在 JLabel 中绘制现有图像?

[英]How do I draw over an existing image within a JLabel?

I am trying to draw a grid over an existing image which is already displayed in a JLabel.我正在尝试在已显示在 JLabel 中的现有图像上绘制网格。

However, my code does not seem to do anything.但是,我的代码似乎没有做任何事情。

This is the method I wrote:这是我写的方法:

public void drawGrid(final JLabel label, final int rowAmount, final int columnAmount) {
    ImageIcon icon = (ImageIcon)label.getIcon();
    BufferedImage tempImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = tempImage.createGraphics();
    g2d.setColor(Color.GREEN);
    BasicStroke bs = new BasicStroke(2);
    g2d.setStroke(bs);
    int width = tempImage.getWidth();
    int height = tempImage.getHeight();
    System.out.println("Width: " + width + ", Height: " + height);
    int x1 = 0;
    int y1;
    int x2 = width;
    int y2;
    for (int i = 1; i < rowAmount; i++) {
        y1 = (height * i) / rowAmount;
        y2 = y1;
        g2d.drawLine(x1, y1, x2, y2);
        System.out.println("Drawing line from (" + x1 + ", " + y1 + ") to (" + x2 + ", " + y2 + ")");
    }
    y1 = 0;
    y2 = height;
    for (int i = 1; i < columnAmount; i++) {
        x1 = (width * i) / columnAmount;
        x2 = x1;
        g2d.drawLine(x1, y1, x2, y2);
        System.out.println("Drawing line from (" + x1 + ", " + y1 + ") to (" + x2 + ", " + y2 + ")");
    }
    icon.paintIcon(label, g2d, 0,0);
    g2d.dispose();
    label.setIcon(new ImageIcon(tempImage));
}

When calling drawGrid(label, 10, 10);当调用drawGrid(label, 10, 10); the prints show the correct coordinates, but nothing happens.打印显示正确的坐标,但没有任何反应。

I already checked whether a manual label.repaint() at the end would do the trick, but Label.setIcon(ImageIcon) already repaints on call and this did not fix it.我已经检查了最后的手动label.repaint()是否可以解决问题,但是Label.setIcon(ImageIcon)已经在调用时重新绘制,这并没有解决它。

Can anyone explain to me what went wrong and how to solve this issue?谁能向我解释出了什么问题以及如何解决这个问题?

Also, I think that the conversions and casts between ImageIcon , Icon , BufferedImage and Graphics2D may be inefficient or faulty, but I am unexperienced with those.另外,我认为ImageIconIconBufferedImageGraphics2D之间的转换和转换可能效率低下或有缺陷,但我对这些没有经验。

I realize that the question now kind of has been answered already, but here is a fixed and improved version of your method:我意识到现在的问题已经得到了解答,但这里是您的方法的固定和改进版本:

public void drawGrid(JLabel lbl, int rows, int columns) {
        Image img = ((ImageIcon) lbl.getIcon()).getImage();
        Graphics2D g2d = (Graphics2D) img.getGraphics();
        g2d.setStroke(new BasicStroke(2));
        g2d.setColor(Color.GREEN);

        int width = img.getWidth(null) / columns;
        int height = img.getHeight(null) / rows;

        for (int i = 1; i < columns; i++) {
            g2d.drawLine(width * i, 0, width * i, img.getHeight(null));
        }
        for (int i = 1; i < rows; i++) {
            g2d.drawLine(0, height * i, img.getWidth(null), height * i);
        }
    }

Oh and if your image has a different size than your label, the lines will be centered on the image and therefore appear offset on the label.哦,如果您的图像与 label 的尺寸不同,线条将位于图像的中心,因此在 label 上会出现偏移。 In that case just change lines 7 and 8 to the following:在这种情况下,只需将第 7 行和第 8 行更改为以下内容:

int width = lbl.getWidth() / columns;
int height = lbl.getHeight() / rows;

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

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