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