import java.applet.*; import java.awt.*; import java.awt.event.*; public class BCApplet extends Applet implements ActionListener { TextField textField, chkField; TextArea display, bcode; String newLine; String bc[] = new String[128]; int cksum, ck2sum; public void init() { // initialize Bar Code array bc[] bc[1] = "212222 "; bc[2] = "222122 "; bc[3] = "222221 "; bc[4] = "121223 "; bc[5] = "121322 "; bc[6] = "131222 "; bc[7] = "122213 "; bc[8] = "122312 "; bc[9] = "132212 "; bc[10] = "221213 "; bc[11] = "221312 "; bc[12] = "231212 "; bc[13] = "112232 "; bc[14] = "122132 "; bc[15] = "112232 "; bc[16] = "113222 "; bc[17] = "123122 "; bc[18] = "123221 "; bc[19] = "223211 "; bc[20] = "221132 "; bc[21] = "221231 "; bc[22] = "213212 "; bc[23] = "223112 "; bc[24] = "312131 "; bc[25] = "311222 "; bc[26] = "321122 "; bc[27] = "321221 "; bc[28] = "312212 "; bc[29] = "322112 "; bc[30] = "322211 "; bc[31] = "212123 "; bc[32] = "212321 "; bc[33] = "232121 "; bc[34] = "111323 "; bc[35] = "131123 "; bc[36] = "131321 "; bc[37] = "112313 "; bc[38] = "132113 "; bc[39] = "132311 "; bc[40] = "211313 "; bc[41] = "231113 "; bc[42] = "231311 "; bc[43] = "112133 "; bc[44] = "112331 "; bc[45] = "132131 "; bc[46] = "113123 "; bc[47] = "113321 "; bc[48] = "133121 "; bc[49] = "313121 "; bc[50] = "211331 "; bc[51] = "231131 "; bc[52] = "213113 "; bc[53] = "213311 "; bc[54] = "213131 "; bc[55] = "311123 "; bc[56] = "311321 "; bc[57] = "331121 "; bc[58] = "312113 "; bc[59] = "312311 "; bc[60] = "332111 "; bc[61] = "314111 "; bc[62] = "221411 "; bc[63] = "431111 "; bc[64] = "111224 "; bc[65] = "111422 "; bc[66] = "121124 "; bc[67] = "121421 "; bc[68] = "141122 "; bc[69] = "141221 "; bc[70] = "112214 "; bc[71] = "112412 "; bc[72] = "122114 "; bc[73] = "122411 "; bc[74] = "142112 "; bc[75] = "142211 "; bc[76] = "241211 "; bc[77] = "221114 "; bc[78] = "413111 "; bc[79] = "241112 "; bc[80] = "134111 "; bc[81] = "111242 "; bc[82] = "121142 "; bc[83] = "121241 "; bc[84] = "114212 "; bc[85] = "124112 "; bc[86] = "124211 "; bc[87] = "411212 "; bc[88] = "421112 "; bc[89] = "421211 "; bc[90] = "212141 "; bc[91] = "214121 "; bc[92] = "412121 "; bc[93] = "111143 "; bc[94] = "111341 "; bc[95] = "131141 "; bc[96] = "114113 "; bc[97] = "114311 "; bc[98] = "411113 "; bc[99] = "411311 "; bc[100]= "113141 "; bc[101]= "114131 "; bc[102]= "311141 "; bc[103]= "411131 "; bc[104]= "211412 "; bc[105]= "211214 "; // Start Code B bc[106]= "211232 "; // bc[107]= "2331112"; // Stop // set up graphics setLayout(new BorderLayout()); setBackground(Color.cyan); Panel p1 = new Panel(); // Add -> BarCode Button to panel startButton = new Button("-> BarCode"); startButton.setBackground(Color.white); startButton.addActionListener(new StartListener()); p1.add(startButton); // Add -> ASCII Button to panel start2Button = new Button("-> ASCII"); start2Button.setBackground(Color.white); start2Button.addActionListener(new Start2Listener()); p1.add(start2Button); // Add Stop Button to panel stopButton = new Button("Stop"); stopButton.setBackground(Color.white); stopButton.addActionListener(new StopListener()); p1.add(stopButton); // Add Clear Button to panel clearButton = new Button("Clear"); clearButton.setBackground(Color.white); clearButton.addActionListener(new ClearListener()); p1.add(clearButton); // Add Test Button to panel testButton = new Button("Test"); testButton.setBackground(Color.white); testButton.addActionListener(new TestListener()); p1.add(testButton); // Add checksum label to panel chksumLabel = new Label(" Check Sum"); chksumLabel.setForeground(Color.black); p1.add(chksumLabel); // Add checksum display to panel chkField = new TextField(8); chkField.setForeground(Color.black); chkField.setBackground(Color.lightGray); chkField.setEditable(false); p1.add(chkField); // define text areas textField = new TextField(40); display = new TextArea(20, 30); bcode = new TextArea(20, 70); textField.setBackground(Color.white); display.setBackground(Color.lightGray); bcode.setBackground(Color.lightGray); display.setEditable(false); bcode.setEditable(false); // use GridBagLAyout in Applet GridBagLayout gridBag = new GridBagLayout(); setLayout(gridBag); GridBagConstraints c = new GridBagConstraints(); // format panel and add to Applet c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.HORIZONTAL; gridBag.setConstraints(p1, c); add(p1); // format textfield and add to Applet c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.HORIZONTAL; gridBag.setConstraints(textField, c); add(textField); // format left display (display) and add to Applet c.gridwidth = GridBagConstraints.RELATIVE; c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.WEST; gridBag.setConstraints(display, c); add(display); // format right display (bcode) and to Applet c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.EAST; gridBag.setConstraints(bcode, c); add(bcode); // start applet textField.addActionListener(this); newLine = System.getProperty("line.separator"); } class StartListener implements ActionListener { public void actionPerformed(ActionEvent evt) { startButton.setEnabled(false); start2Button.setEnabled(false); stopButton.setEnabled(true); clearButton.setEnabled(true); testButton.setEnabled(true); String tLine = textField.getText(); int value = 104; String bLine = bc[value+1]; // start code = 104 int i = 0; cksum = 104; while (i < tLine.length()) { value = +tLine.charAt(i) - 32; bLine = bLine + bc[value+1]; cksum = cksum + (i+1)*(value); ck2sum = cksum % 103; i++; } display.append(tLine + "\n"); bcode.append(bLine + "\n"); textField.setText(""); // chkField.setText(new Integer(ck2sum).toString()); chkField.setText(bc[ck2sum+1]); startButton.setEnabled(true); start2Button.setEnabled(true); } } class Start2Listener implements ActionListener { public void actionPerformed(ActionEvent evt) { startButton.setEnabled(false); start2Button.setEnabled(false); stopButton.setEnabled(true); clearButton.setEnabled(true); testButton.setEnabled(true); //convert.start(); } } class StopListener implements ActionListener { public void actionPerformed(ActionEvent evt) { startButton.setEnabled(true); stopButton.setEnabled(false); clearButton.setEnabled(true); testButton.setEnabled(true); //convert.stop(); } } class ClearListener implements ActionListener { public void actionPerformed(ActionEvent evt) { startButton.setEnabled(true); stopButton.setEnabled(true); clearButton.setEnabled(true); testButton.setEnabled(true); textField.setText(""); chkField.setText(""); display.setText(""); bcode.setText(""); } } class TestListener implements ActionListener { public void actionPerformed(ActionEvent evt) { startButton.setEnabled(true); stopButton.setEnabled(true); clearButton.setEnabled(true); testButton.setEnabled(true); textField.setText("BarCode 1"); chkField.setText("111323"); String tLine = textField.getText(); String bLine = "211214 131123 121124 121241 131321 134111 " + "141221 112214 212222 123221 111323"; display.append(tLine + "\n"); bcode.append(bLine + "\n"); startButton.setEnabled(true); start2Button.setEnabled(true); } } public void actionPerformed(ActionEvent evt) { String text = textField.getText(); int value = 104; String bLine = bc[value+1]; // start code = 104 int i = 0; cksum = 104; ck2sum = 0; while (i < text.length()) { value = +text.charAt(i) - 32; bLine = bLine + bc[value+1]; cksum = cksum + (i+1)*(value); ck2sum = cksum % 103; i++; } display.append(text + newLine); bcode.append(bLine + "\n"); chkField.setText(bc[ck2sum+1]); textField.selectAll(); } public void destroy() { } private Button startButton; private Button start2Button; private Button stopButton; private Button clearButton; private Button testButton; private Label chksumLabel; private int i=0; }