0
我目前正在使用Java进行2D游戏,并且在执行日/夜周期时遇到了一点麻烦。我目前已经有一个系统在整个屏幕上绘制一个半透明的矩形。根据游戏中的时间,矩形变得或多或少透明。与此相关的问题是帧速率受到了很大影响。Java改变图像颜色来表示日/夜周期
所以现在我试图把当前显示的像素的十六进制颜色值,将它分成独立的RGB值,并减少这些值然后返回后面的数字为十六进制,以将其绘制到屏幕。
这是我目前的代码。我的问题是如果值大于99的程序崩溃。
public int getRGB(String hex) {
if (hex.length() > 1) {
String temp;
int colVal;
temp = hex.substring(2, 4);
red = Integer.parseInt(temp, 16);
temp = hex.substring(4, 6);
green = Integer.parseInt(temp, 16);
temp = hex.substring(6, 8);
blue = Integer.parseInt(temp, 16);
colVal = darkenTile(red, green, blue);
return colVal;
} else {
return 0;
}
}
public int darkenTile(int r, int g, int b) {
int col = 0;
String red;
String green;
String blue;
for (int i = 0; i < 20; i++) {
if (r > 0) r--;
if (g > 0) g--;
if (b > 0) b--;
}
if (r == 0) red = "00";
else red = Integer.toString(r);
if (g == 0) green = "00";
else green = Integer.toString(g);
if (b == 0) blue = "00";
else blue = Integer.toString(b);
String hex = red + green + blue;
System.out.println(hex);
col = Integer.parseInt(hex, 16);
return col;
}
当我崩溃时,这就是我得到的。
Exception in thread "Display" java.lang.NumberFormatException: For input string: "110112129"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.cousersoft.game.graphics.Screen.darkenTile(Screen.java:178)
at com.cousersoft.game.graphics.Screen.getRGB(Screen.java:154)
at com.cousersoft.game.graphics.Screen.renderTile(Screen.java:50)
at com.cousersoft.game.level.tile.RockTile.render(RockTile.java:13)
at com.cousersoft.game.level.Level.render(Level.java:84)
at com.cousersoft.game.Game.render(Game.java:200)
at com.cousersoft.game.Game.run(Game.java:163)
at java.lang.Thread.run(Unknown Source)
这行确实在程序崩溃?什么是错误信息? – Dziugas
让我想起恶魔城2:西蒙的追求 - “多么可怕的诅咒之夜。” –
你要检查'hex'字符串是否超过1个字符,但是假设它是8个字符,例如temp = hex.substring(6,8) – Dziugas