1010
1111package com .CDPrintable .MusicBrainzResources ;
1212
13+ import javax .swing .*;
14+ import java .awt .*;
15+ import java .awt .image .BufferedImage ;
16+ import java .awt .print .*;
1317import java .util .ArrayList ;
1418
15- public class MusicBrainzLabelGenerator {
16- private ArrayList <MusicBrainzFinalizedRelease > finalizedReleaseList ;
19+ public class MusicBrainzLabelGenerator implements Printable {
20+ private final ArrayList <MusicBrainzFinalizedRelease > finalizedReleaseList ;
21+ public int LABEL_WIDTH ;
22+ public int LABEL_MAX_HEIGHT ;
23+ public int dpiX ;
24+ public int dpiY ;
25+ public int marginTop ;
26+ public int marginBottom ;
27+ public int marginLeft ;
28+ public int marginRight ;
29+ private int fontSize = 10 ;
30+ public double pageWidth = 8.5 ;
31+ public double pageHeight = 11 ;
32+
33+ public int getFontSize () {
34+ return fontSize ;
35+ }
36+
37+ public void setFontSize (int fontSize ) {
38+ this .fontSize = fontSize ;
39+ }
1740
1841 public MusicBrainzLabelGenerator () {
42+ double [] dpi = getDPI ();
43+ this .dpiX = (int ) dpi [0 ];
44+ this .dpiY = (int ) dpi [1 ];
45+
46+ this .LABEL_WIDTH = 4 * dpiX ; // Example: 1 inch width
47+ this .LABEL_MAX_HEIGHT = 2 * dpiY ; // Example: 1 inch height
48+
1949 finalizedReleaseList = new ArrayList <>();
50+ System .out .println ("DPI: dpiX=" + dpiX + ", dpiY=" + dpiY );
51+ System .out .println ("Label dimensions: " + LABEL_WIDTH + "x" + LABEL_MAX_HEIGHT );
52+
53+ double [] margins = getMargins ();
54+ this .marginTop = (int ) margins [0 ];
55+ this .marginBottom = (int ) margins [1 ];
56+ this .marginLeft = (int ) margins [2 ];
57+ this .marginRight = (int ) margins [3 ];
58+ System .out .println ("Margins: " + margins [0 ] + "x" + margins [1 ] + "x" + margins [2 ] + "x" + margins [3 ]);
59+ }
60+
61+ public int getLabelWidth () {
62+ return LABEL_WIDTH ;
63+ }
64+
65+ public void setLabelWidth (int labelWidth ) {
66+ LABEL_WIDTH = labelWidth * dpiX ;
67+ }
68+
69+ public int getLabelMaxHeight () {
70+ return LABEL_MAX_HEIGHT ;
71+ }
72+
73+ public void setLabelMaxHeight (int labelMaxHeight ) {
74+ LABEL_MAX_HEIGHT = labelMaxHeight * dpiY ;
75+ }
76+
77+ @ Override
78+ public int print (Graphics graphics , PageFormat pageFormat , int pageIndex ) {
79+ Graphics2D g2d = (Graphics2D ) graphics ;
80+ g2d .translate (pageFormat .getImageableX (), pageFormat .getImageableY ());
81+ g2d .setColor (Color .BLACK );
82+
83+ Font font = new Font ("Arial" , Font .PLAIN , fontSize );
84+ g2d .setFont (font );
85+ FontMetrics fontMetrics = g2d .getFontMetrics ();
86+
87+ double lineHeight = fontMetrics .getHeight ();
88+ double maxLinesPerPage = Math .floor (((pageHeight * 72 ) - marginTop - marginBottom ) / lineHeight );
89+
90+ ArrayList <ArrayList <String >> releasesAsLines = new ArrayList <>();
91+
92+ for (MusicBrainzFinalizedRelease release : finalizedReleaseList ) {
93+ ArrayList <String > releaseLines = new ArrayList <>();
94+ releaseLines .add (release .getTitle ());
95+ releaseLines .add (release .getArtist ());
96+
97+ StringBuilder lineBuilder = new StringBuilder ();
98+ for (MusicBrainzTrack track : release .getTracks ()) {
99+ String line = track .getTrackNumber () + ". " + track .getTitle () + " " ;
100+ // Split stuff up IF the line gets too long
101+ if (fontMetrics .stringWidth (lineBuilder + line ) > LABEL_WIDTH ) {
102+ releaseLines .add (lineBuilder .toString ());
103+ lineBuilder .delete (0 , lineBuilder .length ());
104+ }
105+ if (releaseLines .size () * fontMetrics .getHeight () >= LABEL_MAX_HEIGHT ) {break ;}
106+ lineBuilder .append (line );
107+ }
108+ if (!lineBuilder .isEmpty ()) {
109+ releaseLines .add (lineBuilder .toString ());
110+ }
111+
112+ releaseLines .add ("" ); // spacer line
113+ releasesAsLines .add (releaseLines );
114+ }
115+
116+ // Group releases into pages
117+ ArrayList <ArrayList <ArrayList <String >>> pages = new ArrayList <>();
118+ ArrayList <ArrayList <String >> currentPage = new ArrayList <>();
119+ int currentLineCount = 0 ;
120+
121+ for (ArrayList <String > releaseLines : releasesAsLines ) {
122+ if (currentLineCount + releaseLines .size () > maxLinesPerPage && !currentPage .isEmpty ()) {
123+ pages .add (currentPage );
124+ currentPage = new ArrayList <>();
125+ currentLineCount = 0 ;
126+ }
127+ currentPage .add (releaseLines );
128+ currentLineCount += releaseLines .size ();
129+ }
130+
131+ if (!currentPage .isEmpty ()) {
132+ pages .add (currentPage );
133+ }
134+
135+ // Handle page out of bounds
136+ if (pageIndex >= pages .size ()) {
137+ return NO_SUCH_PAGE ;
138+ }
139+
140+ // Draw the releases for this page
141+ int y = 0 ;
142+ for (ArrayList <String > releaseLines : pages .get (pageIndex )) {
143+ for (String line : releaseLines ) {
144+ g2d .drawString (line , 0 , y + fontMetrics .getAscent ());
145+ y += fontMetrics .getHeight ();
146+ }
147+ }
148+
149+ return PAGE_EXISTS ;
20150 }
21151
152+
22153 public void addRelease (MusicBrainzFinalizedRelease release ) {
23154 finalizedReleaseList .add (release );
24155 }
25- }
156+
157+ public void printLabel () {
158+ PrinterJob job = PrinterJob .getPrinterJob ();
159+ job .setPrintable (this );
160+
161+ boolean doPrint = job .printDialog ();
162+ if (doPrint ) {
163+ try {
164+ job .print ();
165+ } catch (PrinterException e ) {
166+ e .printStackTrace ();
167+ }
168+ }
169+ }
170+
171+ private double [] getDPI () {
172+ PrinterJob job = PrinterJob .getPrinterJob ();
173+ PageFormat pageFormat = job .defaultPage ();
174+ Paper paper = pageFormat .getPaper ();
175+
176+ // Get the width and height of the paper in points (1 point = 1/72 inch)
177+ double widthInPoints = paper .getWidth ();
178+ double heightInPoints = paper .getHeight ();
179+
180+ System .out .println ("Paper size: " + widthInPoints + "x" + heightInPoints );
181+
182+ // Dynamically calculate the paper size in inches based on the imageable area
183+ double widthInInches = widthInPoints / 72 ;
184+ double heightInInches = heightInPoints / 72 ;
185+
186+ // Calculate DPI
187+ double dpiX = widthInPoints / widthInInches ;
188+ double dpiY = heightInPoints / heightInInches ;
189+
190+ return new double []{dpiX , dpiY };
191+ }
192+
193+ private double [] getMargins () {
194+ PrinterJob job = PrinterJob .getPrinterJob ();
195+ PageFormat pageFormat = job .defaultPage ();
196+ Paper paper = pageFormat .getPaper ();
197+
198+ // Get paper dimensions
199+ double paperWidth = paper .getWidth ();
200+ double paperHeight = paper .getHeight ();
201+
202+ // Get imageable area dimensions
203+ double imageableX = paper .getImageableX ();
204+ double imageableY = paper .getImageableY ();
205+ double imageableWidth = paper .getImageableWidth ();
206+ double imageableHeight = paper .getImageableHeight ();
207+
208+ // Calculate margins
209+ double rightMargin = paperWidth - (imageableX + imageableWidth );
210+ double bottomMargin = paperHeight - (imageableY + imageableHeight );
211+
212+ return new double []{imageableX , rightMargin , imageableY , bottomMargin };
213+ }
214+
215+ public void displayPagesAsImages () {
216+ try {
217+ // Create PrinterJob and PageFormat
218+ PrinterJob job = PrinterJob .getPrinterJob ();
219+ PageFormat pageFormat = job .defaultPage ();
220+ Paper paper = pageFormat .getPaper ();
221+
222+ // Define image dimensions based on paper size
223+ int width = (int ) paper .getWidth ();
224+ int height = (int ) paper .getHeight ();
225+
226+ // Go through each page index until NO_SUCH_PAGE is returned
227+ int pageIndex = 0 ;
228+ while (true ) {
229+ // Create BufferedImage and draw page content
230+ BufferedImage image = new BufferedImage (width , height , BufferedImage .TYPE_INT_ARGB );
231+ Graphics2D g2d = image .createGraphics ();
232+
233+ // White background
234+ g2d .setColor (Color .WHITE );
235+ g2d .fillRect (0 , 0 , width , height );
236+
237+ int result = this .print (g2d , pageFormat , pageIndex );
238+ g2d .dispose ();
239+
240+ // If there's no such page, break out of the loop
241+ if (result != Printable .PAGE_EXISTS ) {
242+ break ;
243+ }
244+
245+ // Show the image in a JOptionPane
246+ ImageIcon icon = new ImageIcon (image );
247+ JOptionPane .showMessageDialog (
248+ null ,
249+ new JLabel (icon ),
250+ "Page Preview - Page " + (pageIndex + 1 ),
251+ JOptionPane .PLAIN_MESSAGE
252+ );
253+
254+ pageIndex ++;
255+ }
256+ } catch (Exception e ) {
257+ e .printStackTrace ();
258+ JOptionPane .showMessageDialog (null , "An error occurred while generating page images." , "Error" , JOptionPane .ERROR_MESSAGE );
259+ }
260+ }
261+
262+ }
0 commit comments