define "not working". does it compile? does it run? do you get a compile time error, runtime errors, what's the error message? or does it just not work the way you expect it to work?
stultuske
Industrious Poster
4,370 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
you're not calling your drawBar method.
public static void main(String[] args)
{
String code = JOptionPane.showInputDialog("Please enter zipCode: ");
BarCode myBar = new BarCode(code);
myBinary = myBar.getCode();
int length= code.length();
if (length != 5 && length!=9)
System.out.println("please enter a valid zipcode code");
else
System.out.println("The is: " + myBinary);
}
the above code is all the code you are actually running.
stultuske
Industrious Poster
4,370 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
Where is the paint() method? In a Swing app, you would call the repaint() method to request the jvm to call the paint method at some time a little later.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
Where is the paint() method you are talking about?
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
What code is supposed to calll your DrawBarCode() method?
Is There a reason for the java program to call it?
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
You don't write your own repaint method. You call it when you want your GUI to be refreshed by having the JVM call the paint methods for your components.
You need to study how to write a GUI program and do your own drawing. Take a look at this:
Click Here
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
Sorry, I don't write code for students. You need to read the tutorial so you understand how to do your own drawing.
Basically you extend a JPanel, override its paintComponent method and add your drawing code to that method.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
The myBinary variable is defined in another class. Since it's static you could use the classname.variablename syntax to access it.
Or make DrawBarCode an inner class of NewClass.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
You are missing some essential pieces to display your barcode.
1. To display something on the screen you need to put it some kind of window, eg a JFrame.
2. The thing you put in the window is a JPanel, but you need a special one because you are going to draw the contents yoursefl.
3. You do that by defining your own class that extends JPanel. Then you create an instance of that and add it to the JFrame.
4. When Java tries to display your panel it will call its paintComponent(Graphics g) method. You have to override this method to do your own customised painting. You have the code for that already, but its in your DrawBarcode method. You need to put that code in your paintComnponent instead.
JamesCherrill
... trying to help
8,513 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30