Implement PDF export, smooth test runs, and chart display improvements.
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -27,6 +27,12 @@
|
|||||||
<artifactId>jfreechart</artifactId>
|
<artifactId>jfreechart</artifactId>
|
||||||
<version>1.5.3</version>
|
<version>1.5.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- iText for PDF generation -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.itextpdf</groupId>
|
||||||
|
<artifactId>itextpdf</artifactId>
|
||||||
|
<version>5.5.13.3</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ public class DynoGUI extends JFrame {
|
|||||||
plot.mapDatasetToRangeAxis(1, 1);
|
plot.mapDatasetToRangeAxis(1, 1);
|
||||||
|
|
||||||
NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
|
NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
|
||||||
xAxis.setRange(4000.0, 12000.0);
|
xAxis.setRange(0.0, 12000.0);
|
||||||
xAxis.setLabel("U/min");
|
xAxis.setLabel("U/min");
|
||||||
|
|
||||||
XYLineAndShapeRenderer renderer1 = new XYLineAndShapeRenderer();
|
XYLineAndShapeRenderer renderer1 = new XYLineAndShapeRenderer();
|
||||||
|
|||||||
@@ -15,6 +15,13 @@ import org.jfree.chart.JFreeChart;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.Map;
|
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 {
|
public class Main {
|
||||||
|
|
||||||
@@ -37,7 +44,7 @@ public class Main {
|
|||||||
|
|
||||||
private static int testRpmCurrentIndex = 0;
|
private static int testRpmCurrentIndex = 0;
|
||||||
private static double testRpmCurrentValue = 0.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<Double> smoothRpmData;
|
private static java.util.List<Double> smoothRpmData;
|
||||||
private static int smoothRpmDataIndex = 0;
|
private static int smoothRpmDataIndex = 0;
|
||||||
@@ -175,7 +182,7 @@ public class Main {
|
|||||||
testDataExecutor = Executors.newSingleThreadScheduledExecutor();
|
testDataExecutor = Executors.newSingleThreadScheduledExecutor();
|
||||||
testDataExecutor.scheduleAtFixedRate(() -> {
|
testDataExecutor.scheduleAtFixedRate(() -> {
|
||||||
simulateSmoothRpmRun();
|
simulateSmoothRpmRun();
|
||||||
}, 0, 50, TimeUnit.MILLISECONDS); // Update RPM every 50ms
|
}, 0, 1, TimeUnit.MILLISECONDS); // Update RPM every 10ms for faster animation
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void simulateSmoothRpmRun() {
|
private static void simulateSmoothRpmRun() {
|
||||||
@@ -197,7 +204,6 @@ public class Main {
|
|||||||
testDataExecutor = null;
|
testDataExecutor = null;
|
||||||
SwingUtilities.invokeLater(() -> {
|
SwingUtilities.invokeLater(() -> {
|
||||||
dynoGUI.updateWerte(0, 0, 0); // Reset gauges
|
dynoGUI.updateWerte(0, 0, 0); // Reset gauges
|
||||||
dynoGUI.resetChart(); // Clear chart on stop
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -290,7 +296,6 @@ public class Main {
|
|||||||
}
|
}
|
||||||
SwingUtilities.invokeLater(() -> {
|
SwingUtilities.invokeLater(() -> {
|
||||||
dynoGUI.updateWerte(0, 0, 0); // Reset gauges
|
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();
|
JPanel controlPanel = new JPanel();
|
||||||
controlPanel.add(printButton);
|
controlPanel.add(printButton);
|
||||||
|
controlPanel.add(savePdfButton);
|
||||||
chartFrame.add(controlPanel, BorderLayout.SOUTH);
|
chartFrame.add(controlPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
chartFrame.setLocationRelativeTo(dynoGUI);
|
chartFrame.setLocationRelativeTo(dynoGUI);
|
||||||
chartFrame.setVisible(true);
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user