List of usage examples for org.jfree.chart ChartPanel getSize
public Dimension getSize()
From source file:loldmg.GUI.MainFrame.java
/** * Creates new form MainFrame/*from www . ja v a2 s. co m*/ */ public MainFrame() { initComponents(); Instance = this; DPSButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { GraphPanel.removeAll(); chart = createChart(createDataset((Champion) championComboBox.getSelectedItem(), (Champion) targetComboBox.getSelectedItem())); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setSize(new java.awt.Dimension(700, 450)); chartPanel.setVisible(true); GraphPanel.add(chartPanel); GraphPanel.setSize(chartPanel.getSize()); pack(); } }); }
From source file:fr.ign.cogit.simplu3d.rjmcmc.generic.visitor.FilmVisitor.java
/** * Permet d'enregistrer une image partir de l'cran Ne fonctionne qu'avec * l'IHM actuel (Offset ncessaire) Ne prends pas compte de l'existance d'un * fichier de mme nom/*from ww w . j a va 2s . co m*/ * * @param path * le dossier dans lequel l'impr ecran sera supprime * @param fileName * le nom du fichier * @return indique si la capture s'est effectue avec succs */ public boolean screenCapture(String path, String fileName, InterfaceMap3D iMap3D) { try { ChartPanel v = StatsVisitor.CHARTSINGLETON; int xSup = 0; int ySup = 0; boolean hasStats = (v != null); if (hasStats) { xSup = v.getSize().width; ySup = v.getSize().height; } int xSize = iMap3D.getSize().width + xSup; int ySize = Math.max(iMap3D.getSize().height, ySup); BufferedImage bufImage = new BufferedImage(xSize, ySize, BufferedImage.TYPE_INT_RGB); Graphics g = bufImage.createGraphics(); g.setColor(Color.white); // g.drawRect(0, 0, xSize, ySize); g.fillRect(0, 0, xSize, ySize); iMap3D.getCanvas3D().paint(g); if (hasStats) { g.drawImage(v.getChart().createBufferedImage(xSup, ySup), iMap3D.getSize().width, (ySize - ySup) / 2, null); } File fichier = new File(path, fileName); if (fichier.exists()) { System.out.println("Fail"); return false; } else { ImageIO.write(bufImage, "jpg", fichier); return true; } } catch (Exception e) { e.printStackTrace(); } return false; }
From source file:src.gui.LifelinePanel.java
/** * this method created and set the graph for showing the timing diagram based * on the variable diagram// w w w. jav a 2 s.co m */ public void buildLifeLine() { //1. get type and context String dtype = diagram.getChildText("type"); String context = diagram.getChildText("context"); //the frame and lifeline nodes Element frame = diagram.getChild("frame"); String durationStr = frame.getChildText("duration"); lifelineName = ""; String objectClassName = ""; String yAxisName = ""; float lastIntervalDuration = 0; //condition lifeline if (dtype.equals("condition")) { //check if the context is a action if (context.equals("action")) { //get action/operator Element operatorRef = diagram.getChild("action"); Element operator = null; try { XPath path = new JDOMXPath( "elements/classes/class[@id='" + operatorRef.getAttributeValue("class") + "']/operators/operator[@id='" + operatorRef.getAttributeValue("id") + "']"); operator = (Element) path.selectSingleNode(project); } catch (JaxenException e2) { e2.printStackTrace(); } if (operator != null) { // System.out.println(operator.getChildText("name")); //System.out.println("Life line id "+ lifeline.getAttributeValue("id")); //get the object (can be a parametr. literal, or object) Element objRef = lifeline.getChild("object"); Element attrRef = lifeline.getChild("attribute"); //get object class Element objClass = null; try { XPath path = new JDOMXPath( "elements/classes/class[@id='" + objRef.getAttributeValue("class") + "']"); objClass = (Element) path.selectSingleNode(project); } catch (JaxenException e2) { e2.printStackTrace(); } Element attribute = null; try { XPath path = new JDOMXPath( "elements/classes/class[@id='" + attrRef.getAttributeValue("class") + "']/attributes/attribute[@id='" + attrRef.getAttributeValue("id") + "']"); attribute = (Element) path.selectSingleNode(project); } catch (JaxenException e2) { e2.printStackTrace(); } yAxisName = attribute.getChildText("name"); //if (objClass!=null) Element object = null; //check what is this object (parameterof an action, object, literal) if (objRef.getAttributeValue("element").equals("parameter")) { //get parameter in the action try { XPath path = new JDOMXPath( "parameters/parameter[@id='" + objRef.getAttributeValue("id") + "']"); object = (Element) path.selectSingleNode(operator); } catch (JaxenException e2) { e2.printStackTrace(); } String parameterStr = object.getChildText("name"); lifelineName = parameterStr + ":" + objClass.getChildText("name"); objectClassName = parameterStr + ":" + objClass.getChildText("name"); } // //set suround border Border etchedBdr = BorderFactory.createEtchedBorder(); Border titledBdr = BorderFactory.createTitledBorder(etchedBdr, "lifeline(" + lifelineName + ")"); //Border titledBdr = BorderFactory.createTitledBorder(etchedBdr, ""); Border emptyBdr = BorderFactory.createEmptyBorder(10, 10, 10, 10); Border compoundBdr = BorderFactory.createCompoundBorder(titledBdr, emptyBdr); this.setBorder(compoundBdr); //Boolean attribute if (attribute.getChildText("type").equals("1")) { lifelineName += " - " + attribute.getChildText("name"); Element timeIntervals = lifeline.getChild("timeIntervals"); XYSeriesCollection dataset = new XYSeriesCollection(); XYSeries series = new XYSeries("Boolean"); for (Iterator<Element> it1 = timeIntervals.getChildren().iterator(); it1.hasNext();) { Element timeInterval = it1.next(); boolean insertPoint = true; Element durationConstratint = timeInterval.getChild("durationConstratint"); Element lowerbound = durationConstratint.getChild("lowerbound"); Element upperbound = durationConstratint.getChild("upperbound"); Element value = timeInterval.getChild("value"); //Add for both lower and upper bound //lower bound float lowerTimePoint = 0; try { lowerTimePoint = Float.parseFloat(lowerbound.getAttributeValue("value")); lastIntervalDuration = lowerTimePoint; } catch (Exception e) { insertPoint = false; } //System.out.println(" > point x= "+ Float.toString(lowerTimePoint)+ " , y= "+ lowerbound.getAttributeValue("value")); if (insertPoint) { series.add(lowerTimePoint, (value.getText().equals("false") ? 0 : 1)); } //upper bound float upperTimePoint = 0; try { upperTimePoint = Float.parseFloat(upperbound.getAttributeValue("value")); lastIntervalDuration = upperTimePoint; } catch (Exception e) { insertPoint = false; } //System.out.println(" > point x= "+ Float.toString(upperTimePoint)+ " , y= "+ lowerbound.getAttributeValue("value")); if (insertPoint && upperTimePoint != lowerTimePoint) { series.add(upperTimePoint, (value.getText().equals("false") ? 0 : 1)); } } dataset.addSeries(series); //chart = ChartFactory.createXYStepChart(lifelineName, "time", "value", dataset, PlotOrientation.VERTICAL, false, true, false); chart = ChartFactory.createXYStepChart(attribute.getChildText("name"), "time", "value", dataset, PlotOrientation.VERTICAL, false, true, false); chart.setBackgroundPaint(Color.WHITE); XYPlot plot = (XYPlot) chart.getPlot(); plot.setBackgroundPaint(Color.WHITE); NumberAxis domainAxis = new NumberAxis("Time"); domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); domainAxis.setAutoRangeIncludesZero(false); //set timing ruler if (durationStr.trim().equals("")) { if (lastIntervalDuration > 0) domainAxis.setUpperBound(lastIntervalDuration + timingRulerAdditional); else domainAxis.setUpperBound(10.0); } else { try { float dur = Float.parseFloat(durationStr); if (dur >= lastIntervalDuration) { domainAxis.setUpperBound(dur + timingRulerAdditional); } else { domainAxis.setUpperBound(lastIntervalDuration + timingRulerAdditional); } } catch (Exception e) { if (lastIntervalDuration > 0) domainAxis.setUpperBound(lastIntervalDuration + timingRulerAdditional); else domainAxis.setUpperBound(10.0); } } plot.setDomainAxis(domainAxis); String[] values = { "false", "true" }; //SymbolAxis rangeAxis = new SymbolAxis("Values", values); SymbolAxis rangeAxis = new SymbolAxis(yAxisName, values); plot.setRangeAxis(rangeAxis); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new Dimension(chartPanel.getSize().width, 175)); JLabel title = new JLabel("<html><b><u>" + objectClassName + "</u></b></html>"); title.setBackground(Color.WHITE); this.add(title, BorderLayout.WEST); this.add(chartPanel, BorderLayout.CENTER); } } } //if this is a possible sequence of action being modeled to a condition else if (context.equals("general")) { } } else if (dtype.equals("state")) { } }
From source file:ac.kaist.ccs.presentation.CCSHubSelectionCoverage.java
public CCSHubSelectionCoverage(final String title, List<Double> coverSourceNum) { super(title); this.title = title; final XYDataset dataset = createDataset(coverSourceNum); final JFreeChart chart = createChart(dataset); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); final ChartPanel chartPanel = new ChartPanel(chart); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BorderLayout()); JButton buttonExport = new JButton("Export"); buttonPanel.add("East", buttonExport); buttonExport.addActionListener(new ActionListener() { ChartPanel chartPanel;// ww w . ja va 2s .c om public ActionListener init(ChartPanel chartPanel) { this.chartPanel = chartPanel; return this; } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub Dimension size = chartPanel.getSize(); try { //String outPath = textFieldSelectPath.getText(); //String filename = "chromatography.png"; //String path = outPath+"/"+filename; JFileChooser fileChooser = new JFileChooser(); fileChooser.setCurrentDirectory(new File("/Users/mac/Desktop")); fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("JPEG", "jpeg")); int returnVal = fileChooser.showDialog(new JFrame(), "Open File Path"); if (returnVal == JFileChooser.APPROVE_OPTION) { String inputPath = fileChooser.getSelectedFile().getAbsolutePath(); if (!inputPath.endsWith(".jpeg")) inputPath = inputPath + ".jpeg"; OutputStream os = new FileOutputStream(inputPath); System.out.println(inputPath + "///" + size.width + " " + size.height); BufferedImage chartImage = chartPanel.getChart().createBufferedImage(size.width, size.height, null); ImageIO.write(chartImage, "png", os); os.close(); JOptionPane.showMessageDialog(null, "Chart image was saved in " + inputPath); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.init(chartPanel)); panel.add("Center", chartPanel); panel.add("South", buttonPanel); chartPanel.setPreferredSize(new java.awt.Dimension(700, 500)); setContentPane(panel); }
From source file:ac.kaist.ccs.presentation.CCSHubSelectionCo2Coverage.java
public CCSHubSelectionCo2Coverage(final String title, List<Double> coverSourceNum) { super(title); this.title = title; final XYDataset dataset = createDataset(coverSourceNum); final JFreeChart chart = createChart(dataset); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); final ChartPanel chartPanel = new ChartPanel(chart); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BorderLayout()); JButton buttonExport = new JButton("Export"); buttonPanel.add("East", buttonExport); buttonExport.addActionListener(new ActionListener() { ChartPanel chartPanel;/* w w w.j a v a 2 s . c o m*/ public ActionListener init(ChartPanel chartPanel) { this.chartPanel = chartPanel; return this; } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub Dimension size = chartPanel.getSize(); try { //String outPath = textFieldSelectPath.getText(); //String filename = "chromatography.png"; //String path = outPath+"/"+filename; JFileChooser fileChooser = new JFileChooser(); fileChooser.setCurrentDirectory(new File("/Users/mac/Desktop")); fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("JPEG", "jpeg")); int returnVal = fileChooser.showDialog(new JFrame(), "Open File Path"); if (returnVal == JFileChooser.APPROVE_OPTION) { String inputPath = fileChooser.getSelectedFile().getAbsolutePath(); if (!inputPath.endsWith(".jpeg")) inputPath = inputPath + ".jpeg"; OutputStream os = new FileOutputStream(inputPath); System.out.println(inputPath + "///" + size.width + " " + size.height); BufferedImage chartImage = chartPanel.getChart().createBufferedImage(size.width, size.height, null); ImageIO.write(chartImage, "png", os); os.close(); JOptionPane.showMessageDialog(null, "Chart image was saved in " + inputPath); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.init(chartPanel)); panel.add("Center", chartPanel); panel.add("South", buttonPanel); chartPanel.setPreferredSize(new java.awt.Dimension(700, 500)); setContentPane(panel); }
From source file:ac.kaist.ccs.presentation.CCSHubSelectionCost.java
public CCSHubSelectionCost(final String title, Map<Integer, List<Double>> data) { super(title); this.title = title; final XYDataset dataset = createDataset(data); final JFreeChart chart = createChart(dataset); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); final ChartPanel chartPanel = new ChartPanel(chart); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BorderLayout()); JButton buttonExport = new JButton("Export"); buttonPanel.add("East", buttonExport); buttonExport.addActionListener(new ActionListener() { ChartPanel chartPanel;//ww w .j a v a 2 s . co m public ActionListener init(ChartPanel chartPanel) { this.chartPanel = chartPanel; return this; } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub Dimension size = chartPanel.getSize(); try { //String outPath = textFieldSelectPath.getText(); //String filename = "chromatography.png"; //String path = outPath+"/"+filename; JFileChooser fileChooser = new JFileChooser(); fileChooser.setCurrentDirectory(new File("/Users/mac/Desktop")); fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("JPEG", "jpeg")); int returnVal = fileChooser.showDialog(new JFrame(), "Open File Path"); if (returnVal == JFileChooser.APPROVE_OPTION) { String inputPath = fileChooser.getSelectedFile().getAbsolutePath(); if (!inputPath.endsWith(".jpeg")) inputPath = inputPath + ".jpeg"; OutputStream os = new FileOutputStream(inputPath); System.out.println(inputPath + "///" + size.width + " " + size.height); BufferedImage chartImage = chartPanel.getChart().createBufferedImage(size.width, size.height, null); ImageIO.write(chartImage, "png", os); os.close(); JOptionPane.showMessageDialog(null, "Chart image was saved in " + inputPath); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.init(chartPanel)); panel.add("Center", chartPanel); panel.add("South", buttonPanel); chartPanel.setPreferredSize(new java.awt.Dimension(700, 500)); setContentPane(panel); }
From source file:velocitekProStartAnalyzer.MainWindow.java
private void saveChartAsPng(ChartPanel panel) { btnSaveChartAsPng.addActionListener(new ActionListener() { @Override/*from w w w.ja v a 2 s .c o m*/ public void actionPerformed(ActionEvent e) { fileChooser.setDialogTitle("Specify a file to save"); int userSelection = fileChooser.showSaveDialog(frame); BufferedImage bufImage = new BufferedImage(panel.getSize().width, panel.getSize().height, BufferedImage.TYPE_INT_RGB); panel.paint(bufImage.createGraphics()); if (userSelection == JFileChooser.APPROVE_OPTION) { File fileToSave = new File(fileChooser.getSelectedFile() + ".png"); System.out.println("Save as file: " + fileToSave.getAbsolutePath()); try { fileToSave.createNewFile(); ImageIO.write(bufImage, "png", fileToSave); statusLabel.setText("Chart Screenshot Saved as: " + fileToSave.getName()); } catch (Exception ex) { statusLabel.setText("There was an error during saving"); } } } }); btnSaveChartAsPngForChart.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { fileChooser.setDialogTitle("Specify a file to save"); int userSelection = fileChooser.showSaveDialog(frame); BufferedImage bufImage = new BufferedImage(panel.getSize().width, panel.getSize().height, BufferedImage.TYPE_INT_RGB); panel.paint(bufImage.createGraphics()); if (userSelection == JFileChooser.APPROVE_OPTION) { File fileToSave = new File(fileChooser.getSelectedFile() + ".png"); System.out.println("Save as file: " + fileToSave.getAbsolutePath()); try { fileToSave.createNewFile(); ImageIO.write(bufImage, "png", fileToSave); statusLabel.setText("Chart Screenshot Saved as: " + fileToSave.getName()); } catch (Exception ex) { statusLabel.setText("There was an error during saving"); } } } }); }
From source file:src.planning.PlanSimulator.java
public static List<ChartPanel> drawCharts(Element analysis, Element problem) { List<ChartPanel> charts = new ArrayList<ChartPanel>(); // draw the charts List<?> variables = analysis.getChild("variables").getChildren("variable"); for (Iterator<?> iter = variables.iterator(); iter.hasNext();) { Element variable = (Element) iter.next(); Element domainObject = null; try {//from w w w. j a v a2 s. co m XPath path = new JDOMXPath("elements/objects/object[@id='" + variable.getChild("object").getAttributeValue("id") + "']"); domainObject = (Element) path.selectSingleNode(problem.getParentElement().getParentElement()); } catch (JaxenException e) { e.printStackTrace(); } Element attribute = variable.getChild("object").getChild("attribute"); Element classAttr = null; try { XPath path = new JDOMXPath( "project/elements/classes/class[@id='" + attribute.getAttributeValue("class") + "']/attributes/attribute[@id='" + attribute.getAttributeValue("id") + "']"); classAttr = (Element) path.selectSingleNode(problem.getDocument()); } catch (JaxenException e) { e.printStackTrace(); } String chartTitle = domainObject.getChildText("name") + "." + classAttr.getChildText("name"); if (variable.getAttributeValue("type").equals("attr")) { // attribute String attrType = variable.getChild("object").getChild("attribute").getAttributeValue("type"); if (attrType.equals("1")) { //BOOLEAN attribute XYSeriesCollection dataset = new XYSeriesCollection(); XYSeries series = new XYSeries("Boolean"); int stepIndex = 0; for (Iterator<?> iterator = variable.getChild("values").getChildren("value") .iterator(); iterator.hasNext();) { Element value = (Element) iterator.next(); series.add(stepIndex++, (value.getText().equals("false") ? 0 : 1)); } dataset.addSeries(series); JFreeChart chart = ChartFactory.createXYStepChart(chartTitle, "Values", "Steps", dataset, PlotOrientation.VERTICAL, false, true, false); XYPlot plot = (XYPlot) chart.getPlot(); NumberAxis domainAxis = new NumberAxis("Steps"); domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); domainAxis.setAutoRangeIncludesZero(false); plot.setDomainAxis(domainAxis); String[] values = { "false", "true" }; SymbolAxis rangeAxis = new SymbolAxis("Values", values); plot.setRangeAxis(rangeAxis); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new Dimension(chartPanel.getSize().width, 175)); charts.add(chartPanel); } else if (attrType.equals("2") || attrType.equals("3")) { //NUMERIC attributes XYSeriesCollection dataset = new XYSeriesCollection(); XYSeries series = new XYSeries("variable"); int stepIndex = 0; for (Iterator<?> iterator = variable.getChild("values").getChildren("value") .iterator(); iterator.hasNext();) { Element value = (Element) iterator.next(); series.add(stepIndex++, Double.parseDouble(value.getText())); } dataset.addSeries(series); JFreeChart chart = ChartFactory.createXYLineChart(chartTitle, "Steps", "Values", dataset, PlotOrientation.VERTICAL, false, true, false); XYPlot xyPlot = (XYPlot) chart.getPlot(); XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) xyPlot.getRenderer(); renderer.setShapesVisible(true); renderer.setShapesFilled(true); NumberAxis rangeAxis = (NumberAxis) xyPlot.getRangeAxis(); rangeAxis.setAutoRangeIncludesZero(true); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); if (attrType.equals("2")) { NumberAxis domainAxis = (NumberAxis) xyPlot.getDomainAxis(); domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); } ChartPanel chartPanel = new ChartPanel(chart); charts.add(chartPanel); } else if (!attrType.equals("4")) {//NOT PRIMITIVE attributes Element attrClass = null; try { XPath path = new JDOMXPath( "project/elements/classes/class[@id='" + classAttr.getChildText("type") + "']"); attrClass = (Element) path.selectSingleNode(problem.getDocument()); } catch (JaxenException e) { e.printStackTrace(); } if (attrClass != null) { List<Element> classes = XMLUtilities.getClassDescendents(attrClass); String query = "elements/objects/object["; for (Iterator<?> iterator = classes.iterator(); iterator.hasNext();) { Element childClass = (Element) iterator.next(); query += "class='" + childClass.getAttributeValue("id") + "'"; query += " or ";// the last or is for the parent class } query += "class='" + attrClass.getAttributeValue("id") + "']"; // get all the objects of all descendant classes, including the parent class List<?> objects = null; try { XPath path = new JDOMXPath(query); objects = path.selectNodes(problem.getParentElement().getParentElement()); } catch (JaxenException e) { e.printStackTrace(); } if (objects.size() > 0) { //build a list with all the objects names String[] names = new String[objects.size() + 1];// the array is for the axis names[0] = "null";// default null value List<String> objectNames = new ArrayList<String>(); int i = 1; for (Iterator<?> iterator = objects.iterator(); iterator.hasNext();) { Element object = (Element) iterator.next(); names[i++] = object.getChildText("name"); objectNames.add(object.getChildText("name").toLowerCase()); } XYSeriesCollection dataset = new XYSeriesCollection(); XYSeries series = new XYSeries("Objects"); int stepIndex = 0; for (Iterator<?> iterator = variable.getChild("values").getChildren("value") .iterator(); iterator.hasNext();) { Element value = (Element) iterator.next(); series.add(stepIndex++, objectNames.indexOf(value.getText().toLowerCase()) + 1); } dataset.addSeries(series); // draw the chart JFreeChart chart = ChartFactory.createXYStepChart(chartTitle, "Objects", "Steps", dataset, PlotOrientation.VERTICAL, false, true, false); XYPlot plot = (XYPlot) chart.getPlot(); NumberAxis domainAxis = new NumberAxis("Steps"); domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); domainAxis.setAutoRangeIncludesZero(false); plot.setDomainAxis(domainAxis); SymbolAxis rangeAxis = new SymbolAxis("Objects", names); plot.setRangeAxis(rangeAxis); ChartPanel chartPanel = new ChartPanel(chart); charts.add(chartPanel); } } } } } return charts; }