Java tutorial
/******************************************************************************* * Copyright (c) 2009, 2011 The University of Memphis. All rights reserved. * This program and the accompanying materials are made available * under the terms of the LIDA Software Framework Non-Commercial License v1.0 * which accompanies this distribution, and is available at * http://ccrg.cs.memphis.edu/assets/papers/2010/LIDA-framework-non-commercial-v1.0.pdf *******************************************************************************/ package edu.memphis.ccrg.lida.framework.gui.panels; import java.awt.Color; import java.util.logging.Level; import java.util.logging.Logger; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import edu.memphis.ccrg.lida.framework.FrameworkModule; import edu.memphis.ccrg.lida.framework.ModuleName; import edu.memphis.ccrg.lida.framework.shared.activation.Learnable; import edu.memphis.ccrg.lida.framework.tasks.TaskManager; import edu.memphis.ccrg.lida.pam.PerceptualAssociativeMemory; /** * A {@link GuiPanel} which displays a {@link Learnable} element's activation over time (in ticks). * @author Ryan J. McCall */ public class ActivationChartPanel extends GuiPanelImpl { private static final Logger logger = Logger.getLogger(ActivationChartPanel.class.getCanonicalName()); private JFreeChart chart; private XYSeriesCollection dataset = new XYSeriesCollection(); private XYSeries series1 = new XYSeries("Base-level Activation"); private XYSeries series2 = new XYSeries("Current Activation"); private XYSeries series3 = new XYSeries("Total Activation"); private int tickDisplayInterval = 100; private FrameworkModule selectedModule; private Learnable learnable; private String elementName = ""; /** Creates new form JChartGuiPanel */ public ActivationChartPanel() { chart = ChartFactory.createXYLineChart("", "Tick", "Activation", dataset, PlotOrientation.VERTICAL, true, true, false); chart.setBackgroundPaint(new Color(238, 233, 233)); XYPlot plot = (XYPlot) chart.getPlot(); plot.setBackgroundPaint(Color.WHITE); plot.setDomainGridlinePaint(Color.LIGHT_GRAY); plot.setRangeGridlinePaint(Color.LIGHT_GRAY); NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis(); domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); initComponents(); } /** 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. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { toolBar = new javax.swing.JToolBar(); jLabel1 = new javax.swing.JLabel(); moduleComboBox = new javax.swing.JComboBox(); jSeparator1 = new javax.swing.JToolBar.Separator(); jLabel2 = new javax.swing.JLabel(); elementNameTextField = new javax.swing.JTextField(); displayButton = new javax.swing.JButton(); mainPanel = new ChartPanel(chart); toolBar.setRollover(true); jLabel1.setText("Module "); toolBar.add(jLabel1); moduleComboBox.setPreferredSize(new java.awt.Dimension(200, 22)); moduleComboBox.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent evt) { moduleComboBoxActionPerformed(evt); } }); toolBar.add(moduleComboBox); toolBar.add(jSeparator1); jLabel2.setText(" Element Name "); toolBar.add(jLabel2); elementNameTextField.setMaximumSize(new java.awt.Dimension(100, 22)); elementNameTextField.setPreferredSize(new java.awt.Dimension(90, 22)); toolBar.add(elementNameTextField); displayButton.setFont(new java.awt.Font("Tahoma", 1, 11)); displayButton.setText("Plot Activation"); displayButton.setFocusable(false); displayButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); displayButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); displayButton.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent evt) { displayButtonActionPerformed(evt); } }); toolBar.add(displayButton); javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel); mainPanel.setLayout(mainPanelLayout); mainPanelLayout.setHorizontalGroup(mainPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 501, Short.MAX_VALUE)); mainPanelLayout.setVerticalGroup(mainPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 267, Short.MAX_VALUE)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(toolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent( mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(toolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(272, Short.MAX_VALUE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(33, 33, 33).addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))); }// </editor-fold>//GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton displayButton; private javax.swing.JTextField elementNameTextField; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JToolBar.Separator jSeparator1; private javax.swing.JPanel mainPanel; private javax.swing.JComboBox moduleComboBox; private javax.swing.JToolBar toolBar; // End of variables declaration//GEN-END:variables private void moduleComboBoxActionPerformed(java.awt.event.ActionEvent evt) { String selection = moduleComboBox.getSelectedItem().toString(); FrameworkModule newModuleSelection = agent.getSubmodule(selection); if (newModuleSelection != null) { selectedModule = newModuleSelection; refresh(); } else { logger.log(Level.WARNING, "cannot retriveve module with name {1}", new Object[] { TaskManager.getCurrentTick(), selection }); } } private void displayButtonActionPerformed(java.awt.event.ActionEvent evt) { elementName = elementNameTextField.getText().trim(); refresh(); } /** * first optional parameter sets tickDisplayInterval (int) * */ @Override public void initPanel(String[] params) { //TODO additional module support: ProceduralMemory, ActionSelection?, CodeletModules selectedModule = agent.getSubmodule(ModuleName.PerceptualAssociativeMemory); moduleComboBox.addItem(selectedModule); //order affects color of series, TODO find out how to choose colors dataset.addSeries(series3); dataset.addSeries(series2); dataset.addSeries(series1); if (params != null && params.length > 0) { try { tickDisplayInterval = Integer.parseInt(params[0].trim()); } catch (NumberFormatException e) { logger.log(Level.WARNING, "unable to set tick display interval {0}", params[0].trim()); } if (params.length > 1) { elementName = params[1].trim(); elementNameTextField.setText(elementName); refresh(); } } } @Override public void refresh() { ModuleName selectedModuleName = selectedModule.getModuleName(); if (selectedModuleName == ModuleName.PerceptualAssociativeMemory) { learnable = (Learnable) ((PerceptualAssociativeMemory) selectedModule).getNode(elementName); } else { learnable = null; } updateWithLearnable(learnable); } private void updateWithLearnable(Learnable learnable) { if (learnable != null) { //Remove oldest if display interval is reached if (series2.getItemCount() >= tickDisplayInterval) { series1.remove(0); series2.remove(0); series3.remove(0); } //add a new x,y entry to each of the 3 series long currentTick = TaskManager.getCurrentTick(); series1.add(currentTick, learnable.getBaseLevelActivation()); series2.add(currentTick, learnable.getActivation()); series3.add(currentTick, learnable.getTotalActivation()); } else { logger.log(Level.FINEST, "null learnable", TaskManager.getCurrentTick()); } } }