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

📄 inputspacecanvas.java

📁 Single-layer neural networks can be trained using various learning algorithms. The best-known algori
💻 JAVA
字号:
import java.awt.*;import java.util.*;import java.awt.event.*;//import java.awt.image.*;//Modified by Sebastien Baehni in order to support further jvm//(some of the methods were deprecated)class InputSpaceCanvas extends Canvas implements MouseListener, Runnable{  /**	 * 	 */	private static final long serialVersionUID = 1L;	final int BACKGROUND_SIZE_X = 21;	final int BACKGROUND_SIZE_Y = 21;  float background[][];  static Vector points;  boolean play = false;  int value;  private Image dbImage;  private Graphics dbGraphics;  private Thread th;  SimplePerceptronApplet perceptronApplet;      public void startAnimation(SimplePerceptronApplet perceptronApplet)  {	  this.perceptronApplet = perceptronApplet;	  Thread th = new Thread(this);	  th.start();  }    public void run() {      while (play) {         repaint();		 this.perceptronApplet.learnPoints();         try {			 int delay = (Integer.valueOf(this.perceptronApplet.timedelayTF.getText())).intValue();            Thread.sleep(delay);         } catch (InterruptedException e) {            //nichts         }      }  }    Rectangle getBound_sym() {	  Rectangle r = getBounds();	  r.width = r.height;	  return r;  }  InputSpaceCanvas()  {    points = new Vector();        value = 1;    background = new float[BACKGROUND_SIZE_X][BACKGROUND_SIZE_Y];    for(int i=0;i<BACKGROUND_SIZE_X; i++)     for(int j=0;j<BACKGROUND_SIZE_Y; j++)      background[i][j] = -100;    this.setSize(550,400);    this.addMouseListener(this);  }	int coord_x(double x1) {		Rectangle r = getBound_sym(); 		r.width = r.height;		int xx = (int) (x1*r.width*3/4 + 20 );		return xx;	}	int coord_y(double x2) {		Rectangle r = getBound_sym(); 	    int yy = (int) (r.height - x2*r.height*3/4 - 20);		return yy;	}  /*	public void update( Graphics g )	{	  paint( g );	}	*/	public void update(Graphics g)	{	   //Double-Buffer initialisieren	   if (dbImage == null) {	      dbImage = createImage(this.size().width,this.size().height);	      dbGraphics = dbImage.getGraphics();	   }	   //Hintergrund l鰏chen	   dbGraphics.setColor(getBackground());	   dbGraphics.fillRect(0,0,this.size().width,this.size().height);	   //Vordergrund zeichnen	   dbGraphics.setColor(getForeground());	   paint(dbGraphics);	   //Offscreen anzeigen	   g.drawImage(dbImage,0,0,this);	}			  public void paint(Graphics g)  {    Rectangle r = getBound_sym();	//BufferedImage imageBuffer = new BufferedImage(r.width, r.height, BufferedImage.TYPE_INT_RGB);	//Graphics2D g = imageBuffer.createGraphics();		    g.setColor(Color.white);    g.fillRect(0,0,r.width,r.height);	//g.clearRect(0,0,r.width,r.height);    int v,xx,yy;    int h = (r.height - 50)/BACKGROUND_SIZE_Y;    int w = (r.width - 50)/BACKGROUND_SIZE_X;    for(int x1 = 0; x1 < BACKGROUND_SIZE_X; x1++)     for(int x2 = 0; x2 < BACKGROUND_SIZE_Y; x2++)     {       xx = x1*r.width*3/(4*(BACKGROUND_SIZE_X-1)) + 20 - w/2;       yy = r.height - x2*r.height*3/(4*(BACKGROUND_SIZE_Y-1)) - 21 - h/2;       if (background[x1][x2]!=-100)       {         g.setColor(new Color(background[x1][x2],0,1-background[x1][x2]));         g.fillRect(xx,yy,w,h);         if (background[x1][x2] > 0.5) g.setColor(Color.red);         else g.setColor(Color.blue);         g.fillRect(xx+w/3,yy+h/3,w/3,h/3);       }     }	g.setColor(Color.white);	g.fillRect(21, 0, r.width - 21, r.height/4-20);	g.fillRect(21 + 3 * r.width/4, r.height/4-19, 100, r.height*3/4);		g.setColor(Color.black);	if (Perceptron.OPTIMAL == Perceptron.algorithm) {		Neuron output_neuron = (Neuron) SimplePerceptronApplet.perceptron.outputLayer.neurons.get(0);		Enumeration syn = output_neuron.inlinks.elements();		Synapse synapse;		double[] weight_draw = new double[2];		for (int i = 0; 2 > i; i++) {			synapse = (Synapse)syn.nextElement();			weight_draw[i] = synapse.weight;		}		//g.drawLine(20, r.height - 20, (int) (100 * weight_draw[0]) + 20, r.height - 20 - (int) (100 * weight_draw[1]));		if (Perceptron.weights_set) {			Enumeration tns = Perceptron.threshold_neuron.outlinks.elements();			Synapse tnsyn = (Synapse)tns.nextElement();			for (int i = -1; i < 2; i++) {				double start_x = (tnsyn.weight + i) / weight_draw[0];				double stop_x = - 2.0 * weight_draw[1] / weight_draw[0] + (tnsyn.weight + i) / weight_draw[0];				int i_start_x = this.coord_x(start_x);				int i_start_y = this.coord_y(0.0);				int i_stop_x = this.coord_x(stop_x);				int i_stop_y = this.coord_y(2.0);				g.drawLine(i_start_x, i_start_y, i_stop_x, i_stop_y);			}		}	}	g.setColor(Color.white);	g.fillRect(0, 0, 20, r.height);	g.fillRect(0, r.height - 20, r.width, 20);			g.setColor(Color.black);    g.drawLine(5,r.height-20,r.width-5,r.height-20);	//x-Achse	g.drawLine(20,r.height-5,20,5);						//y-Achse	g.setColor(Color.lightGray);	g.drawLine(5,r.height/4-20,r.width-5,r.height/4-20);	//x-Achse	g.drawLine(20 + 3 * r.width/4,r.height-5,20 + 3 * r.width/4,5);	//y-Achse	g.setColor(Color.black);    	    g.drawLine(r.width-5,r.height-20,r.width-10,r.height-17); //x-Pfeil    g.drawLine(r.width-5,r.height-20,r.width-10,r.height-23); //    g.drawLine(20,5,17,10); 							//y-Pfeil    g.drawLine(20,5,23,10); 							//    g.drawString("x1", r.width-25, r.height-5);    g.drawString("x2",3,25);    g.drawLine(20 + 3 * r.width/4, r.height-25,20+3 * r.width/4, r.height-15);    g.drawLine(15,r.height/4-20,25,r.height/4-20);    g.drawString("1",3,r.height/4-16);    g.drawString("1",22+3*r.width/4,r.height-5);    g.drawString("0",8,r.height-5);    Enumeration e = InputSpaceCanvas.points.elements();    Double X;    Double Y;    while (e.hasMoreElements())    {		Sample sample = (Sample) e.nextElement();		//drawPoint((int) sample.out,sample.in[0],sample.in[1]);		if ((Perceptron.OPTIMAL == Perceptron.algorithm) || (Perceptron.SVM == Perceptron.algorithm)){			drawPoint((int) sample.out,sample.in[0],sample.in[1], sample.alpha, (Graphics2D) g);		}		else {			drawPoint((int) sample.out,sample.in[0],sample.in[1], 1.0,(Graphics2D) g);		}    }	//g0.drawImage(imageBuffer, 0, 0, this);  }/*	public void paint(Graphics g)	   {	      int xoffs = 10;	      int yoffs = 10;	      g.setColor(Color.lightGray);	      g.fillOval(xoffs+actx,yoffs+20,100,100);	      g.setColor(Color.red);	      g.drawArc(xoffs+actx,yoffs+20,100,100,actarc1,10);	      g.drawArc(xoffs+actx-1,yoffs+19,102,102,actarc1,10);	      g.setColor(Color.blue);	      g.drawArc(xoffs+actx,yoffs+20,100,100,360-actarc2,10);	      g.drawArc(xoffs+actx-1,yoffs+19,102,102,360-actarc2,10);	   }	   */    // Implementation of the mouselistener ...    public void mouseClicked(MouseEvent e) {		int t_value;		if (1 == value) {			if(3 == e.getButton()) {				t_value = 0;			}			else {				t_value = 1;			}		}		else {			t_value = 0;		}				Rectangle r = getBound_sym();		Double X = new Double((float)(e.getX()-20) / (float)(3*r.width/4));		Double Y = new Double((float)(r.height-20-e.getY()) / (float)(3*r.height/4));		double[] in = new double[2];		in[0] = X.doubleValue();		in[1] = Y.doubleValue();		double out = (double) t_value;		Sample sample = new Sample(in, out);		points.addElement(sample);			Graphics g = getGraphics();				drawPoint(t_value,in[0],in[1], 1.0, (Graphics2D) g);    }    public void mouseEntered(MouseEvent e) {}    public void mouseExited(MouseEvent e) {}    public void mousePressed(MouseEvent e) {}    public void mouseReleased(MouseEvent e) {}	public void drawPoint(int v,double X,double Y, double alpha, Graphics2D g)	{	    //Graphics g = getGraphics();	    Rectangle r = getBound_sym();		//alpha = 0.0;	    int x = 20 + (int)(X*3*r.width/4);	    int y = r.height - 20 - (int)(Y*3*r.height/4);	    if (v==1) g.setColor(Color.red); else g.setColor(Color.blue);	    g.fillArc(x-3,y-3,7,7,0,360);	    g.setColor(Color.white);		float f_alpha = (float) alpha/2;		if (1.0 < f_alpha) {f_alpha = (float) 1.0;}		Color kringel = new Color(f_alpha, f_alpha, f_alpha);		g.setColor(kringel);	    g.drawArc(x-3,y-3,7,7,0,360);	}		//public void drawPoint(int v,double X,double Y) {	//	drawPoint( v,  X,  Y, 1.0);	//}	  public void clearPoints()  {    points.removeAllElements();    repaint();  }  public void setValue(int v)  {    value = v;  }  public void setBackground(double x1, double x2, double v)  {    int x = (int)(x1*(BACKGROUND_SIZE_X-1)+0.01);    int y = (int)(x2*(BACKGROUND_SIZE_Y-1)+0.01);	if (-100 != v) {		if ((Perceptron.OPTIMAL == Perceptron.algorithm) || (Perceptron.SVM == Perceptron.algorithm)) {			if (1.0 < v) {				v = 1.0;			}			else if ((-1.0 < v) && (1.0 > v)) {				v = (v + 1) / 2;			}			else {				v = 0.0;			}		}		else {		    if (v > 1.0) v = 1.0;		    else if ((v < 0.0)&&(v != -100.0)) v = 0.0;		}	}    background[x][y]=(float)v;  }}

⌨️ 快捷键说明

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