ピースを動かす
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Game14 extends JPanel implements MouseListener
{
int haba;
int yoko, tate;
String fname;
Image img;
int[][] ban;
int spx, spy;
Game14()
{
addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
int x, y, dx, dy;
x = e.getX() / haba;
y = e.getY() / haba;
dx = (x<spx) ? -1 : (x>spx) ? 1 : 0;
dy = (y<spy) ? -1 : (y>spy) ? 1 : 0;
// if(x < spx)
// dx = -1;
// else if(x > spx)
// dx = 1;
// else
// dx = 0;
// if(y < spy)
// dy = -1;
// else if(y > spy)
// dy = 1;
// else
// dy = 0;
if(dx != 0 && dy != 0)
return;
while (x != spx || y != spy)
{
ban[spx][spy] = ban[spx+dx][spy+dy];
spx = spx + dx;
spy = spy + dy;
};
repaint();
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }