-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUPCDraw.java
175 lines (137 loc) · 3.95 KB
/
UPCDraw.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//Draws a label using a UPC number
//Emerson Racca
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.Scanner;
public class UPCDraw extends JPanel{
private static int[] upc;
private String upcString;
private int x, y;
public UPCDraw(String bc){
x = 30;
y = 30;
upc = new int[12];
upcString = bc;
int bcBit;
for(int c = 0; c < 12; c++){
bcBit = Integer.parseInt(bc.substring(c, c + 1));
upc[c] = bcBit;
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int high = 30;
int colCount = 0;
String patternCode;
String bitCode;
//bar left
for(int c = 1; c <= 3; c++){
if(c % 2 == 1)
g.setColor(Color.black);
else
g.setColor(Color.white);
g.drawLine(x + colCount, y, x + colCount, y + high);
colCount++;
}
//info left
for(int c = 0; c < 6; c++){
patternCode = getBitPattern("l", upc[c]);
System.out.println(patternCode);
for(int i = 0; i < 7; i++){
bitCode = patternCode.substring(i, i + 1);
if(bitCode.equals("b")) g.setColor(Color.black);
if(bitCode.equals("w")) g.setColor(Color.white);
g.drawLine(x + colCount, y, x + colCount, y + high);
colCount++;
}
}
//bar middle
for(int c = 0; c < 5; c++){
if(c % 2 == 1)
g.setColor(Color.black);
else
g.setColor(Color.white);
g.drawLine(x + colCount, y, x + colCount, y + high);
colCount++;
}
//info right and check digit
for(int c = 0; c < 6; c++){
patternCode = getBitPattern("r", upc[c + 6]);
System.out.println(patternCode);
for(int i = 0; i < 7; i++){
bitCode = patternCode.substring(i, i + 1);
if(bitCode.equals("b")) g.setColor(Color.black);
if(bitCode.equals("w")) g.setColor(Color.white);
g.drawLine(x + colCount, y, x + colCount, y + high);
colCount++;
}
}
//bar right
for(int c = 1; c <= 3; c++){
if(c % 2 == 1)
g.setColor(Color.black);
else
g.setColor(Color.white);
g.drawLine(x + colCount, y, x + colCount, y + high);
colCount++;
}
g.drawRect(x - 5, y - 5, 105, 55);
g.drawString(upcString, x + 5, y + 45);
System.out.println(colCount); //should be 95 columns
}
public int getBit(int loc){
return upc[loc];
}
private String getBitPattern(String s, int bit){
String bitPattern = "";
//left side
if(s.equals("l")){
if(bit == 0) bitPattern = "wwwbbwb";
if(bit == 1) bitPattern = "wwbbwwb";
if(bit == 2) bitPattern = "wwbwwbb";
if(bit == 3) bitPattern = "wbbbbwb";
if(bit == 4) bitPattern = "wbwwwbb";
if(bit == 5) bitPattern = "wbbwwwb";
if(bit == 6) bitPattern = "wbwbbbb";
if(bit == 7) bitPattern = "wbbbwbb";
if(bit == 8) bitPattern = "wbbwbbb";
if(bit == 9) bitPattern = "wwwbwbb";
}
//right side
if(s.equals("r")){
if(bit == 0) bitPattern = "bbbwwbw";
if(bit == 1) bitPattern = "bbwwbbw";
if(bit == 2) bitPattern = "bbwbbww";
if(bit == 3) bitPattern = "bwwwwbw";
if(bit == 4) bitPattern = "bwbbbww";
if(bit == 5) bitPattern = "bwwbbbw";
if(bit == 6) bitPattern = "bwbwwww";
if(bit == 7) bitPattern = "bwwwbww";
if(bit == 8) bitPattern = "bwwbwww";
if(bit == 9) bitPattern = "bbbwbww";
}
return bitPattern;
}
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.print("Enter a barcode: ");
String UPC = reader.nextLine();
//String UPC = "718103124775";
JFrame GUI = new JFrame();
//std
GUI.setSize(200, 200);
GUI.setTitle("UPC Draw");
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = GUI.getContentPane();
UPCDraw drawBC = new UPCDraw(UPC);
pane.add(drawBC);
GUI.setVisible(true);
/*
//test the method for data verification
for(int c = 0; c < 12; c++){
System.out.println("upc[" + c + "] = " + drawBC.getBit(c));
}
*/
}
}