Laying grids above images

2019-09-20 05:57发布

What I have implemented till now in java is ask the user to upload an image from the directory. My next step is that when the image is loaded a grid is placed above that image just for visual purpose so that the image gets divided in a, say 10 x 10 grids. How do I implement this stuff? Here's what I have implemented till now.

        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条回答
别忘想泡老子
2楼-- · 2019-09-20 06:35
  • paint the image in a panel

    protected void paintComponent(Grapchics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);
    }
    
  • Then based on the the number of cells you want, say 10x10, just draw 100 cells (drawRect()) over the image. Something like

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

I haven't test it, but the basic concept is there. You may also want to use variables (a constant probably) for the 10.

UPDATE 1

enter image description here

You can see the precision's a little off because I used int, but you can use doubles and draw by using Grapchics2D Rectangle2D.Double. I'm too lazy to change it

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

UPDATE 2

With 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);
            }
        });
    }
}
查看更多
登录 后发表回答