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

📄 networkview.java

📁 著名的神经网络工具箱
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*************************************************************************

This program is copyrighted. Please refer to COPYRIGHT.txt for the
copyright notice.

This file is part of JavaNNS.

JavaNNS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

JavaNNS is distributed in the hope that it will be useful,
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with JavaNNS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*************************************************************************/


package javanns;

import javax.swing.* ;
import javax.swing.event.* ;
import java.awt.* ;
import java.awt.print.* ;
import java.awt.event.* ;
import java.awt.image.BufferedImage ;
import java.util.Vector ;
import wsi.ra.print.PagePrinter ;

/*==========================================================================*
 * CLASS DECLARATION
 *==========================================================================*/

/**
 * NetworkView is window for displaying networks in 2D.
 *
 */
class NetworkView extends JPanel implements Scrollable, NetworkListener, Printable {
  private boolean under_construction = false;
  /*-------------------------------------------------------------------------*
   * public member variables
   *-------------------------------------------------------------------------*/
  public final static int LEFT=-1, TOP=-1, CENTER=0, RIGHT=1, BOTTOM=1;
  public final static Color DEFAULT_POS = Color.green;
  public final static Color DEFAULT_NEG = Color.red;
  public final static Color DEFAULT_NULL = new Color(0, 0, 0x99);

  Snns snns;
  NetworkViewSettings settings;
  int net_view_no; // the number of the network view

  // descriptors for all units
  int font_size, font_height, font_ascent;
  FontMetrics font_metrics;

  // grid position of the mouse
  private int grid_x, grid_y;

  // for drag and drop
  boolean drawingRect;
  Point startPoint, endPoint, delta = new Point();
  Rectangle rectangle;



  MouseListener mouseListener =
    new MouseAdapter(){
      public void mouseClicked(MouseEvent e){
        Point p = e.getPoint();
        Unit u = network.getUnitAtXY( new int[]{ grid_x, grid_y } );

        if( u != null && isPointOnUnit(p,u) ){ // auf eine Unit geklickt
          int mod = e.getModifiers();
          if( (mod & e.BUTTON1_MASK) == 0 ){ // nicht mit linker Maustaste
            network.selectUnit(u);
            Point p1 = scrollPane.getViewport().getViewPosition();
            snns.popup.show( frame, p.x - p1.x + 100, p.y - p1.y );
          }
          else{ // mit linker Maustaste
            if( network.unitsSelected() ){
              if( (mod & e.CTRL_MASK) == 0 ){
                network.deselectUnits();
                network.selectUnit(u);
              }
              else{
                if( network.isUnitSelected(u) ){
                  network.deselectUnit(u);
                }
                else network.selectUnit(u);
              }
            }
            else network.selectUnit(u);
            requestFocus();
          }
        }

        else{ // neben die Units geklickt
          network.deselectUnits();
          if( isDirectEdit() ) evaluateDirectEdit(true);
        }
      }

      public void mousePressed(MouseEvent e){
        Point p = e.getPoint();
        Unit u = network.getUnitAtXY(new int[]{grid_x,grid_y});
        startPoint = p;
        if( u == null || !network.isUnitSelected(u) ){ // es werden neue Units gewaehlt
          endPoint = p;
          drawingRect = true;
          buildRectangle();
        }
        else if( isPointOnUnit(p) )
          endPoint = (Point)p.clone(); // es werden Units verschoben
      }

      public void mouseReleased(MouseEvent e){
        if( drawingRect ){ // es wurden neue Units gewaehlt
          Vector us = getUnitsInRectangle();
          if( us.size() > 0 ){
            if( network.unitsSelected() && (e.getModifiers() & e.CTRL_MASK) == 0 )
              network.deselectUnits();
            network.selectUnits(us);
            requestFocus();
          }
          drawingRect = false;
          repaint(rectangle);
        }
        else {
          updatePositions(); // es wurden Units verschoben
          endPoint = null;
        }
      }
    };

  MouseMotionListener mouseMotionListener =
    new MouseMotionAdapter(){
        public void mouseDragged(MouseEvent e){
          Point p = e.getPoint();
          if( !drawingRect ) {
            if( endPoint != null ) checkStepSize(p); // ggf. repaint durchfuehren
          }
          else{ // Selektionsrechteck anpassen
            drawingRect = false;
            repaint( rectangle );
            drawingRect = true;
            endPoint = p;
            buildRectangle();
          }
        }
        public void mouseMoved(MouseEvent e){// ToolTips anpassen
          mouseMovedTo(e.getPoint());
        }
    };

  JInternalFrame frame;

  // listeners :
  Vector listeners = new Vector();

  InternalFrameListener frameListener =
    new InternalFrameAdapter(){
      public void internalFrameClosed(InternalFrameEvent e) { close(); }
      public void internalFrameActivated(InternalFrameEvent evt){
        requestFocus();
        fireEvent( NetworkViewEvent.VIEW_ACTIVATED );
      }
      public void internalFrameDeactivated( InternalFrameEvent evt ){
        fireEvent( NetworkViewEvent.VIEW_DEACTIVATED );
      }
    };

  KeyListener keyListener =
    new KeyAdapter(){
      public void keyReleased(KeyEvent e){
        int code = e.getKeyCode();
        if( code == e.VK_DELETE ) network.deleteUnits() ;
        else if( code == e.VK_A && (e.getModifiers() & e.CTRL_MASK) != 0 )
            network.selectAll();
      }
    };

  // for direct editing :
  private JTextField[] labels;
  private boolean edit_top_labels = false,
                  edit_bottom_labels = false;
  private int labels_to_edit;

  // the network the view should show
  Network network;

  JScrollPane scrollPane;
  Point view_position = null; // used to move the viewport to currently created units
  /*-------------------------------------------------------------------------*
   * constructor
   *-------------------------------------------------------------------------*/

  /**
   * Class constructor. Applies given settings for network display.
   *
   * @param the snns kernel
   * @param display type ( size, colors, ... )
   */
  public NetworkView( Snns snns, UIDisplayType dt) {
    this.snns = snns;
    setLayout( null );
    network = snns.network;
    network.addListener( this );

    addMouseListener( mouseListener );
    addMouseMotionListener( mouseMotionListener );
    setRequestFocusEnabled(true);
    addKeyListener( keyListener );

    if( dt != null ) setPreferredSize( dt.dimension );

    if(dt != null && dt.settings != null) settings = dt.settings;
    else settings = NetworkViewSettings.getDefaultSettings();

    font_metrics = getFontMetrics(settings.font);
    font_height = font_metrics.getHeight();
    font_ascent = font_metrics.getAscent();

    scrollPane = new JScrollPane( this,
                                  JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                  JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
    JViewport vp = scrollPane.getViewport();
    vp.setBackground( settings.background_color );

    JPanel pMain = new JPanel();
    JPanel pPalette = new PalettePanel( this );
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    pMain.setLayout( gbl );
    gbl.setConstraints( pPalette, gbc );
    pMain.add( pPalette );
    gbc.weightx = gbc.weighty = .5;
    gbc.gridx = 1;
    gbc.fill = gbc.BOTH;
    gbl.setConstraints( scrollPane, gbc );
    pMain.add( scrollPane );

    net_view_no = dt.displayNo;
    frame = new JInternalFrame( network.getName() + " <" + dt.displayNo + ">",
      true, //resizable
      true, //closable
      true, //maximizable
      true);//iconifiable
    frame.addInternalFrameListener( frameListener );
    frame.setContentPane( pMain );
    frame.setSize( dt.dimension );
    frame.setLocation( dt.position );
//    frame.setBackground( settings.background_color );
    update();
  }


  /**
   * Class constructor. Initializes default settings for network display.
   *
   * @param snns The SNNS kernel
   */
  public NetworkView(Snns snns) {
    this(snns, null);
  }

  /*-------------------------------------------------------------------------*
   * public methods
   *-------------------------------------------------------------------------*/

  public void settingsChanged(){
    //System.out.println("NetworkView.settingsChanged()");
    if( isDirectEdit() ) fillLabels();
    scrollPane.getViewport().setBackground(settings.background_color);
    fireEvent( NetworkViewEvent.SETTINGS_CHANGED );
  }

/*---------------------- drawing methods -------------------------------------*/
  /**
   * Displays the current network, according to the settings.
   *
   * @param g graphic context of the window
   */
  public void paint( Graphics g ){
    if( under_construction ) System.out.println("NetworkView.paint(): "+net_view_no);
    super.paint( g );
    Color old_color = g.getColor();
    Font oldFont = g.getFont();
    g.setFont(settings.font);
    Dimension min_size = getMinimumSize();// er sollte auch schrumpfen koennen, falls Units geloescht oder verschoben werden

    // first draw all links...
    if(settings.show_links)
      for( Unit unit = network.getFirstUnit(); unit != null; unit = network.getNextUnit() ){
        Vector links = unit.getAllIncomingLinks();
        for( int i=0; i<links.size(); i++ ) drawLink( g, (Link)links.elementAt( i ) );
      }

    // ...and then the units
    Unit winner = null;
    if( settings.top_label_type == NetworkViewSettings.WINNER ||
        settings.base_label_type == NetworkViewSettings.WINNER )
      winner = network.getWinnerUnit();

    for(Unit unit = network.getFirstUnit(); unit != null; unit = network.getNextUnit() ) {
      Point p = drawNeuron(g, unit, winner, null );
      if( p.x + settings.grid_size> min_size.width )
        min_size.width = p.x + settings.grid_size;
      if( p.y + settings.grid_size> min_size.height )
        min_size.height = p.y + settings.grid_size;
    }

    // when drawing the rectangle
    if( drawingRect ){
      g.setColor( settings.selection_color );
      g.drawRect( rectangle.x, rectangle.y, rectangle.width - 1, rectangle.height - 1);
    }

    // while dragging
    if( network.unitsSelected() && (delta.x != 0 || delta.y != 0) ){
      g.setColor( settings.null_color );
      int[] pos;
      for( Unit unit = network.getFirstUnit(); unit != null; unit = network.getNextUnit() )
        if( network.isUnitSelected( unit ) ){
          pos = unit.getPosition();
          g.drawRect( X(pos[0]) - (settings.width >> 1) + delta.x,
                      Y(pos[1]) - (settings.width >> 1) + delta.y,
                      settings.width, settings.height );
        }
    }

    g.setColor(old_color);
    g.setFont(oldFont);

    Dimension old = getPreferredSize();

    if( !old.equals( min_size ) ) {
      setPreferredSize( min_size );
      setSize( min_size );
      getParent().validate();
    }

    if( view_position != null ){
      scrollPane.getViewport().setViewPosition( view_position );
      view_position = null;
    }
  }

  /**
   * Updates the display settings and repaints the window.
   */
  public void update() {
    setForeground(settings.text_color);

⌨️ 快捷键说明

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