mport java.awt.Color;
import java.awt.Graphics;
import javax.swing.JOptionPane;



why this is not working need help

public class NewClass
{  static String myBinary;

   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);

   }

          public void  DrawBarCode(Graphics page){

                int x = 200;
                int y = 300;
        int barWidth = 6;  //  Calculate a width of the bar
        int fullBar = 32;   // Calculate the full length bar
        int halfBar = 16;   //  Calculate the half length bar
        int barGap = 6;  // Calculate space between bars
            Graphics graphicsBarCode = page.create();
                graphicsBarCode.setColor(Color.yellow);
                graphicsBarCode.fillRect(x,y,320,30);
                graphicsBarCode.setColor(Color.black);

        char nextChar;

        for(int i = 0; i < myBinary.length(); i++)
        {
            nextChar = myBinary.charAt(i);

            if(nextChar == '0')
            {
            page.fillRect(x, y, barWidth, halfBar); 
            }
            else  //  
            {
               page.fillRect(x, y, barWidth, fullBar);
                       x += barGap;

                        }






}
}
}
Quoted Text Here

need help it prints binary code but not graphics why . very new to java

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?

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.

thanks for comments, I do not get any error, its not calling the paint method. How i supposed call the method.

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.

hey guys you are been supportive. some start up coding will help alot

Where is the paint() method you are talking about?

is not Draw

DrawBarCode(Graphics page) is my paint method

What code is supposed to calll your DrawBarCode() method?
Is There a reason for the java program to call it?

I think i need a repaint() method but how i do that

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

yup I know i need more studies to write a code. But i need this done. if you could tell me in above code where and what i need to do, it will be great help.

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.

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JOptionPane;


/**
   This is a test driver for BarCode and Digit class
*/
public class NewClass
{  static String myBinary;

   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);

   }


}

    class DrawBarcode extends JOptionPane{


    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

}
    public void draw(Graphics g)
    {

                int x = 200;
                int y = 300;
        int barWidth = 6;  //  Calculate a width of the bar
        int fullBar = 32;   // Calculate the full length bar
        int halfBar = 16;   //  Calculate the half length bar
        int barGap = 6;  // Calculate space between bars
            Graphics graphicsBarCode = g.create();
                graphicsBarCode.setColor(Color.yellow);
                graphicsBarCode.fillRect(x,y,320,30);
                graphicsBarCode.setColor(Color.black);

        char nextChar;

        for(int i = 0; i < myBinary.length(); i++)
        {
                nextChar = myBinary.charAt(i);

            if(nextChar == '0')
            {
            g.fillRect(x, y, barWidth, halfBar);    
            }
            else   
            {
               g.fillRect(x, y, barWidth, fullBar);
                       x += barGap;

                        }

                }

     }



}

I tried to extend the JPanel and override it .Now it says Cannot find symbol variable myBinary at class DrawBarcode. any suggestions please

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.

still not calling the DrawBarcode method

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.

thanks for the comments

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


/**
   This is a test driver for BarCode and Digit class
*/
public class NewClass
{  
    static String myBinary;

   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);

   }


}

    class DrawBarcode extends JFrame{
    private JPanel panel;
    final int W = 900, H = 640;



     public DrawBarcode(){

        setSize(W,H);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        getContentPane().add(panel);

        panel = new JPanel();
        panel.setBounds(0,0,W,H);


        class myPanel extends JPanel {
        public myPanel() {
            super();   
        }
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
          super.paintComponent(g);

                int x = 200;
                int y = 300;
        int barWidth = 6;  //  Calculate a width of the bar
        int fullBar = 32;   // Calculate the full length bar
        int halfBar = 16;   //  Calculate the half length bar
        int barGap = 6;  // Calculate space between bars
            Graphics graphicsBarCode = g.create();
                graphicsBarCode.setColor(Color.yellow);
                graphicsBarCode.fillRect(x,y,320,30);
                graphicsBarCode.setColor(Color.black);

           char nextChar;
                  int length = NewClass.myBinary.length();
        for(int i = 0; i <length; i++)
        {
                nextChar = NewClass.myBinary.charAt(i);

            if(nextChar == '0')
            {
            g.fillRect(x, y, barWidth, halfBar);    
            }
            else   
            {
               g.fillRect(x, y, barWidth, fullBar);
                       x += barGap;

                        }

                }

     }
      }     


}
    }

need more help, what i am doing worng it is still not Calling the paint coponent

Where do you create an instance of the class that displays the GUI?

       JFrame frame = new JFrame ("barcode");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      barcodePanel panel = new barcodePanel();

      frame.getContentPane().add(panel);

      frame.pack();
      frame.setVisible(true);

i got this into separate file in same folder

What is that separate file for? It is not a java class. You can not compile and execute it.

i

import javax.swing.JFrame;

public class BarcodePanel
{
   //-----------------------------------------------------------------
   //  Creates the main frame of the program.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      JFrame frame = new JFrame ("BARCODE");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

     BarcodePanel panel = new BarcodePanel();

      frame.getContentPane().add(frame);

      frame.pack();
      frame.setVisible(true);
   }
}

here is all of it chage lil bit

What happens when you compile and execute it?
Copy and paste here the full text of the error messages.

The is: 1100011000110001100011000110001
BUILD SUCCESSFUL (total time: 5 seconds); no error nothing. I never wrote so many code together. use t small separte file for each class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.