diff --git a/src/main/java/com/CDPrintable/Main.java b/src/main/java/com/CDPrintable/Main.java index 4105ff6..f8ce6c9 100644 --- a/src/main/java/com/CDPrintable/Main.java +++ b/src/main/java/com/CDPrintable/Main.java @@ -12,10 +12,10 @@ import com.CDPrintable.MusicBrainzResources.MusicBrainzLabelGenerator; +import java.awt.*; + public class Main { public static void main(String[] args) { -// MusicBrainzLabelGenerator lg = new MusicBrainzLabelGenerator(); -// lg.displayPageAsImage(); ProgramWindow window = new ProgramWindow(); } } \ No newline at end of file diff --git a/src/main/java/com/CDPrintable/MusicBrainzResources/MusicBrainzLabelGenerator.java b/src/main/java/com/CDPrintable/MusicBrainzResources/MusicBrainzLabelGenerator.java index 9ed90ad..335112b 100644 --- a/src/main/java/com/CDPrintable/MusicBrainzResources/MusicBrainzLabelGenerator.java +++ b/src/main/java/com/CDPrintable/MusicBrainzResources/MusicBrainzLabelGenerator.java @@ -55,7 +55,7 @@ public MusicBrainzLabelGenerator() { this.fontSize = ConfigManager.getDoubleProperty("fontSize", 10); this.pageWidth = ConfigManager.getDoubleProperty("pageWidth", 8.5); this.pageHeight = ConfigManager.getDoubleProperty("pageHeight", 11); - this.fontName = ConfigManager.getProperty("fontName", "Arial"); + this.fontName = ConfigManager.getProperty("font", "Arial"); } @@ -244,12 +244,55 @@ public void displayPagesAsImages() { } } + public BufferedImage[] getAsBufferedImages() { + ArrayList images = new ArrayList<>(); + try { + // Create PrinterJob and PageFormat + PrinterJob job = PrinterJob.getPrinterJob(); + PageFormat pageFormat = job.defaultPage(); + Paper paper = pageFormat.getPaper(); + + // Define image dimensions based on paper size + int width = (int) paper.getWidth(); + int height = (int) paper.getHeight(); + + // Go through each page index until NO_SUCH_PAGE is returned + int pageIndex = 0; + while (true) { + // Create BufferedImage and draw page content + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + Graphics2D g2d = image.createGraphics(); + + // White background + g2d.setColor(Color.WHITE); + g2d.fillRect(0, 0, width, height); + + int result = this.print(g2d, pageFormat, pageIndex); + g2d.dispose(); + + // If there's no such page, break out of the loop + if (result != Printable.PAGE_EXISTS) { + break; + } + + // Add the image to the list + images.add(image); + + pageIndex++; + } + return images.toArray(new BufferedImage[images.size()]); + } catch (Exception e) { + e.printStackTrace(); + } + return images.toArray(new BufferedImage[0]); + } + public double getLabelWidth() { return labelWidth; } public void setLabelWidth(double labelWidth) { - this.labelWidth = labelWidth; + this.labelWidth = labelWidth * dpiX; } public double getLabelMaxHeight() { @@ -257,7 +300,7 @@ public double getLabelMaxHeight() { } public void setLabelMaxHeight(double labelMaxHeight) { - this.labelMaxHeight = labelMaxHeight; + this.labelMaxHeight = labelMaxHeight * dpiY; } public double getFontSize() { diff --git a/src/main/java/com/CDPrintable/ProgramWindow.java b/src/main/java/com/CDPrintable/ProgramWindow.java index 4c85ffe..cc343b9 100644 --- a/src/main/java/com/CDPrintable/ProgramWindow.java +++ b/src/main/java/com/CDPrintable/ProgramWindow.java @@ -19,6 +19,7 @@ import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URISyntaxException; import java.util.ArrayList; @@ -48,12 +49,12 @@ public ProgramWindow() { frame.setLayout(new BorderLayout()); JTabbedPane tabbedPane = new JTabbedPane(); - JPanel tablePanel = tablePanel(); + JPanel previewPanel = previewPanel(); JPanel findCDPanel = searchPanel(); JPanel settingsPanel = settingsPanel(); tabbedPane.addTab("Search", findCDPanel); - tabbedPane.addTab("Table", tablePanel); + tabbedPane.addTab("Preview", previewPanel); tabbedPane.addTab("Settings", settingsPanel); frame.add(tabbedPane, BorderLayout.CENTER); @@ -66,16 +67,50 @@ public ProgramWindow() { * Gets a JPanel for the table panel. This is a helper method. * @return A JPanel with the table panel. */ - private JPanel tablePanel() { + private JPanel previewPanel() { + // Set up panels JPanel panel = new JPanel(new BorderLayout()); + JPanel imagePanel = new JPanel(); + imagePanel.setLayout(new BoxLayout(imagePanel, BoxLayout.Y_AXIS)); + JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); + + // Set up buttons + JButton refreshButton = new JButton("Refresh Preview"); + JButton printButton = new JButton("Print"); + refreshButton.addActionListener(_ -> { + imagePanel.removeAll(); + BufferedImage[] images = labelGenerator.getAsBufferedImages(); + for (BufferedImage image : images) { + JLabel label = new JLabel(new ImageIcon(image)); + label.setAlignmentX(Component.CENTER_ALIGNMENT); + imagePanel.add(label); + imagePanel.add(Box.createRigidArea(new Dimension(0, 10))); + } + imagePanel.repaint(); + imagePanel.revalidate(); + }); + printButton.addActionListener(_ -> { + try { + labelGenerator.printLabel(); + } catch (Exception e) { + JOptionPane.showMessageDialog(null, "An error occurred while printing. More info: " + e, "Error", JOptionPane.ERROR_MESSAGE); + } + }); + + // Make a JSCrollPane for the image panel + JScrollPane imageScrollPane = new JScrollPane(imagePanel); + + // Add buttons to the button panel + buttonPanel.add(refreshButton); + buttonPanel.add(printButton); - // Set up all the tables for the cd - String[] columnNames = {"CD Name", "Artist", "Genre", "Year", "Track Count"}; - JTable table = new JTable(new String[][] {new String[] {"None", "", "", "", ""}}, columnNames); - JScrollPane scrollPane = new JScrollPane(table); + // Set up the image panel + imagePanel.setBorder(BorderFactory.createTitledBorder("Preview")); - // Set up the panel - panel.add(scrollPane, BorderLayout.CENTER); + + // Add subpanels to main panel + panel.add(imageScrollPane, BorderLayout.CENTER); + panel.add(buttonPanel, BorderLayout.NORTH); return panel; } @@ -589,5 +624,4 @@ private void validateAndSetDoubleField(JTextField field, Consumer setter private void showError(String message) { JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE); } - } \ No newline at end of file diff --git a/src/main/resources/version.properties b/src/main/resources/version.properties index bf5c94b..9049761 100644 --- a/src/main/resources/version.properties +++ b/src/main/resources/version.properties @@ -1,4 +1,4 @@ # Application version. # MAJOR MINOR PATCH -version=2.0.0 \ No newline at end of file +version=2.1.1 \ No newline at end of file