是否有任何快捷方式將得分顯示爲數字?我有從0到9的數字。所以如果得分是189,它應該畫189,所以它是圖像1,8和9.如果我不得不爲每個可能的結果聲明,它是不值得的。Java顯示圖像數字得分
if(a ==1 && a<2)
g.drawImage(image1,0,0,this);
那麼,有什麼辦法可以拆分整數位,並呼籲g.drawImage: 我有嘗試過嗎?
是否有任何快捷方式將得分顯示爲數字?我有從0到9的數字。所以如果得分是189,它應該畫189,所以它是圖像1,8和9.如果我不得不爲每個可能的結果聲明,它是不值得的。Java顯示圖像數字得分
if(a ==1 && a<2)
g.drawImage(image1,0,0,this);
那麼,有什麼辦法可以拆分整數位,並呼籲g.drawImage: 我有嘗試過嗎?
第1步。創建10個圖像,每個數字一個,並使用數組引用每張圖片,例如,
Image[] D = new Image[] {
new Image("0.png"),
new Image("1.png"),
new Image("2.png"),
new Image("3.png"),
new Image("4.png"),
new Image("5.png"),
new Image("6.png"),
new Image("7.png"),
new Image("8.png"),
new Image("9.png"),
}
第2步:將您的手機號碼字符串:
String digits = number + "";
對於每一位在digits
畫出相應的圖片。
for (int i = 0; i < digits.length(); i++)
drawImage(x, y, D[digits.charAt(i) - '0']);
你必須根據你的圖片的大小調整x
和y
值。
有沒有想過做mod?含義score % 10
給你第一個(最右邊)數字,然後score /= 10
。沖洗並重復。如果您不想直接修改分數,也許可以使用temp。跟蹤你做了多少MOD以及圖像的寬度,並且可以生成圖像。
例如。
int widthOfImgs;//put ur val here, if constant width //otherwise you'll need to add the width of the images // and replace the (widthOfImgs * countMod) with curOffset //int curOffset = 0; int countMod = 0, temp = score;//assuming its an integer score while (temp > 0){//given the score is always positive switch(temp % 10){ case 1: //draw 1 g.drawImage(image1,farRightPos - widthOfImgs * countMod,0,this); //curOffset += widthOfImage1; //dont recall the exact syntax daImage.width? case 2: //blah blah } ++countMod;// if the images are of constant width temp /= 10; }
剛纔看。我認爲這可能會奏效。我在想這樣的事情。如果我的最高分數爲6位數,我會將6個零分作爲開始。並檢查分數。如果分數更新,我可以有6個臨時變量可能會得到價值,我可以通過if語句運行它們,並重新繪製圖像。 – Anak1n
我相信只要得分是一個正整數,這應該工作。而img的寬度是不變的,但你可以相應地調整它(我會加入)。 – SGM1
是得分是一個整數值。 – Anak1n