在图像上方放置网格

时间:2014-03-29 07:44:00

标签: java image swing grid jlabel

到目前为止我在java中实现的是要求用户从目录上传图像。我的下一步是,当图像被加载时,网格被放置在该图像上方仅用于视觉目的,以便图像被划分为例如10×10网格。我该如何实现这些东西?这是我到目前为止所实施的内容。

        JFileChooser choose=new JFileChooser();
        choose.showOpenDialog(null);
        File f=choose.getSelectedFile();
        String filename=f.getAbsolutePath();
        path.setText(filename);        
        BufferedImage img;
        try {
            img=ImageIO.read(f);
            Image dimg = img.getScaledInstance(500,500,Image.SCALE_SMOOTH);
            ImageIcon imageIcon = new ImageIcon(dimg);
            image_label.setIcon(imageIcon);
            }
        catch(Exception e) { 
            System.out.println(e);
        }

1 个答案:

答案 0 :(得分:2)

  • 在面板中绘制图像

    protected void paintComponent(Grapchics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);
    }
    
  • 然后根据您想要的单元格数量(例如10x10),在图像上绘制100个单元格(drawRect())。像

    这样的东西
    protected void paintComponent(Grapchics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);
        int cellHeight = (int)(getHeight() / 10);
        int cellWidth = (int)(getWidth() / 10);
        for (int y = 0; y < getWidth(); y += cellHeight) {
            for (int x = 0; x < getHeight(); x += cellWidth){
                g.drawRect(x, y, cellWidth, cellHeight);
            }
        }
    }
    

我没有测试过,但基本概念就在那里。您可能还想为10使用变量(可能是常量)。

更新1

enter image description here

你可以看到精确度有点偏差,因为我使用了int,但你可以使用Grapchics2D Rectangle2D.Double来使用双打和绘图。我懒得改变它

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ImageGrid extends JPanel {

    private static final int CELLS = 10;

    BufferedImage img;

    public ImageGrid() {
        try {
            img = ImageIO.read(getClass().getResource("/resources/stackoverflow5.png"));
        } catch (IOException ex) {
            Logger.getLogger(ImageGrid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (img != null) {
            g.drawImage(img, 0, 0, this);
            int cellHeight = (int) (getHeight() / CELLS); 
            int cellWidth = (int) (getWidth() / CELLS);
            for (int y = 0; y < getHeight(); y += cellHeight) {
                for (int x = 0; x < getWidth(); x += cellWidth) {
                    g.drawRect(x, y, cellWidth, cellHeight);

                }
            }
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return img == null ? new Dimension(300, 300) 
                : new Dimension(img.getWidth(), img.getHeight());
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                JPanel wrapperPanel = new JPanel(new GridBagLayout());
                wrapperPanel.add(new ImageGrid());
                frame.add(wrapperPanel);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

更新2

JLabel

import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ImageGrid extends JLabel {

    private static final int CELLS = 10;

    BufferedImage img;

    public ImageGrid() {
        try {
            img = ImageIO.read(getClass().getResource("/resources/stackoverflow5.png"));
            setIcon(new ImageIcon(img));
        } catch (IOException ex) {
            Logger.getLogger(ImageGrid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (img != null) {
            int cellHeight = (int) (getHeight() / CELLS); 
            int cellWidth = (int) (getWidth() / CELLS);
            for (int y = 0; y < getHeight(); y += cellHeight) {
                for (int x = 0; x < getWidth(); x += cellWidth) {
                    g.drawRect(x, y, cellWidth, cellHeight);

                }
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                JPanel wrapperPanel = new JPanel(new GridBagLayout());
                wrapperPanel.add(new ImageGrid());
                frame.add(wrapperPanel);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}