From 64cc14239de980d32bb7278ffbd3be5baa7f966c Mon Sep 17 00:00:00 2001 From: rattatwinko Date: Mon, 9 Jun 2025 21:13:15 +0200 Subject: [PATCH] Implement PDF export, smooth test runs, and chart display improvements. --- pom.xml | 6 +++ src/main/java/com/puchdyno/DynoGUI.java | 2 +- src/main/java/com/puchdyno/Main.java | 57 +++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index ee4bf5d..cbf33b4 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,12 @@ jfreechart 1.5.3 + + + com.itextpdf + itextpdf + 5.5.13.3 + diff --git a/src/main/java/com/puchdyno/DynoGUI.java b/src/main/java/com/puchdyno/DynoGUI.java index 999f791..d3c5c0e 100644 --- a/src/main/java/com/puchdyno/DynoGUI.java +++ b/src/main/java/com/puchdyno/DynoGUI.java @@ -77,7 +77,7 @@ public class DynoGUI extends JFrame { plot.mapDatasetToRangeAxis(1, 1); NumberAxis xAxis = (NumberAxis) plot.getDomainAxis(); - xAxis.setRange(4000.0, 12000.0); + xAxis.setRange(0.0, 12000.0); xAxis.setLabel("U/min"); XYLineAndShapeRenderer renderer1 = new XYLineAndShapeRenderer(); diff --git a/src/main/java/com/puchdyno/Main.java b/src/main/java/com/puchdyno/Main.java index 6548988..eb43705 100644 --- a/src/main/java/com/puchdyno/Main.java +++ b/src/main/java/com/puchdyno/Main.java @@ -15,6 +15,13 @@ import org.jfree.chart.JFreeChart; import java.util.ArrayList; import java.util.TreeMap; import java.util.Map; +import com.itextpdf.text.Document; +import com.itextpdf.text.pdf.PdfWriter; +import com.itextpdf.text.Image; +import com.itextpdf.text.Rectangle; +import java.io.FileOutputStream; +import javax.swing.filechooser.FileNameExtensionFilter; +import java.io.File; public class Main { @@ -37,7 +44,7 @@ public class Main { private static int testRpmCurrentIndex = 0; private static double testRpmCurrentValue = 0.0; - private static final double TEST_RPM_INCREMENT_STEP = 1.0; // Very small steps for extremely smooth simulation + private static final double TEST_RPM_INCREMENT_STEP = 100.0; // Increased for faster simulation private static java.util.List smoothRpmData; private static int smoothRpmDataIndex = 0; @@ -175,7 +182,7 @@ public class Main { testDataExecutor = Executors.newSingleThreadScheduledExecutor(); testDataExecutor.scheduleAtFixedRate(() -> { simulateSmoothRpmRun(); - }, 0, 50, TimeUnit.MILLISECONDS); // Update RPM every 50ms + }, 0, 1, TimeUnit.MILLISECONDS); // Update RPM every 10ms for faster animation } private static void simulateSmoothRpmRun() { @@ -197,7 +204,6 @@ public class Main { testDataExecutor = null; SwingUtilities.invokeLater(() -> { dynoGUI.updateWerte(0, 0, 0); // Reset gauges - dynoGUI.resetChart(); // Clear chart on stop }); } } @@ -290,7 +296,6 @@ public class Main { } SwingUtilities.invokeLater(() -> { dynoGUI.updateWerte(0, 0, 0); // Reset gauges - dynoGUI.resetChart(); // Clear chart on stop }); } @@ -418,11 +423,55 @@ public class Main { } }); + JButton savePdfButton = new JButton("Save as PDF"); + savePdfButton.addActionListener(e -> saveChartAsPdf(printChartPanel)); + JPanel controlPanel = new JPanel(); controlPanel.add(printButton); + controlPanel.add(savePdfButton); chartFrame.add(controlPanel, BorderLayout.SOUTH); chartFrame.setLocationRelativeTo(dynoGUI); chartFrame.setVisible(true); } + + private static void saveChartAsPdf(ChartPanel chartPanel) { + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setDialogTitle("Save Chart as PDF"); + fileChooser.setFileFilter(new FileNameExtensionFilter("PDF Documents (*.pdf)", "pdf")); + + int userSelection = fileChooser.showSaveDialog(null); + + if (userSelection == JFileChooser.APPROVE_OPTION) { + File fileToSave = fileChooser.getSelectedFile(); + String filePath = fileToSave.getAbsolutePath(); + if (!filePath.toLowerCase().endsWith(".pdf")) { + filePath += ".pdf"; + } + File finalFile = new File(filePath); + + try { + // Use the dimensions of the ChartPanel to create the PDF document + Document document = new Document(new Rectangle(chartPanel.getWidth(), chartPanel.getHeight())); + PdfWriter.getInstance(document, new FileOutputStream(finalFile)); + document.open(); + + // Create BufferedImage from the ChartPanel's chart + java.awt.Image awtImage = chartPanel.getChart().createBufferedImage(chartPanel.getWidth(), chartPanel.getHeight()); + Image pdfImage = Image.getInstance(awtImage, null); + + // Scale image to fit document + pdfImage.scaleToFit(document.getPageSize().getWidth() - 20, document.getPageSize().getHeight() - 20); // Add some padding + pdfImage.setAlignment(Image.ALIGN_CENTER); + + document.add(pdfImage); + document.close(); + + JOptionPane.showMessageDialog(null, "Chart saved as PDF successfully!\n" + finalFile.getAbsolutePath(), "Save Successful", JOptionPane.INFORMATION_MESSAGE); + } catch (Exception ex) { + JOptionPane.showMessageDialog(null, "Error saving chart as PDF: " + ex.getMessage(), "Save Error", JOptionPane.ERROR_MESSAGE); + ex.printStackTrace(); + } + } + } } \ No newline at end of file