Исходный текст приложения ShowChart приведен в листинге 1.
Листинг 1. Файл ShowChart.java
import java.applet.*; import java.awt.*; import java.net.*; import java.io.*; import java.util.*;
public class ShowChart extends Applet { URL SrcURL; Object URLContent; int errno = 0; String str; byte buf[] = new byte[200];
public String getAppletInfo() { return "Name: ShowChart"; }
public void init() { try { SrcURL = new URL( "http://frolov/chart.txt"); try { InputStream is = SrcURL.openStream(); is.read(buf); str = new String(buf, 0); } catch (IOException ioe) { showStatus("read exception"); errno = 1; } } catch (MalformedURLException uex) { showStatus( "MalformedURLException exception"); errno = 2; } }
public void paint(Graphics g) { Integer AngleFromChart = new Integer(0); int PrevAngle = 0; int rColor, gColor, bColor; Dimension dimAppWndDimension = getSize();
g.setColor(Color.yellow); g.fillRect(0, 0, dimAppWndDimension.width - 1, dimAppWndDimension.height - 1);
g.setColor(Color.black); g.drawRect(0, 0, dimAppWndDimension.width - 1, dimAppWndDimension.height - 1);
showStatus(str); StringTokenizer st = new StringTokenizer(str, ",\r\n");
while(st.hasMoreElements()) { rColor = (int)(255 * Math.random()); gColor = (int)(255 * Math.random()); bColor = (int)(255 * Math.random());
g.setColor(new Color(rColor, gColor, bColor));
String angle = (String)st.nextElement(); AngleFromChart = new Integer(angle) ; g.fillArc(0, 0, 200, 200, PrevAngle, AngleFromChart.intValue()); PrevAngle += AngleFromChart.intValue(); } } }
Исходный текст документа HTML, созданного автоматически для нашего аплета, представлен в листинге 2.
Листинг 2. Файл ShowChart.tmp.html
<applet name="ShowChart" code="ShowChart" codebase= "file:/e:/Sun/Articles/vol12/src/ShowChart" width="200" height="200" align="Top" alt="If you had a java-enabled browser, you would see an applet here."> <hr>If your browser recognized the applet tag, you would see an applet here. <hr> </applet>