我的程序应该打印给定数字的所有百分比。它工作正常,直到我添加一个函数,结果颜色(红色低和绿色高)。现在它只打印奇数或偶数,但不是两个。至于着色,它从绿色变为红色。我希望所有结果都能够根据其价值进行打印和着色。函数只返回偶数
下面的代码
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());
}
}
}
当我打电话System.out.print()
它有我在JTextField
输入的确切数量。
我没有在Google上找到任何答案,也没有在StackOverflow上找到答案。我无法弄清楚,但我确信答案很简单。任何想法 ?
我弄明白了颜色。我所需要做的就是繁殖然后分而不是分乘。 (即)
//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));
如果事情在以前有效,为什么不直接比较两个版本,看看在添加颜色更改时无意中引入了哪些更改? – MarsAtomic 2014-11-03 04:10:30
@mars我只添加了函数'colorOnDigit',然后它被窃听。 – Chax 2014-11-03 04:12:51
当人们不知道代码应该做什么时,很难告诉别人什么是代码中断。你没有真正解释该计划的目的是什么。它试图解决什么问题。业务规则是什么?等请澄清。 – hfontanez 2014-11-03 04:17:27