Dear friend i have the following applet code, i need your support to reviews this code because with my symantec visual cafe show severals errors on the compilation time.
Thanks by cheking my code.
thanks,
Jairo
**************Applet begin here*********************
import java.applet.Applet;
import java.net.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.*;
import java.util.Enumeration;
public class StripChart extends Applet implements Runnable
{
static int chartWidth = 300;
static int chartHeight = 120;
static Color chartFG = new Color(0x336699);
static Color chartBG = new Color(0xFFCC33);
private Image graph;
private Graphics graphGraphics;
private Graphics screenGraphics;
private Thread runner;
long currentTick = -1;
public StripChart() {
setBackground(chartBG);
setForeground(chartFG);
}
public void addNotify() {
super.addNotify();
try {
screenGraphics = getGraphics();
graph = createImage(chartWidth, chartHeight);
graphGraphics = graph.getGraphics();
graphGraphics.setColor(cha
rtBG);
graphGraphics.fillRect(0,0
,chartWidt
h,chartHei
ght);
} catch (Exception e) { throw new Error("Hosed!"); }
}
public Dimension getPreferredSize() { return new Dimension(chartWidth, chartHeight); }
public void setBackground(Color bg) { chartBG = bg; }
public void setForeground(Color fg) { chartFG = fg; }
public Color getBackground() { return chartBG; }
public Color getForeground() { return chartFG; }
private int value; // only here to implement a fake data feed
/** this should ask the 'world' for the next point to be charted */
protected int getNextChartValue()
{
int K = 8; // max +/- change per tick
int delta = ((int)( Math.random()*(2*K+1))) - K;
value -= delta;
if (value<0) value = 0;
if (value>=chartHeight) value = chartHeight-1;
return value;
}
private int getImageOffset() {
if (currentTick<chartWidth) return 0;
return (int) ((currentTick+1) % chartWidth);
}
private void setNextPoint(int val) {
if (val<0) val=0;
if (val>=chartHeight) val=chartHeight-1;
int x = (int) (++currentTick % chartWidth);
graphGraphics.setColor(cha
rtBG);
graphGraphics.drawLine(x,0
, x,chartHeight-val);
graphGraphics.setColor(cha
rtFG);
graphGraphics.drawLine(x,c
hartHeight
-val, x,chartHeight);
paint(screenGraphics);
}
public void run() {
long mark = System.currentTimeMillis()
;
while (runner!=null) {
setNextPoint(getNextChartV
alue());
// just some timing stuff
if (currentTick % chartWidth == chartWidth - 1) {
long now = System.currentTimeMillis()
;
double elapsed = (now - mark) / 1000.0;
double frames = chartWidth;
int fps = (int) (frames / elapsed + 0.5);
showStatus("fps = " + fps);
mark = now;
}
}
}
public void init() { if (isActive()) showStatus(getAppletInfo()
); }
public void start() { runner=new Thread(this); runner.start(); }
public void stop() { runner=null; }
public void destroy() { }
public void update(Graphics g) { paint(g); }
public void paint(Graphics g) {
if (graph==null) return;
int xoff = getImageOffset();
g.drawImage(graph, -xoff, 0, chartWidth, chartHeight, null);
g.drawImage(graph, chartWidth-xoff, 0, chartWidth, chartHeight, null);
}
public final String getAppletInfo()
{ return "StripChart"; }
public boolean mouseMove(Event evt, int x, int y)
{ showStatus(getAppletInfo()
);
return super.mouseMove(evt, x, y);
}
}
class StripChartApp extends Frame implements AppletStub, AppletContext
{
// AppletStub stuff
public boolean isActive() { return false; }
public URL getDocumentBase() { return null; }
public URL getCodeBase() { return null; }
public String getParameter(String name) { return null; }
public AppletContext getAppletContext() { return this; }
public void appletResize(int width, int height) {}
// AppletContext stuff
public Applet getApplet(String name) { return null; }
public Enumeration getApplets() { return null; }
public AudioClip getAudioClip(URL url) { return null; }
public Image getImage(URL url) { return null; }
public void showDocument(URL url) {}
public void showDocument(URL url, String target) {}
public void showStatus(String txt)
{if (status!=null) status.setText(txt); }
static private Applet applet = null;
static private Label status = null;
StripChartApp(String title) {
super(title);
setLayout(new BorderLayout());
add("Center", applet=new StripChart());
add("South", status=new Label());
applet.setStub(this);
}
public static void main(String args[]) {
Frame frame = new StripChartApp("StripChart"
);
frame.pack();
frame.show();
applet.init();
applet.start();
frame.addWindowListener(ne
w WindowAdapter()
{ public void windowClosing(WindowEvent e) { System.exit(0); } });
}
}
*******applet end here******************