【发布时间】:2019-02-12 09:17:22
【问题描述】:
我正在尝试根据随机整数的值更改我拥有的图像,但是,eclipse 说 rand 整数需要一个主体。
我已经试过了
int rand = random.nextInt(4);
但这也行不通。任何帮助将不胜感激。
public class GrassTile extends Tile {
Random random = new Random();
static BufferedImage texture;
int rand;
rand = random.nextInt(4);
if (rand == 0) {
texture = Assets.grass0;
} else if(rand == 1) {
texture = Assets.grass1;
} else if(rand == 2) {
texture = Assets.grass2;
} else if(rand == 3) {
texture = Assets.grass3;
}
public GrassTile(int id) {
super(texture, id);
}
}
谢谢, Java开发
【问题讨论】:
-
您需要将代码放入方法中。
-
您在什么情况下尝试更改图像?
-
你想改变点击按钮吗?或者通过点击一个按钮?还是仅在您第一次启动程序时出现?
-
因为作为@Eran,您需要在方法甚至构造函数中包含该代码,具体取决于您要执行的操作
-
基本语法问题 - 投票结束。