BarcodeLabel.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. //***************************************************************************
  5. // Represents the panel where the Barcode is drawn.
  6. //
  7. // Author: Ali Badereddin
  8. // Date: September 09, 2005
  9. //
  10. //***************************************************************************
  11. public class BarcodeLabel extends JPanel
  12. {
  13. // Put infront and at end of every barcode
  14. final String quiteZone = "000000000";
  15. // Put after quite zone at the front
  16. final String leftStartCode = "101";
  17. // Put before quite zone at the end
  18. final String rightEndCode = "101";
  19. // Put in the center of every barcode
  20. final String centerCode = "01010";
  21. // Represent the left UPC bits for the digits
  22. final String leftCodes[] = {"0001101", "0011001", "0010011", "0111101",
  23. "0100011", "0110001", "0101111", "0111011",
  24. "0110111", "0001011"};
  25. // Represent the right UPC bits for the digits
  26. final String rightCodes[] = {"1110010", "1100110", "1101100", "1000010",
  27. "1011100", "1001110", "1010000", "1000100",
  28. "1001000", "1110100"};
  29. // represents the number of slices in a UPC-A
  30. final int sliceNum = 113; // 3 + 3 + 5 + (12 * 7) + (9 * 2) = 113
  31. String UPC; // UPC to be converted into barcode
  32. String barcode; // the barcode
  33. boolean valid; // UPC is valid or not?
  34. Color barColor; // The color of the slice corresponding to bit 1
  35. Color spaceColor; // The color of the slice corresponding to bit 0
  36. Color errorColor; // In case the bit is neither 0 or 1 (something wrong)
  37. int x; // Represents the current x coordinate of the slice
  38. int sliceWidth; // Represents the width of the slice
  39. int width; // Width of barcode label
  40. int height; // height of barcode label
  41. //---------------------------------------------------------------------
  42. // Constructor...
  43. //
  44. // sw is the slice width and sh is the slice height.
  45. // bc is the bar color, sc is the space color, and ec is error color.
  46. //---------------------------------------------------------------------
  47. public BarcodeLabel (int sw, int sh, Color bc, Color sc, Color ec)
  48. {
  49. // Upc is initially invalid
  50. valid = false;
  51. // Set x initially to zero
  52. x = 0;
  53. // Set the slicewidth
  54. sliceWidth = sw;
  55. // Set the width of barcode label
  56. width = sliceNum * sw;
  57. // Set the height of barcode label
  58. height = sh;
  59. // Set the colors
  60. barColor = bc;
  61. spaceColor = sc;
  62. errorColor = ec;
  63. // Set the background color to space color
  64. this.setBackground (spaceColor);
  65. // Set the preferred size of the barcode label
  66. this.setPreferredSize (new Dimension (width, height));
  67. }
  68. //---------------------------------------------------------------------
  69. // Paints the barcode only if it is valid...
  70. //---------------------------------------------------------------------
  71. public void paintComponent (Graphics page)
  72. {
  73. super.paintComponent (page);
  74. // Clear the barcode before drawing
  75. page.setColor (getBackground ());
  76. page.fillRect (0, 0, width, height);
  77. // Draw the barcode only if the UPC is valid
  78. if (isValid ())
  79. drawBarcode (page);
  80. }
  81. //---------------------------------------------------------------------
  82. // Draws the barcode if it is not null.
  83. //---------------------------------------------------------------------
  84. public void drawBarcode (Graphics page)
  85. {
  86. int barcodeLength;
  87. // Set x to zero
  88. x = 0;
  89. if (barcode != null)
  90. {
  91. // Get the barcode length
  92. barcodeLength = barcode.length ();
  93. // Loop over every character (0 or 1)
  94. for (int i = 0; i < barcodeLength; i++)
  95. {
  96. // Draw a white slice for the 0
  97. if (barcode.charAt (i) == '0')
  98. {
  99. page.setColor (spaceColor);
  100. }
  101. // Draw a black slice for the '1'
  102. else if (barcode.charAt (i) == '1')
  103. {
  104. page.setColor (barColor);
  105. }
  106. // Draw a red slice to show something graphically
  107. else
  108. {
  109. page.setColor (errorColor);
  110. }
  111. // Update the coordinates
  112. x += sliceWidth;
  113. // Draw the slices at the specified coordinates
  114. page.fillRect (x, 0, sliceWidth, height);
  115. }
  116. }
  117. }
  118. //---------------------------------------------------------------------
  119. // Set the UPC that will be converted into barcode.
  120. //---------------------------------------------------------------------
  121. public void setUPC (String c)
  122. {
  123. UPC = c;
  124. }
  125. //---------------------------------------------------------------------
  126. // Get the UPC that will be converted into barcode.
  127. //---------------------------------------------------------------------
  128. public String getUPC ()
  129. {
  130. return UPC;
  131. }
  132. //---------------------------------------------------------------------
  133. // Checks if the UPC is valid. a UPC is valid if it consists
  134. // of exactly 12 digits.
  135. //---------------------------------------------------------------------
  136. public void validateUPC ()
  137. {
  138. valid = UPC.matches ("[0-9]{12}?");
  139. }
  140. //---------------------------------------------------------------------
  141. // Tells whether that UPC is valid.
  142. //---------------------------------------------------------------------
  143. public boolean isValid ()
  144. {
  145. return valid;
  146. }
  147. //---------------------------------------------------------------------
  148. // Sets valid to v.
  149. //---------------------------------------------------------------------
  150. public void setValid (boolean v)
  151. {
  152. valid = v;
  153. repaint ();
  154. }
  155. //---------------------------------------------------------------------
  156. // Helper function to convert a character to an integer.
  157. //---------------------------------------------------------------------
  158. public int charToInteger (char c)
  159. {
  160. return c - 48;
  161. }
  162. //---------------------------------------------------------------------
  163. // Generate the barcode from the UPC...
  164. //---------------------------------------------------------------------
  165. public void generateBarcode ()
  166. {
  167. if (isValid ())
  168. {
  169. barcode = quiteZone + leftStartCode +
  170. leftCodes[charToInteger (UPC.charAt (0))] +
  171. leftCodes[charToInteger (UPC.charAt (1))] +
  172. leftCodes[charToInteger (UPC.charAt (2))] +
  173. leftCodes[charToInteger (UPC.charAt (3))] +
  174. leftCodes[charToInteger (UPC.charAt (4))] +
  175. leftCodes[charToInteger (UPC.charAt (5))] +
  176. centerCode +
  177. rightCodes[charToInteger (UPC.charAt (6))] +
  178. rightCodes[charToInteger (UPC.charAt (7))] +
  179. rightCodes[charToInteger (UPC.charAt (8))] +
  180. rightCodes[charToInteger (UPC.charAt (9))] +
  181. rightCodes[charToInteger (UPC.charAt (10))] +
  182. rightCodes[charToInteger (UPC.charAt (11))] +
  183. rightEndCode + quiteZone;
  184. repaint ();
  185. }
  186. }
  187. //---------------------------------------------------------------------
  188. // Checks out my work...
  189. //---------------------------------------------------------------------
  190. public static void main (String[] args)
  191. {
  192. JFrame frame = new JFrame ("Test");
  193. frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  194. // Create barcode
  195. BarcodeLabel barcodeLabel = new BarcodeLabel (4, 200, Color.blue, Color.yellow, Color.red);
  196. // Set the UPC of the barcode label
  197. barcodeLabel.setUPC ("314159265358");
  198. // Check the UPC of the barcode label
  199. barcodeLabel.validateUPC ();
  200. // Create the barcode
  201. barcodeLabel.generateBarcode ();
  202. frame.setBackground (Color.white);
  203. frame.getContentPane().add (barcodeLabel);
  204. frame.pack();
  205. frame.setVisible (true);
  206. }
  207. }