001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.gui.plot;
025    
026    import java.awt.Graphics;
027    import java.awt.Graphics2D;
028    
029    public class Traces {
030    
031            private PlotSettings plotSettings = null;
032    
033            public Traces(PlotSettings plotSettings) {
034                    this.plotSettings = plotSettings;
035            }
036    
037            public void paintComponent(Graphics g) {
038                    try {
039                            long t0 = System.currentTimeMillis();
040    
041                            Graphics2D g2d = (Graphics2D) g;
042    
043                            double xf = plotSettings.getXFactor();
044                            double yf = plotSettings.getYFactor();
045    
046                            for (int t = 0; t < Math.min(10, plotSettings.getMatrixGUIObject().getColumnCount()); t++) {
047    
048                                    if (plotSettings.isShowTrace(t)) {
049    
050                                            long column = t;
051    
052                                            g2d.setStroke(plotSettings.getTraceStroke(t));
053                                            g2d.setColor(plotSettings.getTraceColor(t));
054    
055                                            double xs = plotSettings.getXStepSize();
056    
057                                            long dots = 0;
058                                            for (double xr = plotSettings.getMinXValue() + xs; xr <= plotSettings.getMaxXValue(); xr += xs) {
059                                                    dots++;
060                                                    long row1 = (long) (xr - xs);
061                                                    long row2 = (long) xr;
062    
063                                                    double yv1 = plotSettings.getMatrixGUIObject().getDoubleValueAt(row1, column);
064                                                    double yv2 = plotSettings.getMatrixGUIObject().getDoubleValueAt(row2, column);
065    
066                                                    int x1 = (int) ((xr - xs) * xf);
067                                                    int x2 = (int) (xr * xf);
068                                                    x2 = (x2 == x1) ? x2++ : x2;
069    
070                                                    int y1 = (int) (plotSettings.getHeight() - 1 - yv1 * yf + plotSettings.getMinYValue() * yf);
071                                                    int y2 = (int) (plotSettings.getHeight() - 1 - yv2 * yf + plotSettings.getMinYValue() * yf);
072    
073                                                    g2d.drawLine(x1, y1, x2, y2);
074                                            }
075    
076                                    }
077    
078                                    long t1 = System.currentTimeMillis();
079                                    if (t1 - t0 > plotSettings.getTimeLimit()) {
080                                            return;
081                                    }
082    
083                            }
084                    } catch (Exception e) {
085                            e.printStackTrace();
086                    }
087            }
088    
089    }