Search code examples
javaswingjlayer

Placing Grid over Image using JLayer


I want to place a Grid over a Image. I used a JLabel that holds the Image using paintComponent method,I used this method because the image will be changing during different phases in my project and for the JLayer the class GridDrawer extends LayerUI which helps to draw the grid (for smaller example I have used only drawRect() method).

My Code:

GridPhoto(Main) Class:

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gridphoto;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;

/**
 *
 * @author VJ
 */
public class GridPhoto {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    new GUI();
                } catch (IOException ex) {
                    Logger.getLogger(GridPhoto.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        });
    }

}

GUI Class:

   package gridphoto;

    import java.awt.image.BufferedImage;
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JLayer;
    import javax.swing.plaf.LayerUI;

    public class GUI {
    JFrame frame;
    JPanel panel;
    JLayer<JLabel> GridLayer;
    JLabel imagelabel;
    LayerUI<JLabel> GridUI;
    BufferedImage img;

    public GUI() throws IOException {
        frame = new JFrame("GridImage Test");
        panel = new JPanel();
        img = ImageIO.read(new File("/Users/VJ/Desktop/gs.png"));
        imagelabel = new JLabel() {
            public void paintComponent(Graphics g) {
               g.drawImage(img.getScaledInstance(500, 500, BOTTOM), 0, 0, null);
            }
        };
        GridUI = new GridDrawer();
        GridLayer = new JLayer(imagelabel, GridUI);
        panel.setLayout(new BorderLayout());
        frame.setLayout(new BorderLayout());
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 700);
     panel.add(GridLayer);
        frame.add(panel);
        frame.setVisible(true);

    }

    public class GridDrawer extends LayerUI<JLabel> {

        public void paintComponent(Graphics g) {
            g.drawRect(0, 0, 250, 250);
        }

    }

}

My problem is that even after adding JLayer to JPanel it only displays the image and not the grid. For example purpose the paintComponent Method of GridDrawer class only draws a Rectangle.

Please tell me what is wrong in my code or is there any other way other than using JLayer to place grid over Image?

Output.


Solution

  • LayerUI does not have a paintComponent(...) method.

    Whenever you override a method make sure you use @Override so you know you are overriding the correct method:

    @Override
    public void paint(...)
    

    and don't forget to invoke super.paint(...).

    Read the Swing tutorial on How to Decorate Components With JLayer Class for more information and working examples.