21. marts 2005 - 13:35
#9
jeg har følgende to classer
package bikeappl;
import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JScrollBar;
/**
* <p>Title: OOP2A Bike Monitor</p>
*
* <p>Description: This is the abstract class for displaying all time dependent bike data graphs. The class
* implements drawing the complete graph except the individual bike data objects, which is
* of course delegated to these objects.</p>
*
*
* @author
* @version 1.0
*/
public abstract class BikeDataGraph extends Graph implements ModelObserver
{
private JScrollBar scrollBar; //Scrollbar for analyzing historical bike data
private LinkedList graphObjList; //A list holding all bike data objects current displayed in the graph
private String graphName;
private String traineeName;
private Color graphColor;
private Color gridColor;
private final int NUMBER_OF_X_INCREMENTS = 50;
private final int NUMBER_OF_Y_INCREMENTS = 5;
private final int FRAME_SIZE = 10;
private int elapsedTime;
private int deltaX; //Defines a x increment in number of pixels
private int deltaY; //Defines a Y increment in number of pixels
private int yOffset; //Defines the offset at the y-axis in number of pixels
private int yStart;
private Model model;
private Point startPoint; //Defines the new starting point for drawing a graph segment
public BikeDataGraph(Model model, String graphName, String traineeName, Color graphColor,
Color backgroundColor)
{
this.model = model;
graphObjList = new LinkedList();
this.graphName = graphName;
this.traineeName = traineeName;
this.graphColor = graphColor;
gridColor = Color.DARK_GRAY;
elapsedTime = 0;
yStart = 0;
this.setLayout(new BorderLayout());
setBackground(backgroundColor);
}
/**
* Delegates graph drawing to the individual bike data object.
*
* @param bikeObj the bike object to be drawn
* @param g the graphics for this window
* @param startPoint the starting point for the drawning, this point is updated after drawning
* @param deltaX the x increment
* @param yOffset the offset at the y-axis
*/
protected abstract void drawObject(BikeData bikeObj, Graphics g, Point startPoint, int deltaX, int yOffset);
/**
* Returns the specific y-value for a bike data object.
*
* @param bikeObj the bike data object
*/
protected abstract int get_Y_Value(BikeData bikeObj);
/**
* Overrides the method in JComponent to draw the specific graph
*
* @param g Graphics
*/
public void paintComponent(Graphics g)
{
super.paintComponent(g); //first call the method in the super class to redraw its components
//g.draw3DRect(0, 0, getWidth(), getHeight(), true);
deltaX = getWidth() / NUMBER_OF_X_INCREMENTS;
yOffset = getHeight() - FRAME_SIZE;
deltaY = yOffset / NUMBER_OF_Y_INCREMENTS;
//System.out.println("deltaX: " + deltaX);
//System.out.println("yOffset: " + yOffset);
startPoint = new Point(0, yStart);
draw(g); //draw grid and graph
}
/**
* Implements the method defined in the super class
*
* @param view - true makes the graph visible else false makes the graph invisible
*/
public void setView(boolean view)
{
setVisible(false);
setVisible(view);
if (view)
model.addListener(this); //register as listener at the model
else
model.removeListener(this);
}
/**
* Draws the grid
*
* @param g
*/
private void drawGrid(Graphics g)
{
g.setColor(gridColor);
//draw the y-divisions
for (int i = 0; i * deltaY < getHeight(); i++)
g.drawLine(0, yOffset - i * deltaY, getWidth(), yOffset - i * deltaY);
//draw the x-divisions
for (int i = 0; i * deltaX < getWidth() - deltaX; i++)
g.drawLine(i * deltaX, 0, i * deltaX, yOffset);
}
/**
* Draws the informations
*
* @param g
*/
private void drawInfo(Graphics g)
{
//Draw graph information
g.setColor(graphColor);
g.drawString(graphName + ": " + traineeName, FRAME_SIZE, 20);
g.drawString("10 sec./div", getWidth() - 80, getHeight()- 3);
g.drawString("Time elapsed: " + elapsedTime + " sec.", getWidth() - 200, 20);
}
/**
* Implements the method defined in the super class and draws the complete graph
*
* @param g
*/
protected void draw(Graphics g)
{
drawGrid(g);
g.setColor(graphColor);
int i = 0;
for (Iterator iter = graphObjList.iterator(); iter.hasNext(); i++)
drawObject((BikeData)iter.next(), g, startPoint, (i > 0) ? deltaX : 0, yOffset);
drawInfo(g);
}
/**
* Implements the method defined in the ModelObserver. When registered as listener in the model, new bike
* objects will be received in this method
*
* @param BikeData bikeObj
*/
public void bikeObjectAdded(BikeData bikeObj)
{
int modelSize = model.getModelSize(); //get number of bike objects in the model
elapsedTime = 10*modelSize; //set time elapsed - 10 sec. between each bike object
graphObjList.addLast(bikeObj); //add bike objects to the graph list
if (graphObjList.size() > getWidth()/deltaX)
{ //graph display is full
graphObjList.removeFirst(); //removed the bike object displayed leftmost in the graph
BikeData bikeData = (BikeData)graphObjList.getFirst();
yStart = get_Y_Value(bikeData); //set the new y start value
}
System.out.println(bikeObj);
repaint(); //invoke paintComponent() will redraw the graph
}
/**
* objectRemoved
*
* @param BikeData bikeObj
*/
public void bikeObjectRemoved(BikeData bikeObj)
{
}
/**
* Shows historical bike data for the whole training session
*/
public void viewSession()
{
model.removeListener(this); //Deregister as listener
scrollBar = new JScrollBar(0); //Make scroll bar horizontal
scrollBar.setMaximum(model.getModelSize()); //set scroll bar max. value equal number of bike objects in the model
scrollBar.setValue(model.getModelSize());
elapsedTime = 10*model.getModelSize();
scrollBar.addAdjustmentListener(new ScrollBarAdjustmentListener());
this.add(scrollBar, BorderLayout.SOUTH);
}
private class ScrollBarAdjustmentListener implements AdjustmentListener
{
public void adjustmentValueChanged(AdjustmentEvent e)
{
//System.out.println("adjustmentValueChanged() called with value: " + e.getValue() + " type: " + e.getAdjustmentType());
int modelSize = model.getModelSize();
graphObjList.clear(); //removed all bike objects from the graph list
//get a new graph list matching the current value of the scroll bar
//model.getModelSection(graphObjList, e.getValue(), NUMBER_OF_X_INCREMENTS);
BikeData bikeData = (BikeData)graphObjList.getFirst();
elapsedTime = 10*e.getValue();
repaint(); //invoke paintComponent() will redraw the graph
}
}
}
package bikeappl;
import java.awt.*;
/**
* <p>Title: OOP2A Bike Monitor</p>
*
* <p>Description: This is the concrete class for showning the pulse graph</p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* @version 1.0
*/
public class PulseGraph extends BikeDataGraph
{
/**
* Constructor
*
* @param model a refence to the objects model
* @param traineeName the name of the person training this session
*/
public PulseGraph(Model model, Color graphColor, String traineeName)
{
super(model, "Pulse graph", traineeName, graphColor, Color.BLACK);
}
/**
* The concrete implementation drawing a bike object in the graph
*
* @param bikeObj the bike object to be drawn
* @param g the graphics for this window
* @param startPoint the starting point for the drawning, this point is updated after drawning
* @param deltaX the x increment
* @param yOffset the offset at the y-axis
*/
protected void drawObject(BikeData bikeObj, Graphics g, Point startPoint, int dx, int yOffset)
{
bikeObj.drawPulse(startPoint, dx, yOffset, g);
}
/**
* The concrete implementation returning the bike objects actual y-value
*
* @param bikeObj the bike data object
*/
protected int get_Y_Value(BikeData bikeObj)
{
return bikeObj.getPulse();
}
}