源代码:package pintu; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random; import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel; public class PicGame extends JPanel implements MouseListener { private final int WIDTH = 50; private final int HEIGHT = 50; private JLabel[] pLabel = new JLabel[25]; private int black = 0;
private int p; public PicGame() { initDrawMe();
setLayout(null);
} public void initDrawMe() { Random random = new Random();
black = random.nextInt(25); for (int i = 0; i < 25; i++) {
if (i == black)
pLabel[i] = new JLabel();
else {
pLabel[i] = new JLabel(new ImageIcon(this.getClass()
.getResource(i + ".jpg")));
}
pLabel[i].setSize(WIDTH, HEIGHT);
pLabel[i].setLocation((i % 5) * WIDTH, (i / 5) * HEIGHT);
this.add(pLabel[i]);
}
} public void drawMe() {
for (int i = 0; i < 25; i++) {
pLabel[i].setLocation((i % 5) * WIDTH, (i / 5) * HEIGHT);
this.add(pLabel[i]);
}
} public void Direction(int x, int y) {
for (p = 0; p < 25; p++) {
if (pLabel[p].contains(x, y))
break;
} if (pLabel[p].getX() == pLabel[black].getX()) {
if ((pLabel[p].getY() - HEIGHT) == pLabel[black].getY())
// UP
changeLabel(pLabel[p], p);
if ((pLabel[p].getY() + HEIGHT) == pLabel[black].getY())
// DOWN
changeLabel(pLabel[p], p);
} if (pLabel[p].getY() == pLabel[black].getY()) {
if ((pLabel[p].getX() - WIDTH) == pLabel[black].getX())
// LEFT
changeLabel(pLabel[p], p);
if ((pLabel[p].getX() + WIDTH) == pLabel[black].getX())
// RIGHT
changeLabel(pLabel[p], p);
}
} public void changeLabel(JLabel p, int t) {
pLabel[black].setIcon(pLabel[t].getIcon());
black = t;
p.setIcon(null);
} public void mouseClicked(MouseEvent e) {
// TODO 自动生成方法存根
int x = e.getX();
int y = e.getY();
System.out.println(x + "," + y);
Direction(x, y);
//drawME();应该不需要这个方法的
} public void mouseEntered(MouseEvent e) {
// TODO 自动生成方法存根 } public void mouseExited(MouseEvent e) {
// TODO 自动生成方法存根 } public void mousePressed(MouseEvent e) {
// TODO 自动生成方法存根 } public void mouseReleased(MouseEvent e) {
// TODO 自动生成方法存根 } public void startDraw() {
int[] countPoint = new int[25]; Random random = new Random();
for (int i = 0; i < 25; i++){
int t = random.nextInt(25);
countPoint[i] = t;
int l = 0;
for (; l < countPoint.length; l++)
if (t==countPoint[i])
break;
if (l != countPoint.length)
continue; this.add(pLabel[i]);
}
System.out.println("shijian");
} }
package pintu; import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.JFrame; public class GameTest implements ActionListener{ private JFrame frame;
private PicGame pic;
private MenuBar menuBar; public GameTest(){
pic = new PicGame();
pic.setSize(250, 250);
pic.addMouseListener(pic); menuBar = new MenuBar();
Menu menu = new Menu("Field");
MenuItem menu1 = new MenuItem("Start");
MenuItem menu2 = new MenuItem("End");
menu.add(menu1);
menu.add(menu2);
menu.addActionListener(this);
menuBar.add(menu);
frame = new JFrame();
frame.setVisible(true);
frame.setSize(260, 295);
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); frame.add(pic);
frame.setMenuBar(menuBar);
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if(e.getActionCommand()=="Start")
pic.startDraw();
if(e.getActionCommand()=="End")
System.exit(0);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
GameTest g = new GameTest();
} } 目前有两个问题:1.JLabel的getX()和getY()返回的怎么是 0 啊?
2.运行的时候说是pLabel数组下标越界.......提示:java.lang.ArrayIndexOutOfBoundsException
3.compment中的contains()方法是判断一个点是否在这个组件中的是吧....可自己用输出个东西验证的时候,不是那样?我怎么都不明白!高手帮忙啊........谢了
import java.awt.event.MouseListener;
import java.util.Random; import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel; public class PicGame extends JPanel implements MouseListener { private final int WIDTH = 50; private final int HEIGHT = 50; private JLabel[] pLabel = new JLabel[25]; private int black = 0;
private int p; public PicGame() { initDrawMe();
setLayout(null);
} public void initDrawMe() { Random random = new Random();
black = random.nextInt(25); for (int i = 0; i < 25; i++) {
if (i == black)
pLabel[i] = new JLabel();
else {
pLabel[i] = new JLabel(new ImageIcon(this.getClass()
.getResource(i + ".jpg")));
}
pLabel[i].setSize(WIDTH, HEIGHT);
pLabel[i].setLocation((i % 5) * WIDTH, (i / 5) * HEIGHT);
this.add(pLabel[i]);
}
} public void drawMe() {
for (int i = 0; i < 25; i++) {
pLabel[i].setLocation((i % 5) * WIDTH, (i / 5) * HEIGHT);
this.add(pLabel[i]);
}
} public void Direction(int x, int y) {
for (p = 0; p < 25; p++) {
if (pLabel[p].contains(x, y))
break;
} if (pLabel[p].getX() == pLabel[black].getX()) {
if ((pLabel[p].getY() - HEIGHT) == pLabel[black].getY())
// UP
changeLabel(pLabel[p], p);
if ((pLabel[p].getY() + HEIGHT) == pLabel[black].getY())
// DOWN
changeLabel(pLabel[p], p);
} if (pLabel[p].getY() == pLabel[black].getY()) {
if ((pLabel[p].getX() - WIDTH) == pLabel[black].getX())
// LEFT
changeLabel(pLabel[p], p);
if ((pLabel[p].getX() + WIDTH) == pLabel[black].getX())
// RIGHT
changeLabel(pLabel[p], p);
}
} public void changeLabel(JLabel p, int t) {
pLabel[black].setIcon(pLabel[t].getIcon());
black = t;
p.setIcon(null);
} public void mouseClicked(MouseEvent e) {
// TODO 自动生成方法存根
int x = e.getX();
int y = e.getY();
System.out.println(x + "," + y);
Direction(x, y);
//drawME();应该不需要这个方法的
} public void mouseEntered(MouseEvent e) {
// TODO 自动生成方法存根 } public void mouseExited(MouseEvent e) {
// TODO 自动生成方法存根 } public void mousePressed(MouseEvent e) {
// TODO 自动生成方法存根 } public void mouseReleased(MouseEvent e) {
// TODO 自动生成方法存根 } public void startDraw() {
int[] countPoint = new int[25]; Random random = new Random();
for (int i = 0; i < 25; i++){
int t = random.nextInt(25);
countPoint[i] = t;
int l = 0;
for (; l < countPoint.length; l++)
if (t==countPoint[i])
break;
if (l != countPoint.length)
continue; this.add(pLabel[i]);
}
System.out.println("shijian");
} }
package pintu; import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.JFrame; public class GameTest implements ActionListener{ private JFrame frame;
private PicGame pic;
private MenuBar menuBar; public GameTest(){
pic = new PicGame();
pic.setSize(250, 250);
pic.addMouseListener(pic); menuBar = new MenuBar();
Menu menu = new Menu("Field");
MenuItem menu1 = new MenuItem("Start");
MenuItem menu2 = new MenuItem("End");
menu.add(menu1);
menu.add(menu2);
menu.addActionListener(this);
menuBar.add(menu);
frame = new JFrame();
frame.setVisible(true);
frame.setSize(260, 295);
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); frame.add(pic);
frame.setMenuBar(menuBar);
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if(e.getActionCommand()=="Start")
pic.startDraw();
if(e.getActionCommand()=="End")
System.exit(0);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
GameTest g = new GameTest();
} } 目前有两个问题:1.JLabel的getX()和getY()返回的怎么是 0 啊?
2.运行的时候说是pLabel数组下标越界.......提示:java.lang.ArrayIndexOutOfBoundsException
3.compment中的contains()方法是判断一个点是否在这个组件中的是吧....可自己用输出个东西验证的时候,不是那样?我怎么都不明白!高手帮忙啊........谢了
if (pLabel[p].getX() == pLabel[black].getX()) {
if ((pLabel[p].getY() - HEIGHT) == pLabel[black].getY()) {
// UP
changeLabel(pLabel[p], p);
}
if ((pLabel[p].getY() + HEIGHT) == pLabel[black].getY()) {
// DOWN
changeLabel(pLabel[p], p);
}
} if (pLabel[p].getY() == pLabel[black].getY()) {
if ((pLabel[p].getX() - WIDTH) == pLabel[black].getX()) {
// LEFT
changeLabel(pLabel[p], p);
}
if ((pLabel[p].getX() + WIDTH) == pLabel[black].getX()) {
// RIGHT
changeLabel(pLabel[p], p);
}
}
调到 for (p = 0; p < 25; p++) {
if (pLabel[p].contains(x, y)) {
break;
}中就不会抛数组下标越界的异常了。
整个代码没细看。呵呵……
加油哟……