0

My program is supposed to print all the percentage of a given number. It was working fine until i add a function that color the result(red is low and green is high). Now it only print odd number or even number, but not both. As for the coloring, it work backward, from green to red. I want all the results to print and to be colored according to their value.

Here's the code

public class Window extends JFrame implements ActionListener{

private JButton theButton = new JButton("Calculer sur 100");
private JEditorPane text = new JEditorPane();
private JTextField textField = new JTextField("Écrire un nombre");
private JScrollPane scroller = new JScrollPane(text);
private StringBuilder sb = new StringBuilder();
private Style style;

public Window() {
    setLayout(new BorderLayout());
    setTitle("Test");
    setSize(400, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    text.setContentType("text/html");

    theButton.addActionListener(this);

    getContentPane().add(scroller, BorderLayout.CENTER);
    getContentPane().add(textField, BorderLayout.NORTH);
    getContentPane().add(theButton, BorderLayout.SOUTH);
    setVisible(true);
}

/**
 * Prints the result on text
 * @param num
 */
private void print100(int num) {
    for (int i = 1; i < num + 1; i++) {
        text.setText(appendString(i));
    }
}


/**
 * Color from red to green according to the result
 * @param a
 * @return a haxaecimal to color the answer
 */
private String colorOnDigit(double a) {
    double green, red;
    int g, r;
    double power = a;
    int blue = 0;

    green = 255 * Math.sqrt( Math.cos ( power * Math.PI / 200 ));
    red = 255 * Math.sqrt( Math.sin ( power * Math.PI / 200 ));

    int precision = 10; //Number of zero = number of digits
    green = Math.floor(green * precision + .5) / precision;
    red = Math.floor(red * precision + .5) / precision;

    r = (int) red;
    g = (int) green;

    String hex = String.format("#%02x%02x%02x", r, g, blue);

    System.out.println("blue " + blue);
    System.out.println("Green " + green);
    System.out.println("Red " + red);
    System.out.println("----------");
    return "<font color = \"" + hex + ">";
}

/**
 * convert the number to string 
 * @param i
 * @return a string that contains the information
 */
private String appendString(int i){
    double a = doMath(i, checkForNumber());

    String s = "<br>" + colorOnDigit(a) + i + " : " + a + "</font>";

    return sb.append(s).toString();
}

/**
* Check if the text in the text is numbers
* return numl
*/
private int checkForNumber() {
    int numl;
    try {
        numl = Integer.parseInt(textField.getText());
    } catch (NumberFormatException e) {
        text.setText("Essayer avec des nombres...");
        return 0;
    }
    return numl;
}

/**
* leave specific number of digit after the dot
* return myNum
*/
private double doMath(int i, int num) {
    double myNum = ((double) i / num) * 100;
    int precision = 100; //Number of zero = number of digits
    myNum = Math.floor(myNum * precision + .5) / precision;
    return myNum;
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == theButton) {
        text.setText("");
        print100(checkForNumber());
    }
}

}

When i call System.out.print() it has the exact number of what i entered in the JTextField.

I didn't found any answer on google nor on StackOverflow. I can't figured it out, but i'm pretty sure the answer is simple. Any idea ?


I got the color figured out. All i needed to do was multiply then divide instead of divide the multiply. (i.e)

//Before
green = 255 * Math.sqrt( Math.cos ( power * Math.PI / 200 ));
red = 255 * Math.sqrt( Math.sin ( power * Math.PI / 200 ));

//After
green = 255 * Math.sqrt( Math.cos ( power / Math.PI * 200 ));
red = 255 * Math.sqrt( Math.sin ( power / Math.PI * 200 ));
Chax
  • 1,041
  • 2
  • 14
  • 36
  • 1
    If things were working before, why not just diff the two versions and see what changes you inadvertently introduced when adding the color changes? – MarsAtomic Nov 03 '14 at 04:10
  • @mars I've only added the function `colorOnDigit` then it bugged. – Chax Nov 03 '14 at 04:12
  • It is hard to tell someone what is broke in code when one does not know what the code is supposed to do in the first place. You didn't really explain what is the purpose of the program. What problem is it trying to solve. What are the business rules? etc. Please clarify. – hfontanez Nov 03 '14 at 04:17
  • @hfontanez **My program is supposed to print all the percentage of a set**. First line. But to be more explicit it take a given number and it gives you all the percentage of the set(It's kinda hard to explain as my native language is not english). I edited my question to be more specific. – Chax Nov 03 '14 at 04:19
  • I found a part of the solution, but i don't know how to fix it. Sometimes the value of _green_ or _red_ is NaN. According to [here](http://stackoverflow.com/questions/2618059/in-java-what-does-nan-mean) it means it's not a number. So i'm probably doing some arithmetic where i shoudn't. Where is now the question. – Chax Nov 03 '14 at 04:32
  • It all must obviously happen in colorOnDigit method. I don't know what you're trying to do here, but your blue value is an int while red and green are doubles, and you perform some operations on red and green that you dont on blue. – FruitAddict Nov 03 '14 at 04:36
  • @FruitAddict i don't need to because i want the color from red to green to appear so blue stay at 0. The **colorOnDigit()** return `` string. All it does is color my string. But the problem is definetly there. – Chax Nov 03 '14 at 04:39
  • You mentioned things worked before adding color. So, there is something obviously wrong in the logic to apply color. What it is not clear to me is what are the rules for coloring? If you can explain that, perhaps we can help you figure out what is wrong with the code. – hfontanez Nov 03 '14 at 04:40
  • @hfontanez Low number should be in red and as it gets higher it should be greener. I'm using a `JEditorPane` so i do my coloring by using the `` html tags. The method return the string of the hex to use in ``. – Chax Nov 03 '14 at 04:42
  • If you run the code and enter 12 as the value it prints out even number. What i don't get is why the number 4 is in red even though _green_ and _red_ value are **NaN** – Chax Nov 03 '14 at 04:44

1 Answers1

1

For the test, I've changed your application slightly, into an simple Java Application. I tested this program by emulating the input from textField (which is modified to be given by the argument of print100 method).

However, the program gives a normal output from 1 to 100, without skipping odd numbers, even 12 is given as an argument to print100 method.

Anyway, you should change the last line of the colorOnDigit into return "<font color = \"" + hex + "\">";. Closing double-quotation mark (which is should be included in the resultant HTML tag) is missing. I think maybe it's the reason that odd tags are missing in your output.

public class OneHundred {

    /**
    * leave specific number of digit after the dot
    * return myNum
    */
    private static double doMath(int i, int num) {
        double myNum = ((double) i / num) * 100;
        int precision = 100; //Number of zero = number of digits
        myNum = Math.floor(myNum * precision + .5) / precision;
        return myNum;
    }

    /**
     * Color from red to green according to the result
     * @param a
     * @return a haxaecimal to color the answer
     */
    private static String colorOnDigit(double a) {
        double green, red;
        int g, r;
        double power = a;
        int blue = 0;

        green = 255 * Math.sqrt( Math.cos ( power * Math.PI / 200 ));
        red = 255 * Math.sqrt( Math.sin ( power * Math.PI / 200 ));

        int precision = 10; //Number of zero = number of digits
        green = Math.floor(green * precision + .5) / precision;
        red = Math.floor(red * precision + .5) / precision;

        r = (int) red;
        g = (int) green;

        String hex = String.format("#%02x%02x%02x", r, g, blue);

        System.out.println("blue " + blue);
        System.out.println("Green " + green);
        System.out.println("Red " + red);
        System.out.println("----------");
        return "<font color = \"" + hex + "\">";
    }

    /**
     * convert the number to string 
     * @param i
     * @return a string that contains the information
     */
    private static String appendString(StringBuilder b, int i, int input){
        double a = doMath(i, input /* assumed an arbitrary input*/ );

        String s = "<br>" + colorOnDigit(a) + i + " : " + a + "</font>\n";

        return b.append(s).toString();
    }

    private static String print100(int num) {
        StringBuilder text = new StringBuilder();

        for (int i = 1; i < 100 + 1; i++) {
            appendString(text, i, num);
        }

        return text.toString();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String l = print100(7);
        System.err.println(l);
    }

}
Byungjoon Lee
  • 913
  • 6
  • 18