carfield.com.hk misc.dfPackage 2000-06-19T16:00:00Z 2000-06-19T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-19T16:00:00Z Graph.java 2000-06-17T16:00:00Z 2000-06-17T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100"> import javax.swing.*; import java.awt.*; /** * Title: Graph&lt;p&gt; * Description: Simple program to produce a graph for the book&lt;p&gt; * Copyright: Copyright (c) Mark Watson&lt;p&gt; * Company: &lt;p&gt; * @author Mark Watson * @version 1.0 */ public class Graph extends JFrame { GraphPanel jPanel1; float [] data1; float [] data2; public Graph() { try { int size = 500; data1 = new float[size]; data2 = new float[size]; float xmin = -5; float xmax = 5; for (int i=0; i&lt;size; i++) { float x = i; x = xmin + x * (xmax - xmin) / (float)size; data1[i] = sigmoid(x); data2[i] = sigmoidP(x); } jbInit(); } catch(Exception e) { e.printStackTrace(); } } protected float sigmoid(float x) { return (float)((1.0f/(1.0f+Math.exp((double)(-x))))-0.5f); } protected float sigmoidP(float x) { double z = sigmoid(x) + 0.5f; return (float)(z * (1.0f - z)); } public static void main(String[] args) { Graph untitled11 = new Graph(); } private void jbInit() throws Exception { jPanel1 = new GraphPanel(data1, data2); jPanel1.setBackground(Color.white); this.setDefaultCloseOperation(3); this.getContentPane().add(jPanel1, BorderLayout.CENTER); setSize(550, 300); jPanel1.setVisible(true); setVisible(true); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-17T16:00:00Z GraphPanel.java 2000-06-17T16:00:00Z 2000-06-17T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">/** * Title: &lt;p&gt; * Description: &lt;p&gt; * Copyright: Copyright (c) &lt;p&gt; * Company: &lt;p&gt; * @author * @version 1.0 */ import javax.swing.*; import java.awt.*; public class GraphPanel extends java.awt.Canvas { // JPanel { public GraphPanel(float [] data1, float []data2) { super(); this.data1 = data1; this.data2 = data2; } Color black = new Color(0, 0, 0); float [] data1; float [] data2; public void paint(Graphics g) { if (data1 == null || data2 == null) return; int width = this.getWidth(); int height = this.getHeight(); System.out.println(&quot;height=&quot;+height); float min = 99999999.9f; float max = -min; int maxindex = 0; float maxval = 0.0f; for (int i=0; i&lt;data1.length; i++) { // assume length of data1 and data2 are the same if (min &gt; data1[i]) min = data1[i]; if (max &lt; data1[i]) max = data1[i]; if (min &gt; data2[i]) min = data2[i]; if (max &lt; data2[i]) max = data2[i]; } System.out.println(&quot;min=&quot; + min +&quot;, max=&quot; + max); g.setColor(Color.red); for (int i=0; i&lt;data1.length - 1; i++) { float y1 = height - 5 - 0.95f *height * ((data1[i] - min) / (max - min)); float y2 = height - 5 - 0.95f *height * ((data1[i+1] - min) / (max - min)); //System.out.println(&quot;data[&quot;+i+&quot;]=&quot;+data[i]+&quot;, y1=&quot;+y1+&quot;, y2=&quot;+y2); g.drawLine(i+20, (int)y1, i+21, (int)y2); y1 = height - 5 - 0.95f *height * ((data2[i] - min) / (max - min)); y2 = height - 5 - 0.95f *height * ((data2[i+1] - min) / (max - min)); //System.out.println(&quot;data[&quot;+i+&quot;]=&quot;+data[i]+&quot;, y1=&quot;+y1+&quot;, y2=&quot;+y2); g.drawLine(i+20, (int)y1, i+21, (int)y2); } float yzero = height - 5 - 0.95f *height * ((0.0f - min) / (max - min)); g.setColor(black); g.drawLine(20, (int)yzero, data2.length + 19, (int)yzero); g.drawLine(width / 2, height/2 - 118, width/2, height/2 + 118); g.drawString(&quot;Sigmoid&quot;, width / 2 - 100, 3 * height / 4 - 10); g.drawString(&quot;SigmoidP&quot;, width / 3 - 5, 1 * height / 4 + 10); g.drawString(&quot;-5&quot;, 4, (int)yzero); g.drawString(&quot;5&quot;, width - 19, (int)yzero); g.drawString(&quot;0.5&quot;, width/2 - 7, 12); g.drawString(&quot;-0.5&quot;, width/2 - 9, height - 3); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-17T16:00:00Z