Alright im working on a program that prints out user imput in a frame, along with a barcode.. it is like the front of an envelope. Here is the description for the program.
This part just explains a check digit.. i think this part in my program is alright
These barcodes actually have four parts: a tall line as the first bar, a series of
tall and short lines corresponding to the digits in the ZIP code, a check digit,
and a tall line on the far right as the last bar. The ZIP code bar dictionary is
shown on the right. The check digit is a number that, when added to the
individual digits of the ZIP code, makes an even multiple of 10. For
example, given the ZIP code 44325, the check digit would be 2.
4 + 4 + 3 + 2 + 5 = 18
18 + 2 = 20
20 is a multiple of 10.
Your job is to take an address and print a label with the barcode (not literally using a printer, but rather in a JFrame). Use three dialog boxes to prompt the user for an address of the form:
First-name Last-name
Street address
City, State ZIP code
Print a label in a small, rectangular JFrame. You should print out the address lines and barcode to look very similar to the label on the first page. The neatness of your output is very important.
Specific Requirements:
· You must have a separate class to draw the bars. Pass in a parameter that indicates whether it is a tall or short bar. Use an if statement in your drawing method that determines how it is drawn.
· You must have a separate class for the address. This class will have functionality to get the three lines of address input from the user, display them on the frame and parse out the ZIP code from the third line. You should have an accessor method for the ZIP code so that you can use it elsewhere (i.e., in another class like main).
· If the any line of the address is missing, send it to the
Dead Letter Box
c/o Local USPS
and print the barcode.
· If there is no ZIP code, (StringTokenizer should give you a count of 3 tokens
before you start taking things out), do not print the barcode. Print an error
message on the frame in lieu of the address.
Return to Sender
Post not deliverable.
· You must make a class that draws the barcode (which means you will need to
pass the ZIP code as a parameter to its constructor). You are required to use a
switch statement to draw the barcode.
And here is all my code.. this post is going to be huge, I'm new here so if there is any way that I could compress it let me know.. right now this is all I know to do
import javax.swing.*;
import java.awt.*;
public class mailComponent extends JComponent
{
int x1;
int x2;
int x3;
int x4;
int x5;
int ZipCode;
private Address label1;
int sum1;
bars one;
public mailComponent()
{
label1 = new Address();
ZipCode = label1.getZipCode();
calcDigits();
CheckSum();
one = new bars();
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
label1.write(g2);
one.drawLong(g2);
drawBars(x1,g2);
drawBars(x2,g2);
drawBars(x3,g2);
drawBars(x4,g2);
drawBars(x5,g2);
drawBars(sum1,g2);
one.drawLong(g2);
}
public void calcDigits()
{
x5 = ZipCode%10;
ZipCode = (ZipCode-x5)/10;
x4 = ZipCode%10;
ZipCode = (ZipCode-x4)/10;
x3 = ZipCode%10;
ZipCode = (ZipCode-x3)/10;
x2 = ZipCode%10;
ZipCode = (ZipCode-x2)/10;
x1 = ZipCode%10;
System.out.println(x1);
System.out.println(x2);
System.out.println(x3);
System.out.println(x4);
System.out.println(x5);
}
public void CheckSum()
{
sum1 = x1+x2+x3+x4+x5;
sum1= sum1%10;
if (sum1 == 0)
sum1 = 0;
else
sum1 = 10-sum1;
}
public void drawBars(int wall, Graphics2D g2)
{
switch (wall)
{
case 1: one.drawShort(g2);
one.drawShort(g2);
one.drawShort(g2);
one.drawLong(g2);
one.drawLong(g2);
break;
case 2: one.drawShort(g2);
one.drawShort(g2);
one.drawLong(g2);
one.drawShort(g2);
one.drawLong(g2);
break;
case 3: one.drawShort(g2);
one.drawShort(g2);
one.drawLong(g2);
one.drawLong(g2);
one.drawShort(g2);
break;
case 4: one.drawShort(g2);
one.drawLong(g2);
one.drawShort(g2);
one.drawShort(g2);
one.drawLong(g2);
break;
case 5: one.drawShort(g2);
one.drawLong(g2);
one.drawShort(g2);
one.drawLong(g2);
one.drawShort(g2);
break;
case 6: one.drawShort(g2);
one.drawLong(g2);
one.drawLong(g2);
one.drawShort(g2);
one.drawShort(g2);
break;
case 7: one.drawLong(g2);
one.drawShort(g2);
one.drawShort(g2);
one.drawShort(g2);
one.drawLong(g2);
break;
case 8: one.drawLong(g2);
one.drawShort(g2);
one.drawShort(g2);
one.drawLong(g2);
one.drawShort(g2);
break;
case 9: one.drawLong(g2);
one.drawShort(g2);
one.drawLong(g2);
one.drawShort(g2);
one.drawShort(g2);
break;
case 0: one.drawLong(g2);
one.drawLong(g2);
one.drawShort(g2);
one.drawShort(g2);
one.drawShort(g2);
}
}
}
import javax.swing.*;
import java.awt.*;
import java.util.StringTokenizer;
public class Address
{
//Class Variables
String s1;
String s2;
String s3;
//Constructor
public Address()
{
String prompt = "first and last name";
s1 = JOptionPane.showInputDialog(prompt);
String prompt2 = "Enter Street Address";
s2 = JOptionPane.showInputDialog(prompt2);
String prompt3 = "City, State Zip";
s3 = JOptionPane.showInputDialog(prompt3);
checkValid();
// noZip();
}
public void write(Graphics2D g2)
{
Font address1 = new Font ("Sans Serif", 0, 20);
g2.setFont(address1);
g2.drawString(s1, 100, 150);
g2.drawString(s2, 38, 110);
g2.drawString(s3, 38, 120);
}
public int getZipCode()
{
StringTokenizer st = new StringTokenizer(s3);
String tristan;
tristan = st.nextToken();
tristan = st.nextToken();
tristan = st.nextToken();
return Integer.parseInt(tristan, 10);
}
public void checkValid()
{
if (s1.equals(""))
{
s1 = "Dead Letter Box";
s2 = "c/o Local USPS";
s3 = "";
}
if (s2.equals(""))
{
s1 = "Dead Letter Box";
s2 = "c/o Local USPS";
s3 = "";
}
}
//this part is commented out because
//I don't know why its not working
/*public boolean noZip()
{
boolean zipCheck = true;
if (s3.equals(""))
{
s1 = "Return to Sender";
s2 = "Post not deliveable";
s3 = null;
zipCheck = false;
}
return zipCheck;
}
*/
}
import javax.swing.*;
import java.awt.*;
import java.awt.Rectangle;
public class bars
{
int x = 50;
public bars()
{
}
public void drawLong(Graphics2D g2)
{
Rectangle big = new Rectangle(x, 200,10, 20);
g2.fill(big);
g2.draw(big);
x = x + 20;
}
public void drawShort(Graphics2D g2)
{
Rectangle small = new Rectangle(x, 200, 10, 10);
g2.fill(small);
g2.draw(small);
x = x + 20;
}
}
import javax.swing.*;
import java.awt.*;
public class MailViewer
{
public static void main (String[] args)
{
JFrame one = new JFrame("Woooorrrrk?!!");
final int FRAME_WIDTH = 800;
final int FRAME_HEIGHT = 600;
one.setSize(FRAME_WIDTH, FRAME_HEIGHT);
one.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
mailComponent two = new mailComponent();
one.add(two);
one.setVisible(true);
}
}
There are my four files, the program works sorta.. I haven't fine tuned where the text appears in the frame yet, I am stressing over getting it to function properly first. I can't get the checking for bad addresses to work right, my brain is fried and its very late if anyone can help me out that would be really awesome.
I'm new to java so if my code looks like an idiot wrote it.. one did haha but please help him out anyway