Package com.nexirius.framework.dataviewer

Source Code of com.nexirius.framework.dataviewer.TrendViewer$ImageCanvas

//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.dataviewer;

import com.nexirius.framework.datamodel.DataModelEvent;
import com.nexirius.framework.datamodel.TrendModel;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.awt.*;

/**
* This viewer is designed to show TrendModel data
*
* @author Marcel Baumann
*/

public class TrendViewer extends DataViewer {



    public static final String s_viewername = "TrendViewer";
    public static final int STYLE_STEPS = 0;
    public static final int STYLE_LINE = 1;

    protected ImageCanvas imageCanvas = null;
    protected boolean autoFit = true;
    protected boolean resizePanel = false;
    private int style = STYLE_STEPS;

    private Color color[] = {Color.blue, Color.green, Color.magenta, Color.orange, Color.pink, Color.red, Color.yellow};

    /**
     * @param model The TrendModel
     */
    public TrendViewer(TrendModel model) {
        super(model);
    }

    public TrendModel getModel() {
        return (TrendModel) getDataModel();
    }

    public JPanel getJPanel() {
        return (JPanel) getJComponent();
    }

    public void create() {
        JPanel panel = new JPanel(new BorderLayout());

        panel.setBorder(new EtchedBorder());
        setJComponent(panel);
        imageCanvas = new ImageCanvas();
        panel.add(BorderLayout.CENTER, imageCanvas);
        imageCanvas.setPreferredSize(new Dimension(100, 100));
        update();
    }

    public void dataModelChangeValue(DataModelEvent event) {
        handleInvisibleFlag(event);
        update();
    }

    public void update() {
        if (isCreated()) {
            imageCanvas.update();
        }
    }

    public String getViewerName() {
        return s_viewername;
    }

    public void grabFocus() {
    }

    public Color getColor(int n) {
        return color[n % color.length];
    }

    class ImageCanvas extends JPanel {
        ImageCanvas() {
            setOpaque(true);
        }

        void update() {
            paint(getGraphics());
        }

        public void paint(Graphics g) {
            super.paint(g);

            if (g == null) {
                return;
            }

            Dimension size = getSize();

            g.setColor(Color.white);

            g.fillRect(0
                    , 0
                    , size.width
                    , size.height);

            g.setColor(Color.gray);

            int frame = 2;
            int width = size.width - 2 * frame;
            int height = size.height - 2 * frame;

            g.drawRect(frame
                    , frame
                    , width
                    , height);

            for (int i = 0; i < getModel().getMaxTrends(); ++i) {
                try {
                    paintTrend(i, g, frame);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }

        private void paintTrend(int number, Graphics g, int frame) {
            int width = getSize().width - 2 * frame;
            int height = getSize().height - 2 * frame;
            double v[] = getModel().getNormalized(number);
            int npoints = v.length;

            if (npoints < 2) {
                return;
            }

            int x[] = null;
            int y[] = null;
            double xstep = (double) width / (double) (npoints - 1);

            switch (style) {
                case STYLE_LINE:
                    x = new int[npoints];
                    y = new int[npoints];

                    for (int i = 0; i < npoints; ++i) {
                        x[i] = frame + (int) (i * xstep);
                        y[i] = getSize().height - frame - (int) (v[i] * height);
                    }

                case STYLE_STEPS:
                    x = new int[2 * npoints];
                    y = new int[2 * npoints];

                    int ybefore = getSize().height - frame - (int) (v[0] * height);

                    for (int i = 0; i < npoints; ++i) {
                        x[2 * i] = frame + (int) (i * xstep);
                        y[2 * i] = ybefore;
                        x[2 * i + 1] = x[2 * i];
                        y[2 * i + 1] = getSize().height - frame - (int) (v[i] * height);
                        ybefore = y[2 * i + 1];
                    }
            }

            g.setColor(getColor(number));
            g.drawPolyline(x, y, x.length);
        }
    }
}
TOP

Related Classes of com.nexirius.framework.dataviewer.TrendViewer$ImageCanvas

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.