⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainentry.java

📁 java neural networkin
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

/**
 * MainEntry
 * Copyright 2005 by Jeff Heaton(jeff@jeffheaton.com)
 *
 * Example program from Chapter 7
 * Programming Neural Networks in Java
 * http://www.heatonresearch.com/articles/series/1/
 *
 * This software is copyrighted. You may use it in programs
 * of your own, without restriction, but you may not
 * publish the source code without the author's permission.
 * For more information on distributing this code, please
 * visit:
 *    http://www.heatonresearch.com/hr_legal.php
 *
 * @author Jeff Heaton
 * @version 1.1
 */


public class MainEntry extends JFrame implements Runnable,NeuralReportable {

  /**
   * The downsample width for the application.
   */
  static final int DOWNSAMPLE_WIDTH = 5;

  /**
   * The down sample height for the application.
   */
  static final int DOWNSAMPLE_HEIGHT = 7;

  /**
   * The entry component for the user to draw into.
   */
  Entry entry;

  /**
   * The down sample component to display the drawing
   * downsampled.
   */
  Sample sample;

  /**
   * The letters that have been defined.
   */
  DefaultListModel letterListModel = new DefaultListModel();

  /**
   * The neural network.
   */
  KohonenNetwork net;

  /**
   * The background thread used for training.
   */
  Thread trainThread = null;

  /**
   * The constructor.
   */
  MainEntry()
  {
    getContentPane().setLayout(null);
    entry = new Entry();
    entry.reshape(168,25,200,128);
    getContentPane().add(entry);

    sample = new Sample(DOWNSAMPLE_WIDTH,DOWNSAMPLE_HEIGHT);
    sample.reshape(307,210,65,70);
    entry.setSample(sample);
    getContentPane().add(sample);

    //{{INIT_CONTROLS
    setTitle("Java Neural Network");
    getContentPane().setLayout(null);
    setSize(405,382);
    setVisible(false);
    JLabel1.setText("Letters Known");
    getContentPane().add(JLabel1);
    JLabel1.setBounds(12,12,84,12);
    JLabel2.setText("Tries:");
    getContentPane().add(JLabel2);
    JLabel2.setBounds(12,264,72,24);
    downSample.setText("Down Sample");
    downSample.setActionCommand("Down Sample");
    getContentPane().add(downSample);
    downSample.setBounds(252,180,120,24);
    add.setText("Add");
    add.setActionCommand("Add");
    getContentPane().add(add);
    add.setBounds(168,156,84,24);
    clear.setText("Clear");
    clear.setActionCommand("Clear");
    getContentPane().add(clear);
    clear.setBounds(168,180,84,24);
    recognize.setText("Recognize");
    recognize.setActionCommand("Recognize");
    getContentPane().add(recognize);
    recognize.setBounds(252,156,120,24);
    JScrollPane1.setVerticalScrollBarPolicy(
      javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    JScrollPane1.setOpaque(true);
    getContentPane().add(JScrollPane1);
    JScrollPane1.setBounds(12,24,144,132);
    JScrollPane1.getViewport().add(letters);
    letters.setBounds(0,0,126,129);
    del.setText("Delete");
    del.setActionCommand("Delete");
    getContentPane().add(del);
    del.setBounds(12,156,144,24);
    load.setText("Load");
    load.setActionCommand("Load");
    getContentPane().add(load);
    load.setBounds(12,180,72,24);
    save.setText("Save");
    save.setActionCommand("Save");
    getContentPane().add(save);
    save.setBounds(84,180,72,24);
    train.setText("Begin Training");
    train.setActionCommand("Begin Training");
    getContentPane().add(train);
    train.setBounds(12,204,144,24);
    JLabel3.setText("Last Error:");
    getContentPane().add(JLabel3);
    JLabel3.setBounds(12,288,72,24);
    JLabel4.setText("Best Error:");
    getContentPane().add(JLabel4);
    JLabel4.setBounds(12,312,72,24);
    tries.setText("0");
    getContentPane().add(tries);
    tries.setBounds(96,264,72,24);
    lastError.setText("0");
    getContentPane().add(lastError);
    lastError.setBounds(96,288,72,24);
    bestError.setText("0");
    getContentPane().add(bestError);
    bestError.setBounds(96,312,72,24);
    JLabel8.setHorizontalTextPosition(
      javax.swing.SwingConstants.CENTER);
    JLabel8.setHorizontalAlignment(
      javax.swing.SwingConstants.CENTER);
    JLabel8.setText("Training Results");
    getContentPane().add(JLabel8);
    JLabel8.setFont(new Font("Dialog", Font.BOLD, 14));
    JLabel8.setBounds(12,240,120,24);
    JLabel5.setText("Draw Letters Here");
    getContentPane().add(JLabel5);
    JLabel5.setBounds(204,12,144,12);
    //}}

    //{{REGISTER_LISTENERS
    SymAction lSymAction = new SymAction();
    downSample.addActionListener(lSymAction);
    clear.addActionListener(lSymAction);
    add.addActionListener(lSymAction);
    del.addActionListener(lSymAction);
    SymListSelection lSymListSelection = new SymListSelection();
    letters.addListSelectionListener(lSymListSelection);
    load.addActionListener(lSymAction);
    save.addActionListener(lSymAction);
    train.addActionListener(lSymAction);
    recognize.addActionListener(lSymAction);
    //}}
    letters.setModel(letterListModel);
    //{{INIT_MENUS
    //}}
  }


  /**
   * The main method.
   *
   * @param args Args not really used.
   */
  public static void main(String args[])
  {
    (new MainEntry()).show();
  }
  //{{DECLARE_CONTROLS
  javax.swing.JLabel JLabel1 = new javax.swing.JLabel();
  javax.swing.JLabel JLabel2 = new javax.swing.JLabel();

  /**
   * THe downsample button.
   */
  javax.swing.JButton downSample = new javax.swing.JButton();

  /**
   * The add button.
   */
  javax.swing.JButton add = new javax.swing.JButton();

  /**
   * The clear button
   */
  javax.swing.JButton clear = new javax.swing.JButton();

  /**
   * The recognize button
   */
  javax.swing.JButton recognize = new javax.swing.JButton();
  javax.swing.JScrollPane JScrollPane1 = new javax.swing.JScrollPane();

  /**
   * The letters list box
   */
  javax.swing.JList letters = new javax.swing.JList();

  /**
   * The delete button
   */
  javax.swing.JButton del = new javax.swing.JButton();

  /**
   * The load button
   */
  javax.swing.JButton load = new javax.swing.JButton();

  /**
   * The save button
   */
  javax.swing.JButton save = new javax.swing.JButton();

  /**
   * The train button
   */
  javax.swing.JButton train = new javax.swing.JButton();
  javax.swing.JLabel JLabel3 = new javax.swing.JLabel();
  javax.swing.JLabel JLabel4 = new javax.swing.JLabel();

  /**
   * How many tries
   */
  javax.swing.JLabel tries = new javax.swing.JLabel();

  /**
   * The last error
   */
  javax.swing.JLabel lastError = new javax.swing.JLabel();

  /**
   * The best error
   */
  javax.swing.JLabel bestError = new javax.swing.JLabel();
  javax.swing.JLabel JLabel8 = new javax.swing.JLabel();
  javax.swing.JLabel JLabel5 = new javax.swing.JLabel();
  //}}
  //{{DECLARE_MENUS
  //}}

  class SymAction implements java.awt.event.ActionListener {
    public void actionPerformed(java.awt.event.ActionEvent event)
    {
      Object object = event.getSource();
      if ( object == downSample )
        downSample_actionPerformed(event);
      else if ( object == clear )
        clear_actionPerformed(event);
      else if ( object == add )
        add_actionPerformed(event);
      else if ( object == del )
        del_actionPerformed(event);
      else if ( object == load )
        load_actionPerformed(event);
      else if ( object == save )
        save_actionPerformed(event);
      else if ( object == train )
        train_actionPerformed(event);
      else if ( object == recognize )
        recognize_actionPerformed(event);
    }
  }

  /**
   * Called to downsample the image.
   *
   * @param event The event
   */
  void downSample_actionPerformed(java.awt.event.ActionEvent event)
  {
    entry.downSample();

  }

  /**
   * Called to clear the image.
   *
   * @param event The event
   */
  void clear_actionPerformed(java.awt.event.ActionEvent event)
  {
    entry.clear();
    sample.getData().clear();
    sample.repaint();

  }

  /**
   * Called to add the current image to the training set
   *
   * @param event The event
   */
  void add_actionPerformed(java.awt.event.ActionEvent event)
  {
    int i;

    String letter = JOptionPane.showInputDialog(
      "Please enter a letter you would like to assign this sample to.");
    if ( letter==null )
      return;

    if ( letter.length()>1 ) {
      JOptionPane.showMessageDialog(this,
                                    "Please enter only a single letter.","Error",
                                    JOptionPane.ERROR_MESSAGE);
      return;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -