2

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.

Jaymit Desai
  • 153
  • 1
  • 2
  • 11
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). That code would need a `main(String[])` to put it on-screen to be 'complete'. 2) One way to get images for the example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Jan 30 '15 at 17:14

1 Answers1

2

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • yes i know about that,Sorry for that ,but even using paint() method doesnt do anything. – Jaymit Desai Jan 30 '15 at 16:29
  • @JaymitDesai, How is your code different from the code found in the working example from the tutorial? Post your updated code including the main() method for executing your class so people can test. – camickr Jan 30 '15 at 17:17
  • Added the Class having main method. – Jaymit Desai Jan 30 '15 at 17:23
  • @JaymitDesai, you posted code is still using paintComponent(). What have you done to debug your code? Did you look at the working example? Did you modify the working example. Do some basic debugging. Make an effort we are not here to do the work for you!!! – camickr Jan 30 '15 at 20:02
  • Using super.paint() solved the problem,sorry for the post i made,ill make sure i create one according to rules next time. – Jaymit Desai Jan 31 '15 at 09:38