Reputation: 141
I have an image displayed in jLabel
. When clicking on any part of the image a 40x40 rectangle will be drawn. Now I want to remove the drawn rectangle from the image when a REMOVE(jButton) pressed. I have tried the code below
public void paint (Graphics g) {
g2 = (Graphics2D) g;
g2.clearRect(n,n1, 40,40 );
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
repaint(n,n1,40,40);
}
It just fill the rectangle with background color instead of removing.Is there any way to remove the rectangle without filling any color?that is by keeping the original image itself?
Upvotes: 0
Views: 230
Reputation: 324108
Custom painting is done in the paintCompnent(..) method of the label, NOT the paint() method. Also you should invoke super.paintComponent(g) as the first statement.
In your case it sounds like you need a Boolean variable to control when the rectangle is drawn. Maybe something like:
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (paintRectangle)
// paint the rectangle
}
Now in the ActionListener you just set the paintRectangle variable to false and invoke repaint() on the component.
Upvotes: 1