我有一个简单的益智游戏.有一个由16块瓷砖组成的图像(随机放置).图像存储在一个数组中,当游戏启动时,它们被添加到主JPanel中.
替代文字http://img248.imageshack.us/img248/7403/27632947.gif
游戏以这种方式工作:每个图像都有"地点"和"数字"."放置"是网格上的当前位置(正确与否),"数字"是图像的所需位置.当用户点击图像时,会检查其"地点"和"数字"属性.如果他们匹配什么都没发生 如果没有游戏检查当前是否有任何图像在内存中.如果没有,则存储该图像的"位置"和"数字".如果内存中有一些图像,则使用存储图像的"数字"检查当前单击的图像的"plac".当他们匹配时 - 他们的位置被交换.这部分工作正常.但是现在,我正在使用更新的图像在我的JPanel上调用addComponent方法,而且没有任何反应.不该"
包奖金;
import javax.swing.*;
import java.util.Random;
import java.awt.event.*;
import java.awt.*;
class Puzzle extends JPanel implements ActionListener {
private int selected_nr=-1;
private int selected_pl=-1;
private boolean memory=false;
private static Img[] images;
public Puzzle(){
JFrame f = new JFrame("Smile");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.setSize(252,252);
f.setVisible(true);
setLayout(new GridLayout(4, 4));
images = new Img[16];
int[] buttons = new int[16];
for(int i=0; i<16; i++){
buttons[i] = i;
}
int rand;
int temp;
Random random;
random = new Random(System.currentTimeMillis());
for (int i = …Run Code Online (Sandbox Code Playgroud) 这是 JFrame
package client.connection;
import java.awt.Dimension;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JFrame;
class DrawFrameRemoteControl extends JFrame
{
private DrawPanelRemoteControl imagePanel;
private ClientRemoteControlConnection clientRemoteControlConnection;
private ObjectInputStream clientInputStream;
private ObjectOutputStream clientOutputStream;
private Dimension imageDimension;
private Dimension serverDimension;
public DrawFrameRemoteControl(Dimension imageDimension,ClientRemoteControlConnection clientRemoteControlConnection,ObjectInputStream clientInputStream,ObjectOutputStream clientOutputStream,Dimension serverDimension)
{
super("Remote Desktop Control");
this.clientRemoteControlConnection=clientRemoteControlConnection;
this.clientInputStream=clientInputStream;
this.clientOutputStream=clientOutputStream;
this.imageDimension=imageDimension;
this.serverDimension=serverDimension;
imagePanel=new DrawPanelRemoteControl(imageDimension);
add(imagePanel);
setSize(imageDimension.width,imageDimension.height);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
}
void drawNewImageGrayscale(byte[] array)
{
imagePanel.setNewImageGrayscale(array);
imagePanel.repaint();
}
}
Run Code Online (Sandbox Code Playgroud)
这是扩展的JPanel类 -
package client.connection;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit; …Run Code Online (Sandbox Code Playgroud)