我是java的初学者,现在我正试图“解决”这个难题: http://zetcode.com/tutorials/javagamestutorial/puzzle/
一切都很清楚,但我想补充一些东西。正如你所看到的,当你运行它时,谜题已经解决了,所以我想添加一个算法,随机放置图像的各个部分。任何提示?
谢谢!
答案 0 :(得分:0)
初始化图片时,您应该将其放入List
,然后使用Collections::shuffle
随机播放List
:
List<Image> croppedImages = new ArrayList<Image>(4 * 3);
for ( int i = 0; i < 4; i++) {
for ( int j = 0; j < 3; j++) {
croppedImages.add(createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(j*width/3, i*height/4,
(width/3)+1, height/4))));
}
}
Collections.shuffle(croppedImages);
现在在初始化片段的循环中:
for ( int i = 0; i < 4; i++) {
for ( int j = 0; j < 3; j++) {
if ( j == 2 && i == 3) {
label = new JLabel("");
centerPanel.add(label);
} else {
button = new JButton();
button.addActionListener(this);
centerPanel.add(button);
button.setIcon(new ImageIcon(croppedImages.get(0)));
croppedImages.remove(0);
}
}