Search code examples
javaswinggraphics2d

the location(x,y) of java swing 'JLabel' and Graphics elements are not same


I have to draw a JLabel with an onclick event on an drawn circle. The newly created JLabel should be placed placed very closed a line which is already created. I was trying to draw this JLabel at the middle position of the line. But problem is, even after setting fixed calculated coordinates(x,y), the JLabel is not drawn in the given location.(unlike g.drawLine() or g.drawOval()). My code is given below: need help to fix it.

    public class ButtonExample extends JFrame{
    JFrame frame;
    JLabel label1, label2, label3;
    private Shape myShape;
    private int arrowAdded = 0;
    public ButtonExample() {
        super("Location test of JLabel and Graphics objects");

        label1 = new JLabel("0,0");
        //label2 = new JLabel("40,40");

        label1.setBounds(0, 0, 50, 50);
        label1.setBorder(BorderFactory.createLineBorder(Color.black));
        //label2.setBounds(100, 100, 50, 50);
        //label2.setBorder(BorderFactory.createLineBorder(Color.black));

        add(label1);
        //add(label2);

        repaint();

         addMouseListener(new MouseAdapter() {
             @Override
             public void mouseClicked(MouseEvent me) {
                super.mouseClicked(me);
                if (myShape.contains(me.getPoint())) {
                    arrowAdded = 1;
                    repaint();
                }
             }
         });

        setLayout(null);
        setSize(1000,600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(1000, 600);
    }

    public void paint(Graphics g){  
        super.paint(g); 
        int startX = 100;
        int startY = 100;
        int endX = 180;
        int endY = 120;

        g.setColor(new Color(0, 255, 0));
        myShape = new Ellipse2D.Double(startX, startY, 30, 30);
        Graphics2D g2d = (Graphics2D) g;
        g2d.draw(myShape);
        g.drawLine(startX, startY, endX, endY);

        int lX = (int)Math.abs(endX-startX)/2;
        int lY = (int)Math.abs(endY-startY)/2;

        if(endX>startX) {
            lX = lX+startX;
        }else {
            lX = lX+endX;
        }

        if(endY>startY) {
            lY = lY+startY;
        }else {
            lY = lY+endY;
        }

        if(arrowAdded == 1) {
            label3 = new JLabel();
            label3.setBounds(lX, lY, 20, 15);
            label3.setBorder(BorderFactory.createLineBorder(Color.black));
            add(label3);
            g.drawRect(lX, lY, 20, 15);
        }enter code here
    }



    public static void main(String[] args) {
        new ButtonExample();
    }

}   

Solution

  • Don't override paint() on a JFrame!

    The frame includes the title bar and borders, so you can't just paint at (0, 0). You would need your painting to be offset by the frame decorations.

    Instead, custom painting should be done by overriding the paintComponent(...) method of a JPanel and then you add the panel to the frame. Now the offsets will be relative to the panel, so you can use (0, 0). Of course you would also add the label to the panel at your desired location.

    Read the section from the Swing tutorial on Custom Painting for more information and working examples.