📄 networkview.java
字号:
setBackground(settings.background_color);
frame.setBackground(settings.background_color);
repaint();
}
/**
* method returns true if the view is in direct edit mode
*/
public boolean isDirectEdit(){
return ( edit_bottom_labels || edit_top_labels );
}
/**
* method returns the NetworkViewSettings id number of the labels which
* are currently edited
*/
public int getLabelsToEdit(){
if( !isDirectEdit() ) return -1;
return labels_to_edit;
}
/*-------------------- private drawing methods--------------------------------*/
/**
* Draws a neuron in the window's graphic context. Neuron properties are
* specified by member variables, which are set in
* {@link #paint(Graphics)}.
*
* @param g graphic context
* @param unit
* @param the winner unit
* @param the color of the unit
* ( if set to <code>null<\code> the color belonging to the activation value
* will be taken )
* @return the point right below the unit
*/
protected Point drawNeuron(Graphics g, Unit unit, Unit winner, Color color ) {
if( under_construction ) System.out.println("NetworkView.drawNeuron()");
String top_label = "", bottom_label = "";
double act = unit.getActivation();
if( !edit_top_labels ){
switch(settings.top_label_type) {
case NetworkViewSettings.ACT: top_label = Snns.maxFixPoint(act, 3); break;
case NetworkViewSettings.INIT_ACT: top_label = Snns.maxFixPoint(unit.getInitAct(), 3); break;
case NetworkViewSettings.OUTPUT: top_label = Snns.maxFixPoint(unit.getOutput(), 3); break;
case NetworkViewSettings.BIAS: top_label = Snns.maxFixPoint(unit.getBias(), 3); break;
case NetworkViewSettings.NAME: top_label = unit.getName(); break;
case NetworkViewSettings.NUMBER: top_label = String.valueOf(unit.getNumber()); break;
case NetworkViewSettings.Z_VAL: top_label = String.valueOf( unit.getPosition()[2] ); break;
case NetworkViewSettings.WINNER:
if( unit.equals( winner ) ) top_label = "Winner";break;
default: top_label = "";
}
}
if( !edit_bottom_labels ) {
switch(settings.base_label_type) {
case NetworkViewSettings.ACT: bottom_label = Snns.maxFixPoint(act, 3); break;
case NetworkViewSettings.INIT_ACT: bottom_label = Snns.maxFixPoint(unit.getInitAct(), 3); break;
case NetworkViewSettings.OUTPUT: bottom_label = Snns.maxFixPoint(unit.getOutput(), 3); break;
case NetworkViewSettings.BIAS: bottom_label = Snns.maxFixPoint(unit.getBias(), 3); break;
case NetworkViewSettings.NAME: bottom_label = unit.getName(); break;
case NetworkViewSettings.NUMBER: bottom_label = String.valueOf(unit.getNumber()); break;
case NetworkViewSettings.Z_VAL: bottom_label = String.valueOf( unit.getPosition()[2] ); break;
case NetworkViewSettings.WINNER:
if( unit.equals( winner ) ) bottom_label = "Winner";break;
default: bottom_label = "";
}
}
int[] pos = unit.getPosition();
Point p = new Point( X(pos[0]), Y(pos[1]) );
if( network.isUnitSelected( unit ) )
g.setColor( settings.selection_color );
else if( color == null )
g.setColor(settings.getColor(act / settings.unit_max));
else g.setColor( color );
g.fillRect(p.x-(settings.width >> 1), p.y-(settings.height >> 1), settings.width, settings.height);
g.drawRect(p.x-(settings.width >> 1), p.y-(settings.height >> 1), settings.width, settings.height);
if( edit_top_labels ) {
Dimension d = labels[ unit.getNumber() - 1 ].getSize();
labels[ unit.getNumber() - 1 ].setLocation( p.x - d.width / 2, p.y - (settings.height>>1) - d.height );
}
else drawLabel(g, top_label, p.x, p.y-(settings.height >> 1)-1, CENTER, BOTTOM);
if( edit_bottom_labels ){
Dimension d = labels[ unit.getNumber() - 1 ].getSize();
labels[ unit.getNumber() - 1 ].setLocation( p.x - d.width / 2, p.y + (settings.height>>1) );
}
else drawLabel(g, bottom_label, p.x, p.y+(settings.height >> 1)+1, CENTER, TOP);
if( network.isSelectedSourceUnit( unit ) ) {
g.setColor( settings.getColor( 0 ) );
g.drawRect(p.x-(settings.width >> 1)-1, p.y-(settings.height >> 1)-1, settings.width+2, settings.height+2);
g.setColor( settings.selection_color );
g.drawRect(p.x-(settings.width >> 1), p.y-(settings.height >> 1), settings.width, settings.height);
}
else {
g.setColor(getBackground());
g.drawRect(p.x-(settings.width >> 1)-1, p.y-(settings.height >> 1)-1, settings.width+2, settings.height+2);
}
Point p2 = new Point();
p2.x = p.x + Math.max( font_metrics.stringWidth( top_label ), font_metrics.stringWidth( bottom_label ) ) / 2;
p2.y = p.y + (settings.height >> 1) + 1 + font_metrics.getHeight();
return p2;
}
/**
* Draws a link between two neurons in the window's graphic context.
* The destination neuron is specified by member variables, which
* are set in <code>paint</code>.
*
* @param g graphic context
* @param link
*/
private void drawLink( Graphics g, Link link ){
double weight = link.getWeight();
// ignore very weak weights
if(weight > -settings.link_trigger && weight < settings.link_trigger) return;
double scaled_w;
scaled_w = weight / settings.link_max;
g.setColor( settings.getColor( scaled_w ) );
int[] p = link.getSourceUnit().getPosition();
Point from = new Point( X(p[0]), Y(p[1]) );
p = link.getTargetUnit().getPosition();
Point to = new Point( X(p[0]), Y(p[1]));
if( link.isSelfConnection() ){
g.drawOval(from.x, from.y - settings.height, settings.width, settings.height);
if( settings.show_directions ){
g.drawLine( from.x - settings.width / 4, from.y - 3 * settings.height / 4, from.x, from.y - settings.height / 2 );
g.drawLine( from.x + settings.width / 4, from.y - 3 * settings.height / 4, from.x, from.y - settings.height / 2 );
}
if(settings.show_weights)
drawLabel(g, Snns.maxFixPoint(weight, 3),
from.x + (int)(.5 * ( 1 + .5 * Math.sqrt( 2 ) ) * settings.width),
from.y - (int)(.5 * ( 1 + .5 * Math.sqrt( 2 ) ) * settings.height),
LEFT, BOTTOM);
return;
}
if(settings.show_directions) {
int dx = to.x-from.x,
dy = to.y-from.y;
if(Math.abs(dx) <= Math.abs((double)dy*settings.width/settings.height)) {
// upper or lower edge of the target neuron
int sgn = dy < 0 ? -1 : 1;
to.x -= dx*settings.height / Math.abs(dy)/2;
to.y -= sgn * settings.height/2;
}
else {
// left or right edge of the target neuron
int sgn = dx < 0 ? -1 : 1;
to.x -= sgn * settings.width/2;
to.y -= dy*settings.width / Math.abs(dx)/2;
}
dx = to.x-from.x;
dy = to.y-from.y;
double R = Math.sqrt(settings.width*settings.width +settings.height*settings.height)/3;
double d = Math.sqrt(dx*dx + dy*dy);
double tx = to.x - R*dx/d,
ty = to.y - R*dy/d;
int x1 = (int)(tx - R*dy/d/3),
y1 = (int)(ty + R*dx/d/3),
x2 = (int)(tx + R*dy/d/3),
y2 = (int)(ty - R*dx/d/3);
g.drawLine(to.x, to.y, x1, y1);
g.drawLine(to.x, to.y, x2, y2);
// g.drawLine(x1, y1, x2, y2);
// g.drawLine((int)tx, (int)ty, from.x, from.y);
g.drawLine(to.x, to.y, from.x, from.y);
}
else g.drawLine(from.x, from.y, to.x, to.y);
if(settings.show_weights)
drawLabel(g, Snns.maxFixPoint(weight, 3), (to.x+from.x)>>1, (to.y+from.y)>>1, CENTER, CENTER);
}
/**
* Displays a label (neuron or link) at specified coordinates, with
* specified alignment.
*
* @param g graphic context
* @param str label to display
* @param x x coordinate in window
* @param y y coordinate in window
* @param align_h horizontal alignment of the label
* @param align_v vertical alignment of the label
*/
private void drawLabel(Graphics g, String str, int x, int y, int align_h, int align_v) {
int w = font_metrics.stringWidth(str) + 2;
int h = font_ascent + 2;
switch(align_h) {
case CENTER: x -= (w>>1); break;
case RIGHT: x -= w; break;
}
switch(align_v) {
case CENTER: y -= (h>>1); break;
case BOTTOM: y -= h; break;
}
int x2 = x + 1;
int y2 = y + font_ascent - 1;
if(str != null && !str.equals("")) {
g.setColor(getBackground());
g.fillRect(x, y, w, h);
g.drawRect(x, y, w, h);
g.setColor(getForeground());
g.drawString(str, x2, y2);
}
}
/*------------------- listeners & events -------------------------------------*/
/**
* method adds a new NetworkViewListener
*
* @param listener
*/
public void addListener( NetworkViewListener l ){
//System.out.println("NetworkView.addListener:"+l);
if( !listeners.contains( l ) ) listeners.add( l );
}
/**
* methods gives information to all listeners
*
* @param event type
*/
void fireEvent(int type){
NetworkViewEvent evt = new NetworkViewEvent(this, type);
//System.out.println("NetworkView.fireEvent:"+evt.getMessage());
for( int i=listeners.size()-1; i>-1; i-- )
( (NetworkViewListener)listeners.get( i ) ).networkViewChanged( evt );
}
/**
* method removes a certain listener from the list
*/
public void removeListener(NetworkViewListener l){
//System.out.println("NetworkView.removeListener:"+l);
listeners.remove( l );
}
/**
* shows text fields instead of labels at the top of each unit
* to edit names and values directly
*/
public void editTopLabels(){
edit_top_labels = true;
labels_to_edit = settings.top_label_type;
prepareLabels();
repaint();
}
/**
* shows text fields instead of labels at the bottom of each unit
* to edit names and values directly
*/
public void editBottomLabels(){
edit_bottom_labels = true;
labels_to_edit = settings.base_label_type;
prepareLabels();
repaint();
}
/**
* tries to send the edited labels back to the network
* shows an error message if something was wrong
*/
public void evaluateDirectEdit( boolean delete_edit_labels ){
if( under_construction ) System.out.println("NetworkView.evaluateDirectEdit()");
try{ labels2units( delete_edit_labels ); }
catch( Exception e2 ){
snns.showException( e2, this );
return;
}
if( delete_edit_labels ) {
deleteLabels();
edit_top_labels = edit_bottom_labels = false;
}
network.fireEvent( NetworkEvent.UNIT_VALUES_EDITED );
}
public void dispose(){ close(); }
/*----------------------- protected methods ----------------------------------*/
/**
* Converts given grid x-coordinate to pixel x-coordinate.
*
* @param x x-coordinate, in grid units
* @return corresponding x-coordinate in pixels
*/
protected int X(int x){ return settings.grid_size * x + (settings.width) + 1; }
/**
* Converts given grid Y coordinate to pixel Y coordinate.
*
* @param y y-coordinate, in grid units
* @return corresponding y-coordinate in pixels
*/
protected int Y(int y){ return settings.grid_size * y + (settings.height>>1) + 1 + font_height; }
/*----------------------- private methods ------------------------------------*/
/**
* Checks if there is a unit at given coordinates.
*
* @param p point with queried coordinates.
* @return <code>true</code> if there is a unit at given point
*/
private boolean isPointOnUnit( Point p ){
Unit unit = network.getUnitAtXY( new int[]{ grid_x, grid_y } );
if( unit == null ) return false;
return isPointOnUnit( p, unit );
}
/**
* Checks if the given point lies on the given unit.
*
* @param p point where the unit is expected
* @param unit unit expected at point
* @return <code>true</code> if the point lies on the unit.
*/
private boolean isPointOnUnit(Point p, Unit unit){
int[] pos = unit.getPosition();
if( p.x < X(pos[0]) - settings.width / 2 || p.x > X(pos[0]) + settings.width / 2 ) return false;
if( p.y < Y(pos[1]) - settings.height / 2 || p.y > Y(pos[1]) + settings.height / 2 ) return false;
return true;
}
private Vector getUnitsInRectangle(){
Vector units = new Vector();
for( Unit unit = network.getFirstUnit(); unit!=null; unit = network.getNextUnit() )
if( isUnitInRectangle( unit ) ) units.addElement( unit );
return units;
}
/**
* Checks if a certain unit lies in the selection rectangle
*
* @param Unit
* @return true if this unit is selected
*/
private boolean isUnitInRectangle( Unit unit ){
int[] pos = unit.getPosition();
if( !rectangle.contains( X(pos[0]), Y(pos[1]) ) ) return false;
return true;
}
/**
* moves dragged units away
*
* @return true if a move was necessary
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -