Jake Horvath
Posts: 3
Nickname: horvath
Registered: May, 2006
|
|
Re: Need help with Java program
|
Posted: May 3, 2006 5:48 PM
|
|
Here are the other two classes that are almost fully complete except for a few minor parts.
import java.awt.*; import javax.swing.*; import java.util.ArrayList;
public class NameComponent extends JComponent { Purpose: defines the "graph", painting grid lines and plot for each searched and found name */ private int grSize = 600; // initial plot dimensions // array of colors for plot lines private Color[] lineClrs = {Color.black, Color.red, Color.blue, Color.green}; private ArrayList<NameRecord> plotVar; // list of currently plotted names private final int DECADES = 11; // number of decades with data private final int GAP = 20; // spacing gap in pixels private final int maxPlotY = 1000; // largest rank plot limit
// constructor public NameComponent() { setPreferredSize(new Dimension(grSize, grSize)); // set JComponent size plotVar = new ArrayList<NameRecord>(); // instantiate list for plotted names }
// adds name to be plotted to list public void addName(NameRecord nameToPlot) { plotVar.add(nameToPlot); repaint(); // call paintComponent }
// clears all plotted names public void clearAll() { plotVar.clear(); // remove all elements of list repaint(); // call paintComponent }
// clears earliest plotted name public void clearOne() { if (plotVar.size() > 0) // checks for existence of a name plot { plotVar.remove(0); // remove 0th name plot repaint(); // call paintComponent } }
// paintComponent public void paintComponent(Graphics g) { int xMax = getWidth(); // get container size int yMax = getHeight();
g.drawLine(0, GAP, xMax, GAP); // draw top and bottom lines GAP pixels "in" g.drawLine(0, yMax - GAP, xMax, yMax - GAP); for (int k=0; k<DECADES; k++) // draw and label vertical decade lines { int i1 = (xMax * k) / DECADES; g.drawLine(i1, 0, i1, yMax); g.drawString("" + (1900 + k * 10), i1 + 2, yMax - 2); }
for(int i=0; i<plotVar.size(); i++) { g.setColor(lineClrs[i % lineClrs.length]); drawLines(g, plotVar.get(i)); } } // draws lines to connect decades public void drawLines(Graphics g, NameRecord nameToPlot) { int xMax = getWidth(); // get container dimensions int yMax = getHeight();
// draw line graph for name ranks - TO BE IMPLEMENTED } }
public class NameRecord { /** Purpose: class definition for one line of data: name and 11 ranks */
private String aName; // name from data record private int[] ranks; // array of ranks by decade private final int MAX_RANK = 1500; // assumed maximum rank private final int START = 1900; // starting year for rank data private final int DECADES = 11; // number of decades of rank data
// constructor public NameRecord(String s) { String strArray[] = s.split(" "); // create array of strings by // splitting input string at each blank aName = strArray[0]; // store name from 0th element of strArray ranks = new int[strArray.length-1]; // create array for ranks for (int i=1; i<strArray.length; i++) // store each rank from 1st position forward { // of strArray ranks[i-1] = Integer.parseInt(strArray[i]); } }
// returns name from data record public String getName() { return aName; }
// return rank stored in ith position public int getRank(int i) { return ranks[i]; }
// determine and return year with best rank public int bestYear() { int year; return 1900; // return "year" of best rank - TO BE MODIFIED } }
|
|