Improved the plot function: it is now easier to change the plot from linear to logarithmic scale and the other way arround.

This commit is contained in:
Andreas Dräger 2010-04-12 08:11:46 +00:00
parent 82686fd280
commit a804380fe0
2 changed files with 191 additions and 131 deletions

View File

@ -296,7 +296,11 @@ public class FunctionArea extends DArea implements Serializable {
updateLegend(); updateLegend();
} }
protected boolean checkLoggable() { /**
*
* @return
*/
public boolean checkLoggable() {
double minY = Double.MAX_VALUE; double minY = Double.MAX_VALUE;
for (int i = 0; i < m_PointSetContainer.size(); i++) { for (int i = 0; i < m_PointSetContainer.size(); i++) {
DPointSet pSet = (m_PointSetContainer.get(i).getConnectedPointSet()); DPointSet pSet = (m_PointSetContainer.get(i).getConnectedPointSet());
@ -975,6 +979,18 @@ public class FunctionArea extends DArea implements Serializable {
repaint(); repaint();
} }
/**
* Allows setting whether or not to paint the y-axis in logarithmic scale.
*
* @param log
* if true logarithmic scale is used, linear scale in case of
* false.
*/
public void toggleLog(boolean log) {
if (log != m_log)
toggleLog();
}
/** /**
* Recreate the legend object with the current point sets. * Recreate the legend object with the current point sets.
* *

View File

@ -1,4 +1,5 @@
package eva2.gui; package eva2.gui;
/* /*
* Title: EvA2 * Title: EvA2
* Description: * Description:
@ -45,6 +46,7 @@ import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.populations.Population; import eva2.server.go.populations.Population;
import eva2.tools.BasicResourceLoader; import eva2.tools.BasicResourceLoader;
import eva2.tools.chart2d.DPointSet; import eva2.tools.chart2d.DPointSet;
/*==========================================================================* /*==========================================================================*
* CLASS DECLARATION * CLASS DECLARATION
*==========================================================================*/ *==========================================================================*/
@ -53,6 +55,10 @@ import eva2.tools.chart2d.DPointSet;
*/ */
public class Plot implements PlotInterface, Serializable { public class Plot implements PlotInterface, Serializable {
/**
* Generated serial version identifier.
*/
private static final long serialVersionUID = -9027101244918249825L;
public static boolean TRACE = false; public static boolean TRACE = false;
private JFileChooser m_FileChooser; private JFileChooser m_FileChooser;
private JPanel m_ButtonPanel; private JPanel m_ButtonPanel;
@ -63,10 +69,13 @@ public class Plot implements PlotInterface, Serializable {
protected JFrame m_Frame; protected JFrame m_Frame;
/** /**
* You might want to try to assign the x-range as x and y-range as y array parameters. * You might want to try to assign the x-range as x and y-range as y array
* parameters.
*/ */
public Plot(String PlotName,String xname,String yname,double[] x,double[] y) { public Plot(String PlotName, String xname, String yname, double[] x,
if (TRACE) System.out.println("Constructor Plot "+PlotName); double[] y) {
if (TRACE)
System.out.println("Constructor Plot " + PlotName);
m_xname = xname; m_xname = xname;
m_yname = yname; m_yname = yname;
m_PlotName = PlotName; m_PlotName = PlotName;
@ -77,22 +86,26 @@ public class Plot implements PlotInterface, Serializable {
} }
m_PlotArea.addDElement(points); m_PlotArea.addDElement(points);
} }
/** /**
* *
*/ */
public Plot(String PlotName, String xname, String yname, boolean init) { public Plot(String PlotName, String xname, String yname, boolean init) {
if (TRACE) System.out.println("Constructor Plot "+PlotName); if (TRACE)
System.out.println("Constructor Plot " + PlotName);
m_xname = xname; m_xname = xname;
m_yname = yname; m_yname = yname;
m_PlotName = PlotName; m_PlotName = PlotName;
if (init) if (init)
init(); init();
} }
/** /**
* *
*/ */
public Plot(String PlotName, String xname, String yname) { public Plot(String PlotName, String xname, String yname) {
if (TRACE) System.out.println("Constructor Plot "+PlotName); if (TRACE)
System.out.println("Constructor Plot " + PlotName);
m_xname = xname; m_xname = xname;
m_yname = yname; m_yname = yname;
m_PlotName = PlotName; m_PlotName = PlotName;
@ -107,14 +120,16 @@ public class Plot implements PlotInterface, Serializable {
} }
}); });
JButton LOGButton = new JButton("Log/Lin"); JButton LOGButton = new JButton("Log/Lin");
LOGButton.setToolTipText("Toggle between a linear and a log scale on the y-axis."); LOGButton
.setToolTipText("Toggle between a linear and a log scale on the y-axis.");
LOGButton.addActionListener(new ActionListener() { LOGButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
m_PlotArea.toggleLog(); m_PlotArea.toggleLog();
} }
}); });
JButton ExportButton = new JButton("Export..."); JButton ExportButton = new JButton("Export...");
ExportButton.setToolTipText("Exports the graph data to a simple ascii file."); ExportButton
.setToolTipText("Exports the graph data to a simple ascii file.");
ExportButton.addActionListener(new ActionListener() { ExportButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
exportPlot(); exportPlot();
@ -135,39 +150,54 @@ public class Plot implements PlotInterface, Serializable {
Robot robot = new Robot(); Robot robot = new Robot();
Rectangle area; Rectangle area;
area = m_Frame.getBounds(); area = m_Frame.getBounds();
BufferedImage bufferedImage = robot.createScreenCapture(area); BufferedImage bufferedImage = robot
.createScreenCapture(area);
JFileChooser fc = new JFileChooser(); JFileChooser fc = new JFileChooser();
if (fc.showSaveDialog(m_Frame) != JFileChooser.APPROVE_OPTION) return; if (fc.showSaveDialog(m_Frame) != JFileChooser.APPROVE_OPTION)
return;
// System.out.println("Name " + outfile); // System.out.println("Name " + outfile);
try { try {
/* Old version /*
FileOutputStream fos = new FileOutputStream(fc.getSelectedFile().getAbsolutePath()+".jpeg"); * Old version FileOutputStream fos = new
BufferedOutputStream bos = new BufferedOutputStream(fos); * FileOutputStream
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos); * (fc.getSelectedFile().getAbsolutePath()+".jpeg");
encoder.encode(bufferedImage); * BufferedOutputStream bos = new
bos.close();*/ * BufferedOutputStream(fos); JPEGImageEncoder encoder =
File file = new File(fc.getSelectedFile().getAbsolutePath()+".png"); * JPEGCodec.createJPEGEncoder(bos);
* encoder.encode(bufferedImage); bos.close();
*/
File file = new File(fc.getSelectedFile()
.getAbsolutePath()
+ ".png");
ImageIO.write(bufferedImage, "png", file); ImageIO.write(bufferedImage, "png", file);
/* JPEG version with javax.imageio /*
float compression = 0.8f; * JPEG version with javax.imageio float compression =
FileImageOutputStream out = new FileImageOutputStream(new File(fc.getSelectedFile().getAbsolutePath()+".jpeg")); * 0.8f; FileImageOutputStream out = new
ImageWriter encoder = (ImageWriter)ImageIO.getImageWritersByFormatName("JPEG").next(); * FileImageOutputStream(new
JPEGImageWriteParam param = new JPEGImageWriteParam(null); * File(fc.getSelectedFile().getAbsolutePath
* ()+".jpeg")); ImageWriter encoder =
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); * (ImageWriter)ImageIO
param.setCompressionQuality(compression); * .getImageWritersByFormatName("JPEG").next();
* JPEGImageWriteParam param = new
encoder.setOutput(out); * JPEGImageWriteParam(null);
encoder.write((IIOMetadata) null, new IIOImage(bufferedImage,null,null), param); *
*
out.close(); * param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT
* ); param.setCompressionQuality(compression);
*
* encoder.setOutput(out); encoder.write((IIOMetadata)
* null, new IIOImage(bufferedImage,null,null), param);
*
* out.close();
*/ */
} catch (Exception eee) { } catch (Exception eee) {
System.err.println("Error on exporting PNG: " + eee.getMessage()); System.err.println("Error on exporting PNG: "
+ eee.getMessage());
} }
} catch (AWTException ee) { } catch (AWTException ee) {
System.err.println("Error on creating PNG: " + ee.getMessage()); System.err.println("Error on creating PNG: "
+ ee.getMessage());
ee.printStackTrace(); ee.printStackTrace();
} }
} }
@ -182,17 +212,22 @@ public class Plot implements PlotInterface, Serializable {
// m_ButtonPanel.add(SaveButton); // m_ButtonPanel.add(SaveButton);
buttonPan.add(saveImageButton); buttonPan.add(saveImageButton);
} }
/** /**
* *
*/ */
public void init() { public void init() {
m_Frame = new JEFrame("Plot: " + m_PlotName); m_Frame = new JEFrame("Plot: " + m_PlotName);
BasicResourceLoader loader = BasicResourceLoader.instance(); BasicResourceLoader loader = BasicResourceLoader.instance();
byte[] bytes = loader.getBytesFromResourceLocation(EvAInfo.iconLocation); byte[] bytes = loader
.getBytesFromResourceLocation(EvAInfo.iconLocation);
try { try {
m_Frame.setIconImage(Toolkit.getDefaultToolkit().createImage(bytes)); m_Frame
.setIconImage(Toolkit.getDefaultToolkit()
.createImage(bytes));
} catch (java.lang.NullPointerException e) { } catch (java.lang.NullPointerException e) {
System.err.println("Could not find EvA2 icon, please move resources folder to working directory!"); System.err
.println("Could not find EvA2 icon, please move resources folder to working directory!");
} }
m_ButtonPanel = new JPanel(); m_ButtonPanel = new JPanel();
@ -203,7 +238,8 @@ public class Plot implements PlotInterface, Serializable {
// getContentPane().smultetLayout( new GridLayout(1, 4) ); // getContentPane().smultetLayout( new GridLayout(1, 4) );
m_Frame.getContentPane().add(m_ButtonPanel, "South"); m_Frame.getContentPane().add(m_ButtonPanel, "South");
m_Frame.getContentPane().add(m_PlotArea,"Center"); // north was not so nice m_Frame.getContentPane().add(m_PlotArea, "Center"); // north was not so
// nice
m_Frame.addWindowListener(new WindowAdapter() { m_Frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
m_PlotArea.clearAll(); // this was a memory leak m_PlotArea.clearAll(); // this was a memory leak
@ -216,8 +252,8 @@ public class Plot implements PlotInterface, Serializable {
} }
/** /**
* Draw a population to the Plot instance. Each individual is annotated with the * Draw a population to the Plot instance. Each individual is annotated with
* given prefix and its fitness. * the given prefix and its fitness.
* *
* @param prefix * @param prefix
* @param pop * @param pop
@ -229,15 +265,17 @@ public class Plot implements PlotInterface, Serializable {
} }
/** /**
* Draw an individual to the Plot instance. It is annotated with the * Draw an individual to the Plot instance. It is annotated with the given
* given prefix and its fitness. * prefix and its fitness.
* *
* @param prefix * @param prefix
* @param pop * @param pop
* @see FunctionArea.drawIcon * @see FunctionArea.drawIcon
*/ */
public void drawIndividual(int iconType, int graphID, String prefix, AbstractEAIndividual indy) { public void drawIndividual(int iconType, int graphID, String prefix,
getFunctionArea().drawIcon(iconType, prefix+" "+indy.getFitness(0), indy.getDoublePosition(), graphID); AbstractEAIndividual indy) {
getFunctionArea().drawIcon(iconType, prefix + " " + indy.getFitness(0),
indy.getDoublePosition(), graphID);
} }
public void setPreferredSize(Dimension prefSize) { public void setPreferredSize(Dimension prefSize) {
@ -260,7 +298,9 @@ public class Plot implements PlotInterface, Serializable {
* *
*/ */
public void setConnectedPoint(double x, double y, int func) { public void setConnectedPoint(double x, double y, int func) {
if (TRACE) System.out.println("size before is " + m_PlotArea.getPointCount(func)); if (TRACE)
System.out.println("size before is "
+ m_PlotArea.getPointCount(func));
m_PlotArea.setConnectedPoint(x, y, func); m_PlotArea.setConnectedPoint(x, y, func);
if (TRACE) { if (TRACE) {
System.out.println("added " + x + "/" + y + " to graph " + func); System.out.println("added " + x + "/" + y + " to graph " + func);
@ -271,12 +311,14 @@ public class Plot implements PlotInterface, Serializable {
public int getPointCount(int graphLabel) { public int getPointCount(int graphLabel) {
return m_PlotArea.getPointCount(graphLabel); return m_PlotArea.getPointCount(graphLabel);
} }
/** /**
* *
*/ */
public void addGraph(int g1, int g2, boolean forceAdd) { public void addGraph(int g1, int g2, boolean forceAdd) {
m_PlotArea.addGraph(g1, g2, forceAdd); m_PlotArea.addGraph(g1, g2, forceAdd);
} }
/** /**
* *
*/ */
@ -293,24 +335,28 @@ public class Plot implements PlotInterface, Serializable {
m_PlotArea.clearLegend(); m_PlotArea.clearLegend();
m_Frame.repaint(); m_Frame.repaint();
} }
/** /**
* *
*/ */
public void clearGraph(int GraphNumber) { public void clearGraph(int GraphNumber) {
m_PlotArea.clearGraph(GraphNumber); m_PlotArea.clearGraph(GraphNumber);
} }
/** /**
* *
*/ */
public void setInfoString(int GraphLabel, String Info, float stroke) { public void setInfoString(int GraphLabel, String Info, float stroke) {
m_PlotArea.setInfoString(GraphLabel, Info, stroke); m_PlotArea.setInfoString(GraphLabel, Info, stroke);
} }
/** /**
* *
*/ */
public void jump() { public void jump() {
m_PlotArea.jump(); m_PlotArea.jump();
} }
/** /**
*/ */
protected Object openObject() { protected Object openObject() {
@ -320,20 +366,19 @@ public class Plot implements PlotInterface, Serializable {
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION) {
File selected = m_FileChooser.getSelectedFile(); File selected = m_FileChooser.getSelectedFile();
try { try {
ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(selected))); ObjectInputStream oi = new ObjectInputStream(
new BufferedInputStream(new FileInputStream(selected)));
Object obj = oi.readObject(); Object obj = oi.readObject();
oi.close(); oi.close();
Class ClassType = Class.forName("FunctionArea"); Class<?> ClassType = Class.forName("FunctionArea");
if (!ClassType.isAssignableFrom(obj.getClass())) if (!ClassType.isAssignableFrom(obj.getClass()))
throw new Exception("Object not of type: " + ClassType.getName()); throw new Exception("Object not of type: "
+ ClassType.getName());
return obj; return obj;
} catch (Exception ex) { } catch (Exception ex) {
JOptionPane.showMessageDialog(m_Frame, JOptionPane.showMessageDialog(m_Frame, "Couldn't read object: "
"Couldn't read object: " + selected.getName() + "\n" + ex.getMessage(),
+ selected.getName() "Open object file", JOptionPane.ERROR_MESSAGE);
+ "\n" + ex.getMessage(),
"Open object file",
JOptionPane.ERROR_MESSAGE);
} }
} }
return null; return null;
@ -356,15 +401,15 @@ public class Plot implements PlotInterface, Serializable {
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION) {
File sFile = m_FileChooser.getSelectedFile(); File sFile = m_FileChooser.getSelectedFile();
if (sFile.exists()) { if (sFile.exists()) {
returnVal = JOptionPane.showConfirmDialog(m_Frame, "The file "+sFile.getName()+" already exists. Overwrite?"); returnVal = JOptionPane.showConfirmDialog(m_Frame, "The file "
if (returnVal != JOptionPane.YES_OPTION) return; + sFile.getName() + " already exists. Overwrite?");
if (returnVal != JOptionPane.YES_OPTION)
return;
} }
if (!(m_PlotArea.exportToAscii(sFile))) { if (!(m_PlotArea.exportToAscii(sFile))) {
JOptionPane.showMessageDialog(m_Frame, JOptionPane.showMessageDialog(m_Frame,
"Couldn't write to file: " "Couldn't write to file: " + sFile.getName(),
+ sFile.getName(), "Export error", JOptionPane.ERROR_MESSAGE);
"Export error",
JOptionPane.ERROR_MESSAGE);
} }
} }
} }
@ -379,15 +424,14 @@ public class Plot implements PlotInterface, Serializable {
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION) {
File sFile = m_FileChooser.getSelectedFile(); File sFile = m_FileChooser.getSelectedFile();
try { try {
ObjectOutputStream oo = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(sFile))); ObjectOutputStream oo = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream(sFile)));
oo.writeObject(object); oo.writeObject(object);
oo.close(); oo.close();
} catch (IOException ex) { } catch (IOException ex) {
JOptionPane.showMessageDialog(m_Frame, JOptionPane.showMessageDialog(m_Frame,
"Couldn't write to file: " "Couldn't write to file: " + sFile.getName() + "\n"
+ sFile.getName() + ex.getMessage(), "Save object",
+ "\n" + ex.getMessage(),
"Save object",
JOptionPane.ERROR_MESSAGE); JOptionPane.ERROR_MESSAGE);
} }
} }
@ -414,6 +458,7 @@ public class Plot implements PlotInterface, Serializable {
public FunctionArea getFunctionArea() { public FunctionArea getFunctionArea() {
return m_PlotArea; return m_PlotArea;
} }
/** /**
* *
*/ */
@ -452,4 +497,3 @@ public class Plot implements PlotInterface, Serializable {
// //plot.addGraph(1,2); // //plot.addGraph(1,2);
// } // }
} }