2013-01-09 143 views
0

嗨我想獲得文本字體大小和顏色的數組的頻率,當我有字符串,我計算頻率我想改變字和顏色的大小根據他們的整體價值的大小,我試圖劃分frequency/average *10,但給我空,但在我的計算器,它給了我,即80/100 * 10 = 8,這將採取索引8和180字體大小陣列的頻率分佈

任何人都可以幫助我嗎?

private static int[] fontWeight = { 40, 60, 80, 100, 120, 140, 160,180 }; 
private static Color[] Colors = { Color.blue, Color.cyan, Color.yellow, Color.green, Color.red, 
     Color.orange, Color.pink,Color.pink }; 

//主

for (String str: wordList) { 


      int wordFreq = randWord.getFrequency();//assume 80 comes here 
      int fontSize = getFontSize(wordFreq); 
      Font font = new Font(Font.SANS_SERIF, Font.ITALIC, fontWeight [fontSize ]); 
      graphics.setFont(font); 
      graphics.setColor(Colors[fontSize]); 
      FontMetrics fm = graphics.getFontMetrics(); 

      graphics.drawString(randWord.getWord() + "", x, y); 
      } 

//方法

private static int getFontSize(int freq) { 

    int newFont = (int) (freq/100)*10; 

    if (newFont >= 5) newFont = 6; 
    // if(newFont<2) 
    // newFont = 1; 
    System.out.println("freq " + freq + " font index" + newFont + " font size " 
      + fontWeight[newFont]); 

    return newFont; 
} 

感謝您的幫助

回答

1

整數除法行爲不同,如果分子大於分母越小,結果是0 。例如80/100返回0.你要做的就是將分子乘以10,然後除以它。

int newFont = (int) (freq/100)*10; 

應更改爲

int newFont = (int) (freq*10/100);