Results 1 to 13 of 13
  1. #1
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Default Runs fine in command prompt but not in NetBeans??

    I am getting multiple errors that I have not seen since I started trying to create this pie chart for an applet I am building.

    Java Code:
    package applet1;
     
    import java.awt.Color;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.geom.Arc2D;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Point2D;
    import java.util.ArrayList;
     
    import javax.swing.JPanel;
     
    public class NewClass extends JPanel {
     
        enum Type {
            STANDARD, SIMPLE_INDICATOR, GRADED_INDICATOR
        }
     
        private Type type = null; //the type of pie chart
     
        private ArrayList values;
        private ArrayList colors;
     
        private ArrayList gradingValues;
        private ArrayList gradingColors;
     
        double percent = 0; //percent is used for simple indicator and graded indicator
     
        public NewClass(int percent) {
     
            type = Type.SIMPLE_INDICATOR;
            this.percent = percent;
        }
     
        public NewClass(ArrayList values, ArrayList colors) {
     
            type = Type.STANDARD;
     
            this.values = values;
            this.colors = colors;
        }
     
        public NewClass(int percent, ArrayList gradingValues, ArrayList gradingColors) {
            type = Type.GRADED_INDICATOR;
     
            this.gradingValues = gradingValues;
            this.gradingColors = gradingColors;
            this.percent = percent;
     
        }
     
        @Override
        protected void paintComponent(Graphics g) {
     
            int width = getSize().width;
     
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias!
                    RenderingHints.VALUE_ANTIALIAS_ON);
     
            if (type == Type.SIMPLE_INDICATOR) {
     
                //colours used for simple indicator
                Color backgroundColor = Color.WHITE;
                Color mainColor = Color.BLUE;
     
                g2d.setColor(backgroundColor);
                g2d.fillOval(0, 0, width, width);
                g2d.setColor(mainColor);
                Double angle = (percent / 100) * 360;
                g2d.fillArc(0, 0, width, width, -270, -angle.intValue());
     
            } else if (type == Type.STANDARD) {
     
                int lastPoint = -270;
     
                for (int i = 0; i < values.size(); i++) {
                    g2d.setColor(colors.get(i)); //here it says actual object cannot be converted to color by invocation method
     
                    Double val = (Double) values.get(i);
                    Double angle = (val / 100) * 360;
     
                    g2d.fillArc(0, 0, width, width, lastPoint, -angle.intValue());
                    System.out.println("fill arc " + lastPoint + " "
                            + -angle.intValue());
     
                    lastPoint = lastPoint + -angle.intValue();
                }
            } else if (type == Type.GRADED_INDICATOR) {
     
                int lastPoint = -270;
     
                double gradingAccum = 0;
     
                for (int i = 0; i < gradingValues.size(); i++) { //Says assigned value "i" is never used??         
           g2d.setColor(gradingColors.get(i));                          
           Double val = (Double) gradingValues.get(i);           
       gradingAccum = gradingAccum + val;                              Double angle = null;                                /**                  * If the sum of the gradings is greater than the percent, then we want to recalculate               * the last wedge, and break out of drawing.                 */                                 if (gradingAccum > percent) {
     
                        System.out.println("gradingAccum > percent");
     
                        //get the previous accumulated segments. Segments minus last one
                        double gradingAccumMinusOneSegment = gradingAccum - val;
     
                        //make an adjusted calculation of the last wedge
                        angle = ((percent - gradingAccumMinusOneSegment) / 100) * 360;
     
                        g2d.fillArc(0, 0, width, width, lastPoint, -angle.intValue());
     
                        lastPoint = lastPoint + -angle.intValue();
     
                        break;
     
                    }else {
     
                        System.out.println("normal");
                        angle = (val / 100) * 360;
     
                        g2d.fillArc(0, 0, width, width, lastPoint, -angle.intValue());
     
                        System.out.println("fill arc " + lastPoint + " "
                                + -angle.intValue());
     
                        lastPoint = lastPoint + -angle.intValue();
                    }
                }
            }
        }
    }
    It is telling me that g2d.setColor(colors.get(i));, cannot be converted to color by invocation method.
    It is also complaining about my for loop farther down in the code, saying that the assigned value for the increment variable is never used??
    I am also getting a box that pops up when I first open NetBeans that says there were reference problems of some sort. It is claiming that one or more project resources cannot be found, and gives me the option of resolving this issue by finding them myself, but I have no idea??
    I have attached a screenshot of the box here,Runs fine in command prompt but not in NetBeans??-reference-error.jpg and am hoping that someone can tell me what to do about it, as well as explaining to me what I am doing wrong with my coding here!!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Runs fine in command prompt but not in NetBeans??

    g2d.setColor(colors.get(i));
    When I compile your code the compiler doesn't like this line:

    Java Code:
    C:\Users\Educator\android-workspace\ntj\src>javac -cp . applet1\NewClass.java
    applet1\NewClass.java:81: error: method setColor in class Graphics cannot be applied to given types;
    
                    g2d.setColor(colors.get(i)); //here it says actual object cannot be converted to col
    or by invocation method
                       ^
      required: Color
      found: Object
      reason: actual argument Object cannot be converted to Color by method invocation conversion
    1 error
    As the compiler says, it wanted a Color but found an Object. The problem is that colors.get(i) returns a reference of type Object, not Color. And setColor() demands something the compiler knows to be a Color.

    There are two ways to proceed: (1) Declare colors to be ArrayList<Color> rather than ArrayList. This use of generics will mean that the compiler will understand that colors.get(i) returns a Color and not just any old Object.

    Or, if you are unused to using generics (although this sort of situation is what they are made for) then you could (2) cast the returned Object to Color.

    Java Code:
    g2d.setColor((Color)colors.get(i));
    Then the compiler will be quiet and accept your assurance that the array list really is returning a Color. Of course you are liable to get a runtime exception if, for any reason, the array list contains something that is not a Color.

    ---

    I have no idea for the difference in runtime behaviour between NB and the command line (console). In part because what you posted isn't runnable. Ie doesn't have a main() method.

    In fact, as posted your code will not compile. So it is quite impossible for that code to be run anywhere: NB, console or anywhere else. If you are running something from the command line it is some other (earlier?) version of the code.

    ---

    As far as the NB specific stuff goes, sorry I don't really have a lot of experience with NB. You could click the "resolve" button and play about in whatever labyrinth it leads you to. Personally I would look about the menus for some way to "clean" or "rebuild" the project. Also if the IDE is getting upset about "project resources" you could try creating a whole new project and copying the existing code into it.
    Last edited by pbrockway2; 02-02-2013 at 03:18 AM.

  3. #3
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Angry Re: Runs fine in command prompt but not in NetBeans??

    Quote Originally Posted by pbrockway2 View Post
    I have no idea for the difference in run-time behavior between NB and the command line (console). In part because what you posted isn't runnable. Ie doesn't have a main() method.
    No, this particular code is not runnable, but I am getting the same hassles when I run the code for just the pie chart which I did separately, in Notepad++!!
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import java.text.*;
    import javax.swing.*;
     
    public class PieChartExample
    {
        public static void main(String[] args)
        {
            JFrame f = new JFrame("Pie Chart");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(new PieChartPanel());
            f.setSize(350,300);
            f.setLocation(300,300);
            f.setVisible(true);
        }
    }
     class PieChartPanel extends JPanel
    {
        BufferedImage image;
        final int PAD = 30;
        Font font;
        NumberFormat numberFormat;
        
    	public PieChartPanel()
        {
            font = new Font("Book Antiqua", Font.BOLD, 20);
            numberFormat = NumberFormat.getPercentInstance();
            addMouseListener(new Visibility(this));
            addComponentListener(new ComponentAdapter()
            {});
        }
     protected void paintComponent(Graphics graphics)
        {
            super.paintComponent(graphics);
            Graphics2D graphics2d = (Graphics2D)graphics;
            graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            createChartImage();
            graphics2d.drawImage(image, 0, 0, this);
        }
        private void createChartImage()
        {
            int[] marks = { 98, 76, 90, 85, 75 };
            int width = getWidth();
            int height = getHeight();
            int cp = width/2;
            int cq = height/2;
            image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = image.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setPaint(Color.blue);
            g2.fillRect(0, 0, width, height);
            g2.setPaint(Color.black);
            int pie = Math.min(width,height) - 2*PAD;
            g2.draw(new Ellipse2D.Double(cp - pie/2, cq - pie/2, pie, pie));
            double total = 0;
            for(int j = 0; j < marks.length; j++)
                total += marks[j];
            double theta = 0, phi = 0;
            double p, q;
            for(int j = 0; j < marks.length; j++)
            {
                p = cp + (pie/2) * Math.cos(theta);
                q = cq + (pie/2) * Math.sin(theta);
                g2.draw(new Line2D.Double(cp, cq, p, q));
                phi = (marks[j]/total) * 2 * Math.PI;
                p = cp + (9*pie/24) * Math.cos(theta + phi/2);
                q = cq + (9*pie/24) * Math.sin(theta + phi/2);
                g2.setFont(font);
                String st = String.valueOf(marks[j]);
                FontRenderContext frc = g2.getFontRenderContext();
                float textWidth = (float)font.getStringBounds(st, frc).getWidth();
                LineMetrics lm = font.getLineMetrics(st, frc);
                float sp = (float)(p - textWidth/2);
                float sq = (float)(q + lm.getAscent()/2);
                g2.drawString(st, sp, sq);
                p = cp + (pie/2 + 4*PAD/5) * Math.cos(theta + phi/2);
                q = cq + (pie/2 + 4*PAD/5) * Math.sin(theta+ phi/2);
                st = numberFormat.format(marks[j]/total);
                textWidth = (float)font.getStringBounds(st, frc).getWidth();
                lm = font.getLineMetrics(st, frc);
                sp = (float)(p - textWidth/2);
                sq = (float)(q + lm.getAscent()/2);
                g2.drawString(st, sp, sq);
                theta += phi;
            }
            g2.dispose();
        }
     public void toggleVisibility()
        {
            repaint();
        }
    }
    class Visibility extends MouseAdapter
    {
        PieChartPanel piechart;
    public Visibility(PieChartPanel pc)
        {
            piechart = pc;
        }
     public void mousePressed(MouseEvent event)
        {
            if(event.getClickCount() > 1)
                piechart.toggleVisibility();
        }
    }
    That code DOES run fine from the desktop, but coughs up a world of these same errors when I try to transpose it into NetBeans....
    So there IS a difference in the way the two work.... I have seen it first hand.
    Try the above code from a java file on your desktop, and it will show the pie graph just fine, but put that same code into NetBeans, and it will spit back a bunch of errors??
    HELP!!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Runs fine in command prompt but not in NetBeans??

    Quote Originally Posted by zlloyd1 View Post
    No, this particular code is not runnable,
    Oh. My mistake then. I thought that when you titled the thread "Runs fine in command prompt but not in NB" you were talking about the same code running fine in one place but not the other. Perhaps a better title would have been "Some code runs fine in command prompt, but other code doesn't run in NB".

    Do you understand why the code you first posted does not compile? And what to do to make it compile?

    Sorry to sound tetchy, but it is important to be precise. Speaking of which:

    Quote Originally Posted by zlloyd1 View Post
    but I am getting the same hassles when I run the code for just the pie chart
    No you don't. You get altogether different hassles because the second lot of code you posted does not use array lists. (And the only errors in the first code related to array lists). Moreover the second lot of code compiles and runs fine, while the first lot did not compile or run at all.

    So a good place to start would be by posting the actual errors you are getting. Notice that the PieChartExample, PieChartPanel etc classes belong in the default package. You *will* get errors if you try and place the code, as you have posted it, in a named package like applet1 where you had NewClass.

  5. #5
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Smile Re: Runs fine in command prompt but not in NetBeans??

    Quote Originally Posted by pbrockway2 View Post
    You get altogether different hassles because the second lot of code you posted does not use array lists. (And the only errors in the first code related to array lists).
    I will look at the array lists, and see if I can put together what you are saying here, but my main goal is to that I have an applet that compiles, and runs correctly, AND I have a pie chart that works too, but I need to make an applet that opens a pie chart, and I am having difficulties combining the two programs into one??

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Runs fine in command prompt but not in NetBeans??

    OK. What I was saying about using array lists is that you can cast what get(i) returns to Color (in both places where you call that method). If you do that the code you first posted compiles, runs and displays a pie chart.

    If you get stuck, post the code you are using and describe the behaviour when it is compiled and/or run.

  7. #7
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Smile Re: Runs fine in command prompt but not in NetBeans??

    Thank you for the replies, pbrockway2.
    So you are saying to change the line, "g2d.setColor(gradingColors.get(i));", to "g2d.setColor((Color)colors.get(i));", and it should compile and run correctly??

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Runs fine in command prompt but not in NetBeans??

    I am saying change both of the lines where you get items from the array list:

    Java Code:
    g2d.setColor(colors.get(i));
    
    // AND
    
    g2d.setColor(gradingColors.get(i));
    so that the value returned by get() is cast to Color. But don't change gradingColors into colors or nothing good will happen.

    I know that with those two changes the code compiles and a pie chart results. (After adding a main() method somewhere. I tried only the simplest NewClass constructor and, so, only saw the simplest pie chart. You'll should test and make sure that the runtime behaviour is what you intend.) Be sure you understand *why* you have to cast the value that get() returns: I notice that for the values you did cast the returned value to Double.

  9. #9
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Default Re: Runs fine in command prompt but not in NetBeans??

    Quote Originally Posted by pbrockway2 View Post
    I know that with those two changes the code compiles and a pie chart results. (After adding a main() method somewhere.
    Where did you have to add a main method??

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Runs fine in command prompt but not in NetBeans??

    You don't have to add it anywhere in particular. As part of NewClass will do for testing. But you do need some public class with a main() method, otherwise what are you running?

    ---

    Also this whole discussion is cross posted from blue-walrus.com. Frankly it's a little annoying to spend time reading code you represented as something you were trying to create when it turns out to be code created by felixzacat. It's not so much the misrepresentation in and of itself - after all the code seems to have been freely offered to anyone who wanted to use it - but that posting it here suggests a level of familiarity with Java (using the collections framework, writing Swing applications) that you don't have. And responses you get which assume that familiarity miss the point and end up being a waste of time.

    Honestly frankencoding, where you cut-n-paste bits and pieces of other people's code and bolt them together is no way to learn Java. Just my opinion, of course. But it is my opinion.

    If the code as presented there is buggy (it is, the array lists have either to be made generic or the values obtained from them cast as described above), or lacking a main() method, that is something that should be taken up with the code's author.
    Last edited by pbrockway2; 02-03-2013 at 04:17 AM.

  11. #11
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Smile Re: Runs fine in command prompt but not in NetBeans??

    Quote Originally Posted by pbrockway2 View Post
    You don't have to add it anywhere in particular. As part of NewClass will do for testing. But you do need some public class with a main() method, otherwise what are you running?

    ---

    Also this whole discussion is cross posted from blue-walrus.com. Frankly it's a little annoying to spend time reading code you represented as something you were trying to create when it turns out to be code created by felixzacat. It's not so much the misrepresentation in and of itself - after all the code seems to have been freely offered to anyone who wanted to use it - but that posting it here suggests a level of familiarity with Java (using the collections framework, writing Swing applications) that you don't have. And responses you get which assume that familiarity miss the point and end up being a waste of time.

    Honestly frankencoding, where you cut-n-paste bits and pieces of other people's code and bolt them together is no way to learn Java. Just my opinion, of course. But it is my opinion.

    If the code as presented there is buggy (it is, the array lists have either to be made generic or the values obtained from them cast as described above), or lacking a main() method, that is something that should be taken up with the code's author.
    I did not write that I had written this code, as I am not personally familiar with making a pie chart myself, but I do have some knowledge of Java programming, including using the swing libraries. I am however, just now learning about how to make applets, and was interested in combining this pie chart with an applet successfully. Also, I did not say there was anything wrong with the pie chart code, on contrary, I said that it does compile and run properly from the console, but that when I tried to use the exact same code in NetBeans, it came back with a lot of errors. I was curious about the errors that were not there when I ran this program from the command line console is all. There was no main method in it when it ran from the console, and it still ran fine, but I can stick a main method in there to try and get it to work in NetBeans, and will!!

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Runs fine in command prompt but not in NetBeans??

    Quote Originally Posted by zlloyd1 View Post
    There was no main method in it when it ran from the console, and it still ran fine
    Balderdash.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Smile Re: Runs fine in command prompt but not in NetBeans??

    Quote Originally Posted by DarrylBurke View Post
    Balderdash.

    db
    NO, it DOES run as is!!
    Runs fine in command prompt but not in NetBeans??-run-desktop.jpg

    I did not add anything to the program code, and this is what comes out when I run it from command prompt....

    However, ths code does already have a main method:

    Java Code:
    public class PieChartExample
    {
        public static void main(String[] args)
        {
            JFrame f = new JFrame("Pie Chart");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(new PieChartPanel());
            f.setSize(350,300);
            f.setLocation(300,300);
            f.setVisible(true);
        }
    SO, I am not really clear on what the issue was there??
    Last edited by zlloyd1; 02-04-2013 at 07:04 AM.

Similar Threads

  1. Command Prompt versus NetBeans
    By JavaPete in forum NetBeans
    Replies: 1
    Last Post: 10-21-2012, 02:50 AM
  2. Replies: 7
    Last Post: 09-06-2012, 09:21 PM
  3. Replies: 6
    Last Post: 02-08-2011, 02:29 AM
  4. Code runs fine but get compile error
    By BillyB in forum New To Java
    Replies: 4
    Last Post: 11-29-2010, 02:18 PM
  5. accessing command prompt using java (netbeans)...
    By kureikougaiji in forum New To Java
    Replies: 6
    Last Post: 06-27-2008, 03:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •