/** * GaGraph.java * J Scott Cameron *This is a swing component that you can drop into any *Swing panel or frame that will graph data for *a GeneticAlgorithm **/ import javax.swing.*; import java.awt.*; import java.awt.geom.*; import java.awt.font.*; import java.applet.*; import Knapsack.*; import GeneticAlgorithm.*; import java.util.*; import java.awt.event.*; import java.io.*; public class GaGraph extends JPanel { Vector data;/*Vector that contains Genotypes of the *best rated individuals from each *generation*/ Vector avgData;/*Vector that contains average ratings * from each generation*/ int w, h; /*width/height of panel*/ /* initialize the graph with appropriate vectors */ public GaGraph(Vector v,Vector v2) { data = v; avgData = v2; } /* update the data for the graph and repaint*/ public void updateGraph(Vector v,Vector v2) { data = v; avgData = v2; repaint(); } /* draw the graph */ public void paintComponent(Graphics g) { super.paintComponent(g); setBackground(Color.white); w = getSize().width; h = getSize().height; /* give the graph a margin of 20 pixels */ int graphBottom = h-20; int graphRight = w-20; Graphics2D g2; g2 = (Graphics2D) g; /* set Rendering state so that the graph is pretty * and anti-aliased */ g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); /* find the best score and worst score as the range of the graph*/ int bestScore = ((Genotype)data.get(data.size()-1)).getRating(); if (bestScore < 0) bestScore =0; int worstScore = 0; for(int i =0 ;i