We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,809 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

java help

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

4
Contributors
47
Replies
18 Hours
Discussion Span
1 Year Ago
Last Updated
48
Views
mikekhd
Light Poster
28 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

mikekhd
Light Poster
28 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

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

mikekhd
Light Poster
28 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

mikekhd
Light Poster
28 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

is not Draw

DrawBarCode(Graphics page) is my paint method

mikekhd
Light Poster
28 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

mikekhd
Light Poster
28 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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.

mikekhd
Light Poster
28 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
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;

                        }

                }

     }



}
mikekhd
Light Poster
28 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

mikekhd
Light Poster
28 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

still not calling the DrawBarcode method

mikekhd
Light Poster
28 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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
Moderator
8,513 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

thanks for the comments

mikekhd
Light Poster
28 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1514 seconds using 2.75MB