-1

I am working on a project for detecting faces from an input image. I am using opencv with Java. The problem which I'm facing is as below

  1. The faces that are detected are to be placed on a JLabels setIcon method.
  2. First time it places the faces, but for the next image, the previous faces are not cleared.

Following code that I tried to add and remove faces

1) Adding faces:

jFaceLabel is JLabel array initialized to size 100 jpDetectedImage is a JPanel which contains the JLabels (faces)

jFaceLabel = new JLabel[100];

for(int index=0;index<ImageHandler.noOfDetections;index++){
    jFaceLabel[index] = new JLabel();
    jFaceLabel[index].setIcon(new ImageIcon("C://Users//Public//Pictures//Sample Pictures//TestPics//temp//"+index+".jpg"));
    //jFaceLabel[index].setIcon(face);
    int x = this.jpDetectedImage.getX() + (index%2) * 64 + 10 * ((index%2)+1);
    int y = this.jpDetectedImage.getY() + (index/2) * 64 + 10 * ((index/2)+1);
    jFaceLabel[index].setBounds(x, y, 64, 64);
    this.jpDetectedImage.add(jFaceLabel[index]);

    if(index>8 && (index%2==0)){
        this.jpDetectedImage.setPreferredSize(new Dimension(
        this.jpDetectedImage.getPreferredSize().width,
        this.jpDetectedImage.getPreferredSize().height + 74
        ));
    }
    System.out.println("Placed : "+tempPath+"//"+index+".jpg");
}
jpDetectedImage.repaint();

2) Removing faces:

for(int j=0;j<ImageHandler.noOfDetections;j++){
    jFaceLabel[j].getParent().remove(jFaceLabel[j]);
}
this.jpDetectedImage.repaint();

The problem is, the first time every faces gets displayed on the JLabels but successive detection of faces, result in overlapping of the old faces. The detected faces are stored at a physical path and are deleted when a image for detection is loaded.

I require, is the removal of the jFaceLabel array from jpDetectedImage panel and a new memory allocation for every successive detection phase.

How to remove the JLabels from JPanel dynamically and add them again with a new ImageIcon?

  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) (continued...) – Andrew Thompson Jun 01 '14 at 10:46
  • (continued...) 3) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). 4) *"Please help me with this!"* Please don't forget to ask a question. What *is* your question? – Andrew Thompson Jun 01 '14 at 10:47
  • *"How to remove the JLabels from JPanel dynamically and add them again with a new ImageIcon?"* Don't do that. Either call `setIcon` or use a `CardLayout`. – Andrew Thompson Jun 01 '14 at 11:14
  • @AndrewThompson I tried doing the same but the previous face does not get removed (though its deleted physically) – Anand Karandikar Jun 01 '14 at 11:25

1 Answers1

3

The better way to do this is to update the label's icon in place using setIcon(), as shown here.

image1

A less flexible alternative is to remove the component and validate the container, as shown here.

image2

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045