packersfan
Programmer
I have an applet that creates an axis and within it, draws a barchart. When I load the applet, it looks perfect, but then when I click to another screen and then click back to my web page, the applet plots another instance of each data point. (I have two data points, so when I click back there are four, and then six and then eight....). I'm stuck and don't know what to do! Any suggestions? Here is the code:
import java.awt.*;
import java.util.*;
import java.applet.*;
public class Plot1 extends java.applet.Applet {
Label statLabel = new Label("Label"
;
int min = 0;
int max = 30;
String title= "Cores to Order";
public void init() {
BorderLayout border = new BorderLayout();
BarChart stats = new BarChart(title, min, max);
setLayout(border);
add("North", statLabel);
add("Center", stats);
}
}
class GraphItem {
String title;
int value;
Color color;
public GraphItem(String title, int value, Color color) {
this.title = title;
this.value = value;
this.color = color;
}// end contructor
} // end GraphItem
class Graph extends java.awt.Canvas {
//variables needed
public int top;
public int bottom;
public int left;
public int right;
int titleHeight;
int labelWidth;
FontMetrics fm;
int padding = 4;
String title;
int min;
int max;
Vector items;
public Graph(String title, int min, int max) {
this.title = title;
this.min = min;
this.max = max;
items = new Vector();
} // end contructor
public Dimension preferredSize() {
return(new Dimension(1000, 1000));
}
public void reshape (int x, int y, int width, int height) {
super.reshape(x, y, width, height);
fm = getFontMetrics(getFont());
titleHeight = fm.getHeight();
labelWidth = Math.max(fm.stringWidth(new Integer(min).toString()), fm.stringWidth(new Integer(max).toString()) + 2);
top = padding + titleHeight;
bottom = size().height - padding;
left = padding + labelWidth;
right = size().width - padding;
} //end reshape
public void paint (Graphics g) {
//draw the title
fm = getFontMetrics(getFont());
g.drawString(title, (size().width - fm.stringWidth(title))/2, top);
//draw max and min values
g.drawString(new Integer(min).toString(), padding, bottom);
g.drawString(new Integer(max).toString(), padding, top + titleHeight);
//draw the vertical and horizontal lines
g.drawLine(left, top, left, bottom);
g.drawLine(left, bottom, right, bottom);
} // end paint
//Facility for adding and removing the items to be graphed
public void addItem(String name, int value, Color col) {
items.addElement(new GraphItem(name, value, col));
}// end addItem
public void addItem(String name, int value) {
items.addElement(new GraphItem(name, value, Color.black));
}// end addItem
public void removeItem(String name) {
for (int i = 0; i < items.size(); i++) {
if (((GraphItem)items.elementAt(i)).title.equals(name))
items.removeElementAt(i);
}
}//end removeItem
} //end Graph
class BarChart extends Graph {
int position;
int increment;
public BarChart(String title, int min, int max) {
super(title, min, max);
} // end constructor
public void paint(Graphics g) {
super.paint(g);
this.addItem("one", 13, Color.blue);
this.addItem("two", 25);
increment = (right - left)/(items.size());
position = left;
Color temp = g.getColor();
for (int i = 0; i < items.size(); i++) {
GraphItem item = (GraphItem)items.elementAt(i);
int adjustedValue = bottom - (((item.value - min)*(bottom - top))
/(max - min));
g.drawString(item.title, position + (increment -
fm.stringWidth(item.title))/2, adjustedValue - 2);
g.setColor(item.color);
g.fillRect(position, adjustedValue, increment,
bottom - adjustedValue);
position+=increment;
g.setColor(temp);
}
} // end paint
} // end BarChart
import java.awt.*;
import java.util.*;
import java.applet.*;
public class Plot1 extends java.applet.Applet {
Label statLabel = new Label("Label"
int min = 0;
int max = 30;
String title= "Cores to Order";
public void init() {
BorderLayout border = new BorderLayout();
BarChart stats = new BarChart(title, min, max);
setLayout(border);
add("North", statLabel);
add("Center", stats);
}
}
class GraphItem {
String title;
int value;
Color color;
public GraphItem(String title, int value, Color color) {
this.title = title;
this.value = value;
this.color = color;
}// end contructor
} // end GraphItem
class Graph extends java.awt.Canvas {
//variables needed
public int top;
public int bottom;
public int left;
public int right;
int titleHeight;
int labelWidth;
FontMetrics fm;
int padding = 4;
String title;
int min;
int max;
Vector items;
public Graph(String title, int min, int max) {
this.title = title;
this.min = min;
this.max = max;
items = new Vector();
} // end contructor
public Dimension preferredSize() {
return(new Dimension(1000, 1000));
}
public void reshape (int x, int y, int width, int height) {
super.reshape(x, y, width, height);
fm = getFontMetrics(getFont());
titleHeight = fm.getHeight();
labelWidth = Math.max(fm.stringWidth(new Integer(min).toString()), fm.stringWidth(new Integer(max).toString()) + 2);
top = padding + titleHeight;
bottom = size().height - padding;
left = padding + labelWidth;
right = size().width - padding;
} //end reshape
public void paint (Graphics g) {
//draw the title
fm = getFontMetrics(getFont());
g.drawString(title, (size().width - fm.stringWidth(title))/2, top);
//draw max and min values
g.drawString(new Integer(min).toString(), padding, bottom);
g.drawString(new Integer(max).toString(), padding, top + titleHeight);
//draw the vertical and horizontal lines
g.drawLine(left, top, left, bottom);
g.drawLine(left, bottom, right, bottom);
} // end paint
//Facility for adding and removing the items to be graphed
public void addItem(String name, int value, Color col) {
items.addElement(new GraphItem(name, value, col));
}// end addItem
public void addItem(String name, int value) {
items.addElement(new GraphItem(name, value, Color.black));
}// end addItem
public void removeItem(String name) {
for (int i = 0; i < items.size(); i++) {
if (((GraphItem)items.elementAt(i)).title.equals(name))
items.removeElementAt(i);
}
}//end removeItem
} //end Graph
class BarChart extends Graph {
int position;
int increment;
public BarChart(String title, int min, int max) {
super(title, min, max);
} // end constructor
public void paint(Graphics g) {
super.paint(g);
this.addItem("one", 13, Color.blue);
this.addItem("two", 25);
increment = (right - left)/(items.size());
position = left;
Color temp = g.getColor();
for (int i = 0; i < items.size(); i++) {
GraphItem item = (GraphItem)items.elementAt(i);
int adjustedValue = bottom - (((item.value - min)*(bottom - top))
/(max - min));
g.drawString(item.title, position + (increment -
fm.stringWidth(item.title))/2, adjustedValue - 2);
g.setColor(item.color);
g.fillRect(position, adjustedValue, increment,
bottom - adjustedValue);
position+=increment;
g.setColor(temp);
}
} // end paint
} // end BarChart