Question

I have tried the code below for drawing a rectangle on an image(jLabel) but its not drawing. Please help to solve this.

protected void paintComponent(Graphics g)
{

   this.paintComponent(g);
   g2 = (Graphics2D) g;

   g.drawRect(n,n1,40,40);

}



private void jLabel1MouseClicked(java.awt.event.MouseEvent evt)   
{                                     
    // TODO add your handling code here:
    k=0;
    f=jLabel1.getMousePosition();
    n=f.x;
    n1=f.y;
    n=n+p/2;
    n1=n1+(q/2-25);
    repaint(n,n1,40,40);     
}                                    
Was it helpful?

Solution

You have infinite recursion there, and will get a stack overflow. This:

this.paintComponent(g)

should be instead:

super.paintComponent(g);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top