private void initComponents() {
    handler = new CharacterEventHandler();
    producer = new RandomCharacterGenerator();
    producer.setDone(true);
    producer.start();
    displayCanvas = new AnimatedCharacterDisplayCanvas(producer);
    feedbackCanvas = new CharacterDisplayCanvas(this);
    quitButton = new JButton();
    startButton = new JButton();
    stopButton = new JButton();
    score = new ScoreLabel(producer, this);

    Container pane = getContentPane();
    JPanel p1 = new JPanel();
    p1.setLayout(new BoxLayout(p1, BoxLayout.PAGE_AXIS));
    p1.add(displayCanvas);
    p1.add(feedbackCanvas);

    JPanel p2 = new JPanel();
    score.setText("      ");
    score.setFont(new Font("MONOSPACED", Font.BOLD, 30));
    p2.add(score);
    startButton.setText("Start");
    p2.add(startButton);
    stopButton.setText("Stop");
    stopButton.setEnabled(false);
    p2.add(stopButton);
    quitButton.setText("Quit");
    p2.add(quitButton);
    p1.add(p2);
    pane.add(p1, BorderLayout.NORTH);
    pack();

    addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent evt) {
            quit();
          }
        });
    feedbackCanvas.addKeyListener(
        new KeyAdapter() {
          public void keyPressed(KeyEvent ke) {
            char c = ke.getKeyChar();
            if (c != KeyEvent.CHAR_UNDEFINED) newCharacter((int) c);
          }
        });
    startButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            displayCanvas.setDone(false);
            producer.setDone(false);
            score.resetScore();
            startButton.setEnabled(false);
            stopButton.setEnabled(true);
            feedbackCanvas.setEnabled(true);
            feedbackCanvas.requestFocus();
          }
        });
    stopButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            startButton.setEnabled(true);
            stopButton.setEnabled(false);
            producer.setDone(true);
            displayCanvas.setDone(true);
            feedbackCanvas.setEnabled(false);
          }
        });
    quitButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            quit();
          }
        });
  }
示例#2
0
  /**
   * This method is called from within the constructor to initialize the form. WARNING: Do NOT
   * modify this code. The content of this method is always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    PlyarPic = new javax.swing.JLabel();
    PlayerLabel = new javax.swing.JLabel();
    ScoreLabel = new javax.swing.JLabel();
    EstimateLabel = new javax.swing.JLabel();
    hitsLabel = new javax.swing.JLabel();
    arrowLabel = new javax.swing.JLabel();

    setBackground(new java.awt.Color(34, 177, 76));
    setToolTipText("");

    PlyarPic.setMaximumSize(new java.awt.Dimension(35, 35));
    PlyarPic.setPreferredSize(new java.awt.Dimension(35, 35));

    PlayerLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

    ScoreLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    ScoreLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    ScoreLabel.setText("0");

    EstimateLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    EstimateLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    EstimateLabel.setText("0");

    hitsLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    hitsLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    hitsLabel.setText("0");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(
                layout
                    .createSequentialGroup()
                    .addComponent(
                        PlyarPic,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(
                        PlayerLabel,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        67,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(
                        ScoreLabel,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        35,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(10, 10, 10)
                    .addComponent(
                        EstimateLabel,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        35,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(
                        hitsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(
                        arrowLabel,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        34,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(4, 4, 4)));
    layout.setVerticalGroup(
        layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(
                PlyarPic,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE)
            .addComponent(
                PlayerLabel,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE)
            .addGroup(
                layout
                    .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(
                        hitsLabel,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                    .addComponent(
                        ScoreLabel,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                    .addComponent(
                        EstimateLabel,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        35,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(
                        arrowLabel,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        35,
                        javax.swing.GroupLayout.PREFERRED_SIZE)));
  } // </editor-fold>//GEN-END:initComponents