Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Applet redraws itself when I switch to another screen! 1

Status
Not open for further replies.

packersfan

Programmer
Jul 30, 2002
2
0
0
US
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(&quot;one&quot;, 13, Color.blue);
this.addItem(&quot;two&quot;, 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
 
Two things I can think of:

1. Use a private boolean field. Lets call it bChanged. Whenever a method makes a change, set bChanged to true. At the start of your paint method, check bChanged before doing anything. After the paint, set it to false.

2. (Not sure if this will work) yourClass.setIgnoreRepaint(true). According to the api, this
Sets whether or not paint messages received from the operating system should be ignored. This does not affect paint events generated in software by the AWT, unless they are an immediate response to an OS-level paint message. [morning] HavaTheJut
 
Good idea Hava. Very simple, yet seems very workable. I would think that eventually if you had this as a truly dynamic program you would want to run a thread and check the parameters of the inputs for the graph every so often and if they change, to redraw it.
 
Just in case anyone is wondering, I used the removeItem method to remove the items that I had added to the vector after it was drawn and it got rid of the problems!

Thanks for everyone's help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top