jmeanshift.ChartProof.java Source code

Java tutorial

Introduction

Here is the source code for jmeanshift.ChartProof.java

Source

/*
 * Copyright(c) 2010-2011, Diego Schmaedech Martins (UFSM, Federal University of Santa Maria, Brazil).
 *
 *
 * All rights reserved.
 *
 * COMMERCIAL USE:
 * This library is part of Mothorus Eye Tracker developed under
 *                  GNU LESSER GENERAL PUBLIC LICENSE
 *                   Version 3, 29 June 2007 License
 * If you have any commercial interest in this work please contact schmadech@gmail.com
 *
 *
    
    
 IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
    
 By downloading, copying, installing or using the software you agree to this license.
 If you do not agree to this license, do not download, install, copy or use the software.
    
    
 Intel License Agreement
 For Open Source Computer Vision Library
    
 Copyright (C) 2000, Intel Corporation, all rights reserved.
 Third party copyrights are property of their respective owners.
    
 Redistribution and use in source and binary forms, with or without modification,
 are permitted provided that the following conditions are met:
    
 * Redistribution's of source code must retain the above copyright notice,
 this list of conditions and the following disclaimer.
    
 * Redistribution's in binary form must reproduce the above copyright notice,
 this list of conditions and the following disclaimer in the documentation
 and/or other materials provided with the distribution.
    
 * The name of Intel Corporation may not be used to endorse or promote products
 derived from this software without specific prior written permission.
    
 This software is provided by the copyright holders and contributors "as is" and
 any express or implied warranties, including, but not limited to, the implied
 warranties of merchantability and fitness for a particular purpose are disclaimed.
 In no event shall the Intel Corporation or contributors be liable for any direct,
 indirect, incidental, special, exemplary, or consequential damages
 (including, but not limited to, procurement of substitute goods or services;
 loss of use, data, or profits; or business interruption) however caused
 and on any theory of liability, whether in contract, strict liability,
 or tort (including negligence or otherwise) arising in any way out of
 the use of this software, even if advised of the possibility of such damage.
 *
 */
package jmeanshift;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.GridLayout;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Vector;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * Created on Oct 27, 2010, 11:43:06 PM
 *
 * @author Diego Schmaedech
 */
public class ChartProof extends javax.swing.JPanel implements ChartMouseListener {

    private static Vector<Double> data1 = new Vector<Double>();
    private static Vector<Double> data2 = new Vector<Double>();
    private static final String TITLE = "";
    private XYSeries series = new XYSeries("classes");
    private ChartPanel chartPanel;
    private XYPlot plot;
    XYSeriesCollection dataset = new XYSeriesCollection(series);
    NumberAxis domainAxis;
    NumberAxis rangeAxis;

    /**
     * Creates new form ChartProof
     */
    public ChartProof() {
        initComponents();
        bubblyBubblesScatter(jPanel1);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jcbRandomMode = new javax.swing.JCheckBox();
        jcbMergeClusters = new javax.swing.JCheckBox();

        jPanel1.setBackground(new java.awt.Color(254, 254, 254));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 778, Short.MAX_VALUE));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 269, Short.MAX_VALUE));

        jToolBar1.setRollover(true);

        jLabel1.setText("Criteria");
        jToolBar1.add(jLabel1);

        jTextField1.setColumns(5);
        jTextField1.setText("0.002");
        jToolBar1.add(jTextField1);

        jLabel2.setText("Bandwidth");
        jToolBar1.add(jLabel2);

        jTextField2.setColumns(5);
        jTextField2.setText("2");
        jToolBar1.add(jTextField2);

        jButton1.setText("Do Mean-Shift");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        jButton2.setText("New Points");
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);

        jButton3.setText("Reset Axis");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton3);

        jcbRandomMode.setText("Random Mode");
        jcbRandomMode.setFocusable(false);
        jcbRandomMode.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jcbRandomMode);

        jcbMergeClusters.setText("Merge Clusters");
        jcbMergeClusters.setFocusable(false);
        jcbMergeClusters.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jcbMergeClusters.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jcbMergeClusters);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 778, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jToolBar1,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 25,
                                javax.swing.GroupLayout.PREFERRED_SIZE)));
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        dataset.removeAllSeries();
        Vector<Vector> vdataset = new Vector<Vector>();
        vdataset.add(data1);
        vdataset.add(data2);
        // dataset.add(data3);
        //dataset.add(data4);
        System.out.print("[");
        for (int k = 0; k < data1.size(); k++) {
            System.out.print(" " + data1.get(k));
        }
        System.out.print(" ]");
        System.out.print("\n");
        System.out.print("[");
        for (int k = 0; k < data1.size(); k++) {
            System.out.print(" " + data2.get(k));
        }
        System.out.print(" ]");
        System.out.print("\n");
        MeanShift domean = new MeanShift();
        double criteria = Double.parseDouble(jTextField1.getText().trim());
        double band = Double.parseDouble(jTextField2.getText().trim());
        domean.setMergeClusters(jcbMergeClusters.isSelected());
        domean.doMeanShift(vdataset, band, criteria, jcbRandomMode.isSelected());

        XYSeries[] clusters = new XYSeries[domean.getkClusters()];
        XYSeries[] clustersCenter = new XYSeries[domean.getkClusters()];

        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();

        //varre o numero de cluster
        for (int y = 0; y < domean.getkClusters(); y++) {

            clustersCenter[y] = new XYSeries("center " + y);
            dataset.addSeries(clustersCenter[y]);

            clusters[y] = new XYSeries("cluster " + y);
            dataset.addSeries(clusters[y]);

            double[] cc = domean.clusterCenter.get(y);
            clustersCenter[y].add(cc[0], cc[1]);
            renderer.setSeriesLinesVisible(dataset.getSeriesCount() - 2, Boolean.FALSE);
            renderer.setSeriesLinesVisible(dataset.getSeriesCount() - 1, Boolean.FALSE);
            renderer.setSeriesShape(dataset.getSeriesCount() - 2, new Rectangle2D.Double(-3.0, -3.0, 6.0, 6.0));
            renderer.setSeriesShape(dataset.getSeriesCount() - 1, new Ellipse2D.Double(-2.0, -2.0, 3.0, 3.0));
            renderer.setSeriesShapesFilled(dataset.getSeriesCount() - 2, Boolean.TRUE);
            renderer.setSeriesShapesFilled(dataset.getSeriesCount() - 1, Boolean.TRUE);

            for (int k = 0; k < domean.clustersMembers.size(); k++) {
                if (y == domean.clustersMembers.get(k)) {
                    clusters[y].add(data1.get(k), data2.get(k));
                    //System.out.println("y: " + y + " x1: " +series.getX(k)+ " x2: "+series.getY(k));
                }
            }

        }
        plot.setRenderer(renderer);

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        data1.removeAllElements();
        data2.removeAllElements();
        series.clear();
        dataset.removeAllSeries();
        dataset.addSeries(series);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        domainAxis.setLowerBound(0);
        domainAxis.setUpperBound(255);

        rangeAxis.setLowerBound(0);
        rangeAxis.setUpperBound(255);
    }//GEN-LAST:event_jButton3ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JCheckBox jcbMergeClusters;
    private javax.swing.JCheckBox jcbRandomMode;
    // End of variables declaration//GEN-END:variables

    private void bubblyBubblesScatter(JPanel panel) {

        JFreeChart chart = ChartFactory.createScatterPlot(TITLE, "X1", "X2", dataset, PlotOrientation.VERTICAL,
                true, true, false);

        chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.yellow));

        plot = chart.getXYPlot();

        plot.setBackgroundPaint(Color.WHITE);
        plot.setDomainGridlinePaint(Color.BLUE);
        plot.setRangeGridlinePaint(Color.CYAN);
        plot.setForegroundAlpha(0.65f);

        domainAxis = (NumberAxis) plot.getDomainAxis();
        domainAxis.setLowerBound(0);
        domainAxis.setUpperBound(255);
        domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis = (NumberAxis) plot.getRangeAxis();

        rangeAxis.setLowerBound(0);
        rangeAxis.setUpperBound(255);
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        chartPanel = new ChartPanel(chart);
        chartPanel.addChartMouseListener(this);
        panel.setLayout(new GridLayout(0, 1));
        panel.add(chartPanel);

    }

    @Override
    public void chartMouseClicked(ChartMouseEvent cme) {
        ChartEntity chartentity = cme.getEntity();
        if (chartentity != null) {
            Point2D p = chartPanel.translateScreenToJava2D(cme.getTrigger().getPoint());
            final Rectangle2D plotArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
            int i = (int) plot.getDomainAxis().java2DToValue(p.getX(), plotArea, plot.getDomainAxisEdge());
            int j = (int) plot.getRangeAxis().java2DToValue(p.getY(), plotArea, plot.getRangeAxisEdge());
            series.addOrUpdate(i, j);
            data1.add((double) i);
            data2.add((double) j);
        } else {

        }

    }

    @Override
    public void chartMouseMoved(ChartMouseEvent cme) {

    }

}