panel1 = new Panel(); label = new Label("Plotting a Function" + MaxActByts + "aa" + count); panel1.add(label); add("North", panel1);
canvas = new PlotCanvas(this,ActByts,count,MaxActByts); add("Center", canvas); fromLabel = new Label("-100");
/* Bottom Panel, with from and to and status DrawLine. */
bottomPanel = new Panel(); bottomPanel.setLayout(new GridLayout(0,1,5,5));
/* Domain panel */ xdomainPanel = new Panel();
xdomainPanel.add(new Label("Plot x from : ")); xdomainPanel.add(new Label("to : ")); bottomPanel.add(xdomainPanel);
/* Status DrawLine */ statusPanel = new Panel(); statusLabel = new Label(" "); statusLabel.setForeground(Color.magenta); statusPanel.add(statusLabel);
bottomPanel.add(statusPanel);
add("South", bottomPanel); }
class PlotCanvas extends Canvas { DrawLine containedBy; public int mousex, mousey; public boolean isMouse = false; int timeThrough = 0; Color color = Color.green; int inset = 20; int fn = 0; float ymax, ymin, xmax, xmin; int[] ActByts; int count; int MaxActByts; PlotCanvas(DrawLine containedByIn,int[] ActByts_,int count_,int MaxActByts_) { super(); containedBy = containedByIn; xmin = 0; xmax = 100; ActByts = ActByts_; count = count_; MaxActByts = MaxActByts_; } public void changeColor (Color inColor) { color = inColor; } public void setFn(int newFn) { fn = newFn; } public void setXmin(float newXmin) { xmin = newXmin; } public float getXmin() { return(xmin); } public void setXmax(float newXmax) { xmax = newXmax; } /** Draws a box around this panel, plus other stuff. */ public void paint(Graphics g) { int x, y; Dimension d = size(); /* These need to be made into floats. */ float xcur, ycur; float xplot, yplot; PlotCoord pc;
g.drawRect(0,0, d.width - 1, d.height - 1);
g.setColor(color); /* This time through is just to find out what the max y value is. */ ymax = 0; ymin = 0; ymax = MaxActByts; float stepSize = (xmax - xmin)/50; /* float stepSize = (xmax - xmin)/50; for (xcur = xmin; xcur <= xmax; xcur += stepSize) { // The function we are going to plot. ycur = func(xcur);
if (ycur > ymax) ymax = ycur; if (ycur < ymin) ymin = ycur; } */ pc = new PlotCoord (xmax, ymax, xmin, ymin, d.width, d.height, inset);
switch (fn) { case 0: retval = Math.abs(x) + 30; break; case 1: retval = x * x; break; case 2: retval = x * x * x; break; case 3: retval = x * x + x - 52; break; default: System.out.println("Bad function number"); retval = -1; } return(retval); } /* public boolean mouseDown(Event e, int x, int y) { Graphics gc; gc = getGraphics();
用一个exe程序生成图片.
2 用定时刷新(用一定时器重画)
我做过,可E-mail to me:[email protected]
file : DrawLine.java
*/
import java.awt.*;import java.awt.geom.*;
import java.awt.font.*;
import java.util.*;
import java.lang.*;
import java.applet.Applet;public class DrawLine extends Applet{ TextField fromFld, toFld;
String MyText;
Label label, toLabel, fromLabel, statusLabel;
PlotCanvas canvas;
public int i;
Button redb, blueb, domainChangeb;
Color color;
Panel panel1, panel2, bottomPanel, statusPanel, xdomainPanel;
Choice fnChoice;
float beginXmin = 0, beginXmax = 100;
int MaxActByts;
int actbyts;
int[] ActByts;
int count=5;
public void init() {
MaxActByts = Integer.parseInt(getParameter("MaxActByts"));
count = Integer.parseInt(getParameter("Row_count"));
ActByts = new int[count];
for ( int j=0;j<count;j++ ){
ActByts[j] = Integer.parseInt(getParameter("ActByts"+j));
}
actbyts =Integer.parseInt(getParameter("ActByts0"));
setLayout(new BorderLayout(5,5));
panel1 = new Panel();
label = new Label("Plotting a Function" + MaxActByts + "aa" + count);
panel1.add(label);
add("North", panel1);
canvas = new PlotCanvas(this,ActByts,count,MaxActByts);
add("Center", canvas);
fromLabel = new Label("-100");
/* Bottom Panel, with from and to and status DrawLine. */
bottomPanel = new Panel();
bottomPanel.setLayout(new GridLayout(0,1,5,5));
/* Domain panel */
xdomainPanel = new Panel();
xdomainPanel.add(new Label("Plot x from : "));
xdomainPanel.add(new Label("to : "));
bottomPanel.add(xdomainPanel);
/* Status DrawLine */
statusPanel = new Panel();
statusLabel = new Label(" ");
statusLabel.setForeground(Color.magenta);
statusPanel.add(statusLabel);
bottomPanel.add(statusPanel);
add("South", bottomPanel);
}
class PlotCanvas extends Canvas {
DrawLine containedBy;
public int mousex, mousey;
public boolean isMouse = false;
int timeThrough = 0;
Color color = Color.green;
int inset = 20;
int fn = 0;
float ymax, ymin, xmax, xmin;
int[] ActByts;
int count;
int MaxActByts;
PlotCanvas(DrawLine containedByIn,int[] ActByts_,int count_,int MaxActByts_) {
super();
containedBy = containedByIn;
xmin = 0;
xmax = 100;
ActByts = ActByts_;
count = count_;
MaxActByts = MaxActByts_;
} public void changeColor (Color inColor) {
color = inColor;
}
public void setFn(int newFn) {
fn = newFn;
} public void setXmin(float newXmin) {
xmin = newXmin;
} public float getXmin() {
return(xmin);
} public void setXmax(float newXmax) {
xmax = newXmax;
} /** Draws a box around this panel, plus other stuff. */
public void paint(Graphics g) {
int x, y;
Dimension d = size();
/* These need to be made into floats. */
float xcur, ycur;
float xplot, yplot;
PlotCoord pc;
g.drawRect(0,0, d.width - 1, d.height - 1);
g.setColor(color);
/* This time through is just to find out what the max y value is. */
ymax = 0;
ymin = 0;
ymax = MaxActByts;
float stepSize = (xmax - xmin)/50;
/*
float stepSize = (xmax - xmin)/50;
for (xcur = xmin; xcur <= xmax; xcur += stepSize) {
// The function we are going to plot.
ycur = func(xcur);
if (ycur > ymax)
ymax = ycur;
if (ycur < ymin)
ymin = ycur;
}
*/
pc = new PlotCoord (xmax, ymax, xmin, ymin, d.width, d.height, inset);
int xcoord = 0, ycoord = 0;
g.setColor(Color.black);
float xaxisStart = xmax, xaxisEnd = xmin, xaxisPosy;
float yaxisStart = ymax, yaxisEnd = ymin, yaxisPosx;;
/* Axis rules:
If 0 is on the map, print out the axis there.
If 0 is not on the map, print out the axis on the
minimum. */
if (xmin < 0 && xmax > 0)
yaxisPosx = 0;
else
yaxisPosx = xmin;
if (ymin < 0 && ymax > 0)
xaxisPosy = 0;
else
xaxisPosy = ymin ;
/* System.out.println("Yaxis at x = " + yaxisPosx +
" Xaxis at y = " + xaxisPosy);
*/
Font f = new Font ("Dialog", Font.PLAIN, 10);
g.setFont(f);
/* Draw x axis. */
g.drawLine(pc.xcoord(xaxisStart), pc.ycoord(xaxisPosy),
pc.xcoord(xaxisEnd), pc.ycoord(xaxisPosy));
/* Draw y axis. */
g.drawLine(pc.xcoord(yaxisPosx), pc.ycoord(yaxisStart),
pc.xcoord(yaxisPosx), pc.ycoord(yaxisEnd));
/* This draws one tick on y */
ycoord = pc.ycoord(ymin + (ymax - ymin)/2);
xcoord = pc.xcoord(yaxisPosx);
g.drawLine(xcoord+3, ycoord , xcoord - 3, ycoord);
g.drawString(
new Integer(Math.round(ymin + (ymax - ymin)/2)).toString(),
xcoord + 5, ycoord + 10);
/* This draws one tick on x */
xcoord = pc.xcoord(xmin + (xmax - xmin)/2);
ycoord = pc.ycoord(xaxisPosy);
g.drawLine(xcoord, ycoord + 3, xcoord , ycoord - 3);
g.drawString(
new Integer(Math.round(xmin + (xmax - xmin)/2)).toString(),
xcoord -5, ycoord + 15);
/* draw a tick at 0 if it is on the graph */
if (xmin < 0 && xmax > 0) {
xcoord = pc.xcoord(0);
ycoord = pc.ycoord(xaxisPosy);
g.drawLine(xcoord, ycoord + 3, xcoord , ycoord - 3);
g.drawString("0", xcoord -5, ycoord + 15);
}
if (ymin < 0 && ymax > 0) {
ycoord = pc.ycoord(0);
xcoord = pc.xcoord(yaxisPosx);
g.drawLine(xcoord+3, ycoord , xcoord - 3, ycoord);
g.drawString("0", xcoord + 7, ycoord + 10);
}
/* Draw tick s at the max's. */
/* This draws one tick on y */
ycoord = pc.ycoord(ymax);
xcoord = pc.xcoord(yaxisPosx);
g.drawLine(xcoord+3, ycoord , xcoord - 3, ycoord);
g.drawString(
new Integer(Math.round(ymax)).toString(),
xcoord + 5, ycoord + 10);
/* This draws one tick on x */
xcoord = pc.xcoord(xmax);
ycoord = pc.ycoord(xaxisPosy);
g.drawLine(xcoord, ycoord + 3, xcoord , ycoord - 3);
g.drawString(
new Integer(Math.round(xmax)).toString(),
xcoord - 5, ycoord + 15);
/* end of tick s at the max's. */
g.setColor(color);
int xcoordPrev = 0, ycoordPrev = 0;
float xprev = 0, yprev = 0;
stepSize = (xmax - xmin)/10;
int k=0;
for (xcur = xmin; xcur <= xmax; xcur += stepSize) {
//ycur = func(xcur);
ycur=0;
if (k < count ){
ycur = ((float)ActByts[k]);
}
k++;
xcoord = pc.xcoord(xcur);
ycoord = pc.ycoord(ycur);
/* ie, don't do the very first x */
if (xcur != xmin) {
/*if (xcur > -0.01 && xcur < 0.01 )
System.out.println( xprev + ":" +
xcoordPrev + " " +
yprev + ":" +
ycoordPrev + " " +
xcur + ":" +
xcoord + " " +
ycur + ":" +
ycoord);*/
/*g.drawOval(xcoord-3, ycoord-3, 6, 6);*/
g.drawLine(xcoordPrev, ycoordPrev,
xcoord, ycoord);
}
g.drawString(String.valueOf(ActByts[k]), xcoord, ycoord + 5);
xcoordPrev = xcoord;
ycoordPrev = ycoord;
xprev = xcur;
yprev = ycur;
}
} float func( float x) {
float retval;
switch (fn) {
case 0:
retval = Math.abs(x) + 30;
break;
case 1:
retval = x * x;
break;
case 2:
retval = x * x * x;
break;
case 3:
retval = x * x + x - 52;
break;
default:
System.out.println("Bad function number");
retval = -1;
}
return(retval);
}
/* public boolean mouseDown(Event e, int x, int y) {
Graphics gc;
gc = getGraphics();
isMouse = true;
mousex = x;
mousey = y;
System.out.println("Got a mouse event at " + x + ", " + y);
gc.setColor(color);
System.out.println(color);
System.out.println(gc.getColor());
gc.drawLine(mousex, mousey, mousex + 50, mousey + 10);
return true;
}
*/
}
}<!-- index.htm --><html><head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
<meta NAME="GENERATOR" CONTENT="Microsoft FrontPage 3.0">
<title>SimplePlot.java</title>
</head><body TEXT="#FFFFFF" BGCOLOR="#000000" LINK="#EEEE00" VLINK="#009999">
<h1 align="center">Report Char</h1><hr align="center">
<p align="center">
<applet code="line70.class" width="600" height="400">
<param name=MaxActByts value="6480">
<param name=Row_Count value="10"><param name=ActByts0 value="36">
<param name=ActByts1 value="12">
<param name=ActByts2 value="176">
<param name=ActByts3 value="159">
<param name=ActByts4 value="2472">
<param name=ActByts5 value="275">
<param name=ActByts6 value="31">
<param name=ActByts7 value="30">
<param name=ActByts8 value="5922">
<param name=ActByts9 value="6480">
</applet>
</p>
<hr align="center">
<b>