Skip to content

Commit 4dd4eb5

Browse files
Merge pull request #77 from EatSleepProgramRepeat/7-make-a-preview-tab
7-make-a-preview-tab
2 parents 11baa07 + 6eb30e5 commit 4dd4eb5

File tree

4 files changed

+93
-16
lines changed

4 files changed

+93
-16
lines changed

src/main/java/com/CDPrintable/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
import com.CDPrintable.MusicBrainzResources.MusicBrainzLabelGenerator;
1414

15+
import java.awt.*;
16+
1517
public class Main {
1618
public static void main(String[] args) {
17-
// MusicBrainzLabelGenerator lg = new MusicBrainzLabelGenerator();
18-
// lg.displayPageAsImage();
1919
ProgramWindow window = new ProgramWindow();
2020
}
2121
}

src/main/java/com/CDPrintable/MusicBrainzResources/MusicBrainzLabelGenerator.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public MusicBrainzLabelGenerator() {
5555
this.fontSize = ConfigManager.getDoubleProperty("fontSize", 10);
5656
this.pageWidth = ConfigManager.getDoubleProperty("pageWidth", 8.5);
5757
this.pageHeight = ConfigManager.getDoubleProperty("pageHeight", 11);
58-
this.fontName = ConfigManager.getProperty("fontName", "Arial");
58+
this.fontName = ConfigManager.getProperty("font", "Arial");
5959

6060
}
6161

@@ -244,20 +244,63 @@ public void displayPagesAsImages() {
244244
}
245245
}
246246

247+
public BufferedImage[] getAsBufferedImages() {
248+
ArrayList<BufferedImage> images = new ArrayList<>();
249+
try {
250+
// Create PrinterJob and PageFormat
251+
PrinterJob job = PrinterJob.getPrinterJob();
252+
PageFormat pageFormat = job.defaultPage();
253+
Paper paper = pageFormat.getPaper();
254+
255+
// Define image dimensions based on paper size
256+
int width = (int) paper.getWidth();
257+
int height = (int) paper.getHeight();
258+
259+
// Go through each page index until NO_SUCH_PAGE is returned
260+
int pageIndex = 0;
261+
while (true) {
262+
// Create BufferedImage and draw page content
263+
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
264+
Graphics2D g2d = image.createGraphics();
265+
266+
// White background
267+
g2d.setColor(Color.WHITE);
268+
g2d.fillRect(0, 0, width, height);
269+
270+
int result = this.print(g2d, pageFormat, pageIndex);
271+
g2d.dispose();
272+
273+
// If there's no such page, break out of the loop
274+
if (result != Printable.PAGE_EXISTS) {
275+
break;
276+
}
277+
278+
// Add the image to the list
279+
images.add(image);
280+
281+
pageIndex++;
282+
}
283+
return images.toArray(new BufferedImage[images.size()]);
284+
} catch (Exception e) {
285+
e.printStackTrace();
286+
}
287+
return images.toArray(new BufferedImage[0]);
288+
}
289+
247290
public double getLabelWidth() {
248291
return labelWidth;
249292
}
250293

251294
public void setLabelWidth(double labelWidth) {
252-
this.labelWidth = labelWidth;
295+
this.labelWidth = labelWidth * dpiX;
253296
}
254297

255298
public double getLabelMaxHeight() {
256299
return labelMaxHeight;
257300
}
258301

259302
public void setLabelMaxHeight(double labelMaxHeight) {
260-
this.labelMaxHeight = labelMaxHeight;
303+
this.labelMaxHeight = labelMaxHeight * dpiY;
261304
}
262305

263306
public double getFontSize() {

src/main/java/com/CDPrintable/ProgramWindow.java

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.awt.*;
2020
import java.awt.event.MouseAdapter;
2121
import java.awt.event.MouseEvent;
22+
import java.awt.image.BufferedImage;
2223
import java.io.IOException;
2324
import java.net.URISyntaxException;
2425
import java.util.ArrayList;
@@ -48,12 +49,12 @@ public ProgramWindow() {
4849
frame.setLayout(new BorderLayout());
4950

5051
JTabbedPane tabbedPane = new JTabbedPane();
51-
JPanel tablePanel = tablePanel();
52+
JPanel previewPanel = previewPanel();
5253
JPanel findCDPanel = searchPanel();
5354
JPanel settingsPanel = settingsPanel();
5455

5556
tabbedPane.addTab("Search", findCDPanel);
56-
tabbedPane.addTab("Table", tablePanel);
57+
tabbedPane.addTab("Preview", previewPanel);
5758
tabbedPane.addTab("Settings", settingsPanel);
5859

5960
frame.add(tabbedPane, BorderLayout.CENTER);
@@ -66,16 +67,50 @@ public ProgramWindow() {
6667
* Gets a JPanel for the table panel. This is a helper method.
6768
* @return A JPanel with the table panel.
6869
*/
69-
private JPanel tablePanel() {
70+
private JPanel previewPanel() {
71+
// Set up panels
7072
JPanel panel = new JPanel(new BorderLayout());
73+
JPanel imagePanel = new JPanel();
74+
imagePanel.setLayout(new BoxLayout(imagePanel, BoxLayout.Y_AXIS));
75+
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
76+
77+
// Set up buttons
78+
JButton refreshButton = new JButton("Refresh Preview");
79+
JButton printButton = new JButton("Print");
80+
refreshButton.addActionListener(_ -> {
81+
imagePanel.removeAll();
82+
BufferedImage[] images = labelGenerator.getAsBufferedImages();
83+
for (BufferedImage image : images) {
84+
JLabel label = new JLabel(new ImageIcon(image));
85+
label.setAlignmentX(Component.CENTER_ALIGNMENT);
86+
imagePanel.add(label);
87+
imagePanel.add(Box.createRigidArea(new Dimension(0, 10)));
88+
}
89+
imagePanel.repaint();
90+
imagePanel.revalidate();
91+
});
92+
printButton.addActionListener(_ -> {
93+
try {
94+
labelGenerator.printLabel();
95+
} catch (Exception e) {
96+
JOptionPane.showMessageDialog(null, "An error occurred while printing. More info: " + e, "Error", JOptionPane.ERROR_MESSAGE);
97+
}
98+
});
99+
100+
// Make a JSCrollPane for the image panel
101+
JScrollPane imageScrollPane = new JScrollPane(imagePanel);
102+
103+
// Add buttons to the button panel
104+
buttonPanel.add(refreshButton);
105+
buttonPanel.add(printButton);
71106

72-
// Set up all the tables for the cd
73-
String[] columnNames = {"CD Name", "Artist", "Genre", "Year", "Track Count"};
74-
JTable table = new JTable(new String[][] {new String[] {"None", "", "", "", ""}}, columnNames);
75-
JScrollPane scrollPane = new JScrollPane(table);
107+
// Set up the image panel
108+
imagePanel.setBorder(BorderFactory.createTitledBorder("Preview"));
76109

77-
// Set up the panel
78-
panel.add(scrollPane, BorderLayout.CENTER);
110+
111+
// Add subpanels to main panel
112+
panel.add(imageScrollPane, BorderLayout.CENTER);
113+
panel.add(buttonPanel, BorderLayout.NORTH);
79114

80115
return panel;
81116
}
@@ -589,5 +624,4 @@ private void validateAndSetDoubleField(JTextField field, Consumer<Double> setter
589624
private void showError(String message) {
590625
JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
591626
}
592-
593627
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Application version.
22

33
# MAJOR MINOR PATCH
4-
version=2.0.0
4+
version=2.1.1

0 commit comments

Comments
 (0)