When you create a JPanel class, the constructor runs once and creates the panel, drawing it only once.


public class CountTapFrame2 {

public static void main(String[] args){
JFrame frame = new JFrame("Count taps with GUI");
frame.setSize(300, 300);
frame.setLocation(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CountTapPanel2 myCounter = new CountTapPanel2();

frame.setContentPane(myCounter);
frame.setVisible(true);
myCounter.process();

}
}

 

import java.awt.*;
import javax.swing.*;
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
/**
* Write a description of class CountTapPanel2 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class CountTapPanel2 extends JPanel {

private JLabel tapCount; //output
Finch tap;

public CountTapPanel2()
{
//setLayout(new FlowLayout());
tapCount = new JLabel(" 0 ");
tapCount.setFont(new Font("Serif", Font.BOLD, 128));
tapCount.setForeground(Color.blue);
add(tapCount);
}
public void process(){
tapCount.setText(" " + 0 + " ");
tap = new Finch();
tap.setLED(0,255,0);
int taps=0;
int check=0;
while(check<50){
if (tap.isTapped()){
taps=taps+1;
//System.out.print("*");
tapCount.setText(" " + taps + " ");
}
tap.sleep(200);
check++;

}
//System.out.println("\nI was tapped " + taps + " times");

tap.setLED(255,0,0);
tap.sleep(10000);
tap.quit();
System.exit(0);
}

}