ASBarcode.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. //***************************************************************************
  5. // This is the main panel... ASBarcode stands for ASTI SPUMANTE Barcode.
  6. //
  7. // Author: Ali Badereddin
  8. // Date: September 09, 2005
  9. //
  10. //***************************************************************************
  11. public class ASBarcode extends JPanel
  12. {
  13. // Represents the orange color used at code project
  14. final Color codeProjectOrange = new Color (255, 153, 0);
  15. // Declare the barcode label
  16. BarcodeLabel barcodeLabel;
  17. // Declare the panel that will hold the barcode label
  18. JPanel barcodePanel;
  19. // Declare the buttons
  20. JButton generateCheckDigitButton;
  21. JButton generateBarcodeButton;
  22. // Declare the panel that will hold the buttons
  23. JPanel buttonPanel;
  24. // Declare the UPC field
  25. UPCField upcField;
  26. // Declare the panel that will hold the UPC field
  27. JPanel upcPanel;
  28. //*********************************************************************
  29. // Contructor.
  30. //*********************************************************************
  31. public ASBarcode ()
  32. {
  33. // Create the Barcode Label with a slice width of 2 pixels and a
  34. // slice height of 150 pixels.
  35. barcodeLabel = new BarcodeLabel (2, 150, Color.black, codeProjectOrange, Color.red);
  36. // Create the barcode panel and add the barcode label to it
  37. barcodePanel = new JPanel ();
  38. barcodePanel.add (barcodeLabel);
  39. // Create the buttons
  40. generateCheckDigitButton = new JButton ("Generate Check Digit");
  41. generateBarcodeButton = new JButton ("Generate Bar Code");
  42. // Set the buttons background color to orange
  43. generateCheckDigitButton.setBackground (codeProjectOrange);
  44. generateBarcodeButton.setBackground (codeProjectOrange);
  45. // Add action listeners to the buttons
  46. generateCheckDigitButton.addActionListener (new CheckDigitListener ());
  47. generateBarcodeButton.addActionListener (new BarcodeListener ());
  48. // The buttons are initially disabled
  49. generateCheckDigitButton.setEnabled (false);
  50. generateBarcodeButton.setEnabled (false);
  51. // Add the buttons to the panel
  52. buttonPanel = new JPanel ();
  53. buttonPanel.add (generateCheckDigitButton);
  54. buttonPanel.add (generateBarcodeButton);
  55. // Create the UPC field
  56. upcField = new UPCField (generateCheckDigitButton, generateBarcodeButton);
  57. upcField.setBackground (codeProjectOrange);
  58. // Create the UPC panel and add the upc field to it
  59. upcPanel = new JPanel ();
  60. JLabel enterUPCLabel = new JLabel ("Enter UPC ");
  61. enterUPCLabel.setForeground (codeProjectOrange);
  62. upcPanel.add (enterUPCLabel);
  63. upcPanel.add (upcField);
  64. // Set the layout of the main panel to BorderLayout
  65. setLayout (new BorderLayout ());
  66. // Set the color of the panels
  67. upcPanel.setBackground (Color.black);
  68. barcodePanel.setBackground (Color.black);
  69. buttonPanel.setBackground (Color.black);
  70. // Add the upc field in the North
  71. add (upcPanel, BorderLayout.NORTH);
  72. // Add the barcode label at the center
  73. add (barcodePanel, BorderLayout.CENTER);
  74. // Add the buttons in the South
  75. add (buttonPanel, BorderLayout.SOUTH);
  76. }
  77. //*********************************************************************
  78. // Generates the check digit of the UPC-A.
  79. //*********************************************************************
  80. private class CheckDigitListener implements ActionListener
  81. {
  82. public void actionPerformed (ActionEvent e)
  83. {
  84. // Generate the check digit and append it at the end of the
  85. // UPC field.
  86. upcField.generateCheckDigit ();
  87. }
  88. }
  89. //*********************************************************************
  90. // Generates the barcode based on the UPC-A.
  91. //*********************************************************************
  92. private class BarcodeListener implements ActionListener
  93. {
  94. public void actionPerformed (ActionEvent e)
  95. {
  96. // Generate the barcode only if the check digit is valid
  97. if (upcField.isCheckDigitValid ())
  98. {
  99. // Set the UPC of the barcode label
  100. barcodeLabel.setUPC (upcField.getUPC());
  101. // Validate the UPC of the barcode label
  102. barcodeLabel.validateUPC ();
  103. // Generate the barcode
  104. barcodeLabel.generateBarcode ();
  105. // Select the text in the upc field
  106. upcField.selectAll ();
  107. }
  108. else
  109. {
  110. // Make the barcode as invalid so that nothing would be
  111. // drawn on the barcode panel.
  112. // Check BarcodeLabel.paintComponent
  113. barcodeLabel.setValid (false);
  114. }
  115. }
  116. }
  117. //---------------------------------------------------------------------
  118. // Check out my work..
  119. //---------------------------------------------------------------------
  120. public static void main (String[] args)
  121. {
  122. // Create and set up the window.
  123. JFrame frame = new JFrame("ASTI SPUMANTE BAR-CODE");
  124. frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  125. frame.getContentPane().add (new ASBarcode ());
  126. // Display the window.
  127. frame.pack();
  128. frame.setVisible (true);
  129. }
  130. }