Hej folkens !! Jeg har en GUI og ønsker en graf, hvor man kan vise nogle dataer. Nogen der kan hjælpe mig dette. Tænkte på at lave grafen en koordinatsystem, hvor dataerne vises som søjler.
Det ser fint ud, men ønsker først og fremmest bare at lave en graf på min GUI. Er nemlig igang med eksamensprojekt, så ved ik helt om det vil blive accepteret at bruge JFreeChart.
Ja det så rigtigt :) Men er næsten 100% sikker på det ik vil godkendes. Men kunne du hjælpe mig med at lave et simpelt kordinatsystem, med x og y akser??
/** * <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) {
/** * 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
/** * 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);
/** * 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 }
/** * 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(); }
Det var noget af en doktorafhandling du har postet, skal nok kigge på det, men tør ikke love det bliver i aften.
Har temmelig meget at rive i pt.
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.