Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/com/CDPrintable/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");

}

Expand Down Expand Up @@ -244,20 +244,63 @@ public void displayPagesAsImages() {
}
}

public BufferedImage[] getAsBufferedImages() {
ArrayList<BufferedImage> 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() {
return labelMaxHeight;
}

public void setLabelMaxHeight(double labelMaxHeight) {
this.labelMaxHeight = labelMaxHeight;
this.labelMaxHeight = labelMaxHeight * dpiY;
}

public double getFontSize() {
Expand Down
54 changes: 44 additions & 10 deletions src/main/java/com/CDPrintable/ProgramWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -589,5 +624,4 @@ private void validateAndSetDoubleField(JTextField field, Consumer<Double> setter
private void showError(String message) {
JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
}

}
2 changes: 1 addition & 1 deletion src/main/resources/version.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Application version.

# MAJOR MINOR PATCH
version=2.0.0
version=2.1.1