当前位置: 代码迷 >> J2ME >> 求高手帮忙 小弟我是一个菜鸟
  详细解决方案

求高手帮忙 小弟我是一个菜鸟

热度:644   发布时间:2016-04-17 21:18:00.0
求高手帮忙 我是一个初学者
为什么只能下在第二列上啊 其他地方不能下啊
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class FiveChessJFrame extends JFrame implements MouseListener{
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
int x = 0;
int y = 0;
int[][] checkwho = new int[19][19];
FiveChessJFrame() {
this.setSize(670, 420);
this.setLocation((width-670)/2, (height-420)/2);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.addMouseListener(this);
this.setVisible(true);

}
public void paint(Graphics g){
BufferedImage bgimage = null;
try {
bgimage = ImageIO.read(new File("E:/java/workspace/fivechess/image/wuziqipan.jpg"));
}
catch (IOException e){
e.printStackTrace();
}
g.drawImage(bgimage, 0, 0, this);
g.setColor(new Color(245,222,179));
g.fillRect(190, 110, 288, 288);
g.setColor(Color.black);
for(int i = 0;i <= 18;i++) {
g.drawLine(190, 110+16*i,478,110+16*i);
g.drawLine(190+16*i, 110, 190+16*i, 398);
}
for(int i = 1; i <= 19 ; i++){
for(int j = 1; j <= 19; j++) {
int tempx;
int tempy;
if (checkwho[i][j] == 1) {
tempx = i*16+190;
tempy = j*16+110;
//JOptionPane.showMessageDialog(this, "tempx:"+tempx+"tempy:"+tempy+"checkwho[i][j]:"+checkwho[i][j]);
g.setColor(Color.black);
g.fillOval(tempx-8, tempy-8, 16, 16);
}
if (checkwho[i][j] == 2) {
tempx = i*16+190;
tempy = j*16+110;
g.setColor(Color.black);
g.drawOval(tempx-8, tempy-8, 16, 16);
g.setColor(Color.white);
g.fillOval(tempx-8, tempy-8, 16, 16);
}
  }
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
//System.out.println("x = "+e.getX());
//System.out.println("y = "+e.getY());
x = e.getX();
y = e.getY();

if (x>=190 && x<=478 && y>=110 && y<=398) {
//JOptionPane.showMessageDialog(this, "鼠标在棋盘范围内!");
int i;
int j;
i = (x-190)/16;
j = (y-110)/16;
checkwho[i][j] = 1;
//JOptionPane.showMessageDialog(this, "x:"+i+"y:"+j+"checkwho[x][y]:"+checkwho[i][j]);
this.repaint();
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}
}
//主类
public class FiveChess {
public static void main(String[] args) {
FiveChessJFrame ff = new FiveChessJFrame();
}
}


------解决方案--------------------
int[][] checkwho = new int[19][19];


for(int i = 1; i <= 19 ; i++){
for(int j = 1; j <= 19; j++) {
int tempx;
int tempy;
if (checkwho[i][j] == 1) {

当你循环取到checkwho[1][19]的时候就数组溢出了。。。

根据业务更改数组的length或者循环的size。。。
  相关解决方案