Java tutorial
/** * Hub Miner: a hubness-aware machine learning experimentation library. * Copyright (C) 2014 Nenad Tomasev. Email: nenad.tomasev at gmail.com * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/>. */ package gui.images; import draw.basic.ColorPalette; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Paint; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.chart.renderer.category.CategoryItemRenderer; import org.jfree.data.category.DefaultCategoryDataset; /** * This panel represents lists of hubs, good hubs and bad hubs. It also * represents the point type distribution in a separate char. * * @author Nenad Tomasev <nenad.tomasev at gmail.com> */ public class ClassHubsPanel extends javax.swing.JPanel { /** * @return JPanel showing the major hubs in the class. */ public JPanel getHubsPanel() { return hubsPanel; } /** * @return JPanel showing the major good hubs in the class. */ public JPanel getGoodHubsPanel() { return goodHubsPanel; } /** * @return JPanel showing the major bad hubs in the class. */ public JPanel getBadHubsPanel() { return badHubsPanel; } /** * Bar rendered for the chart. */ class CustomRenderer extends BarRenderer { // Color array for different point types. private Paint[] colors; public CustomRenderer() { ColorPalette cp = new ColorPalette(); this.colors = new Paint[] { cp.DARK_OLIVE_GREEN, cp.OLD_GOLD, cp.COPPER, cp.FIREBRICK_RED }; } @Override public Paint getItemPaint(final int row, final int column) { return (this.colors[column % this.colors.length]); } } /** * Sets the distribution of point types to be shown in the column chart. * * @param pTypes Float array representing the point type distribution. */ public void setPointTypeDistribution(float[] pTypes) { DefaultCategoryDataset hDistDataset = new DefaultCategoryDataset(); hDistDataset.addValue(pTypes[0], "Examples", "safe"); hDistDataset.addValue(pTypes[1], "Examples", "borderline"); hDistDataset.addValue(pTypes[2], "Examples", "rare"); hDistDataset.addValue(pTypes[3], "Examples", "outlier"); JFreeChart chart = ChartFactory.createBarChart("Point Type Distribution", "", "", hDistDataset, PlotOrientation.VERTICAL, false, true, false); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new Dimension(316, 240)); final CategoryPlot plot = chart.getCategoryPlot(); CategoryItemRenderer renderer = new CustomRenderer(); plot.setRenderer(renderer); pointTypePanel.removeAll(); pointTypePanel.add(chartPanel); pointTypePanel.revalidate(); pointTypePanel.repaint(); } /** * Creates new form ClassHubsPanel */ public ClassHubsPanel(Color classColor, String className) { initComponents(); hubsPanel.setLayout(new FlowLayout()); badHubsPanel.setLayout(new FlowLayout()); goodHubsPanel.setLayout(new FlowLayout()); this.setBackground(classColor); this.classNameLabel.setText(className); classNameLabel.repaint(); pointTypePanel.setLayout(new FlowLayout()); pointTypePanel.setPreferredSize(new Dimension(318, 262)); pointTypePanel.setMaximumSize(new Dimension(318, 262)); pointTypePanel.setMinimumSize(new Dimension(318, 262)); } /** * 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() { pointTypePanel = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); badHubsScrollPane = new javax.swing.JScrollPane(); badHubsPanel = new javax.swing.JPanel(); goodHubsScrollPane = new javax.swing.JScrollPane(); goodHubsPanel = new javax.swing.JPanel(); hubsScrollPane = new javax.swing.JScrollPane(); hubsPanel = new javax.swing.JPanel(); classNameLabel = new javax.swing.JLabel(); javax.swing.GroupLayout pointTypePanelLayout = new javax.swing.GroupLayout(pointTypePanel); pointTypePanel.setLayout(pointTypePanelLayout); pointTypePanelLayout.setHorizontalGroup(pointTypePanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 378, Short.MAX_VALUE)); pointTypePanelLayout.setVerticalGroup(pointTypePanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 262, Short.MAX_VALUE)); jLabel1.setText("Bad Hubs:"); jLabel2.setText("Good Hubs:"); jLabel3.setText("Hubs:"); badHubsScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); javax.swing.GroupLayout badHubsPanelLayout = new javax.swing.GroupLayout(badHubsPanel); badHubsPanel.setLayout(badHubsPanelLayout); badHubsPanelLayout.setHorizontalGroup(badHubsPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 335, Short.MAX_VALUE)); badHubsPanelLayout.setVerticalGroup(badHubsPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 100, Short.MAX_VALUE)); badHubsScrollPane.setViewportView(badHubsPanel); goodHubsScrollPane .setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); javax.swing.GroupLayout goodHubsPanelLayout = new javax.swing.GroupLayout(goodHubsPanel); goodHubsPanel.setLayout(goodHubsPanelLayout); goodHubsPanelLayout.setHorizontalGroup(goodHubsPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 335, Short.MAX_VALUE)); goodHubsPanelLayout.setVerticalGroup(goodHubsPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 100, Short.MAX_VALUE)); goodHubsScrollPane.setViewportView(goodHubsPanel); hubsScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); javax.swing.GroupLayout hubsPanelLayout = new javax.swing.GroupLayout(hubsPanel); hubsPanel.setLayout(hubsPanelLayout); hubsPanelLayout.setHorizontalGroup(hubsPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 335, Short.MAX_VALUE)); hubsPanelLayout.setVerticalGroup(hubsPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 100, Short.MAX_VALUE)); hubsScrollPane.setViewportView(hubsPanel); classNameLabel.setText("class"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(classNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(pointTypePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(2, 2, 2) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(goodHubsScrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE) .addComponent(badHubsScrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) .addComponent(hubsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(badHubsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE) .addComponent(goodHubsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(hubsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))) .addGroup(layout.createSequentialGroup().addComponent(classNameLabel).addGap(20, 20, 20) .addComponent(pointTypePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap())); }// </editor-fold>//GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel badHubsPanel; private javax.swing.JScrollPane badHubsScrollPane; private javax.swing.JLabel classNameLabel; private javax.swing.JPanel goodHubsPanel; private javax.swing.JScrollPane goodHubsScrollPane; private javax.swing.JPanel hubsPanel; private javax.swing.JScrollPane hubsScrollPane; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JPanel pointTypePanel; // End of variables declaration//GEN-END:variables }