1.2数据模型
这里是引用
package ch1.data;
public abstract class Animal {
String name ;
Point [] allCanAtPoint; //全部点位置
Point point; //动物当前所在的点位置
ViewForAnimal animalView; //动物的外观视图
public void setAtPoint(Point p) {
if(p!=null){
point = p;
point.setIsHaveAnimal(true);
point.setAtPointAnimal(this);
}
}
public Point getAtPoint() {
return point;
}
public void setAllCanAtPoint(Point [] point){
allCanAtPoint = point;
}
public void setAnimalView(ViewForAnimal animalView) {
this.animalView = animalView;
animalView.setAnimal(this);
}
public ViewForAnimal getAnimalView() {
return animalView;
}
public void setName(String s) {
name = s;
}
public String getName() {
return name;
}
public abstract boolean move();
}
这里是引用
package ch1.data;
public class LeftAnimal extends Animal{
public boolean move(){
int k = -1;
boolean successMove = false;
Point p = getAtPoint(); //动物当前所在点
for(int i=0;i<allCanAtPoint.length;i++){//寻找p点位置索引
if(allCanAtPoint[i].equals(p)){
k = i; //找到动物当前所处的位置:allCanAtPoint[k]
break;
}
}
if(k==allCanAtPoint.length-1){//已经在最右面的点位置
return false;
}
if(allCanAtPoint[k+1].isHaveAnimal()==false) { //前面位置上没有动物
this.setAtPoint(allCanAtPoint[k+1]);
successMove = true;
p.setAtPointAnimal(null);
return successMove ;
}
if((k+1)==allCanAtPoint.length-1){ //前面位置上是已经到达终点的动物
return false;
}
if(allCanAtPoint[k+2].isHaveAnimal()==false) {//前方隔位上没有动物
this.setAtPoint(allCanAtPoint[k+2]);
successMove = true;
p.setAtPointAnimal(null);
return successMove ;
}
return successMove ;
}
}
package ch1.data;
public class RightAnimal extends Animal{
public boolean move(){
int k = -1;
boolean successMove = false;
Point p = getAtPoint();//动物当前所在点
for(int i=0;i<allCanAtPoint.length;i++){//寻找p点位置索引
if(allCanAtPoint[i].equals(p)){
k = i;
break;
}
}
if(k==0){ //已经在最左面的点位置
return false;
}
if(allCanAtPoint[k-1].isHaveAnimal()==false) {//前面位置上没有动物
this.setAtPoint(allCanAtPoint[k-1]);//动物到达allCanAtPoint[k-1]点
successMove = true;
p.setAtPointAnimal(null); //p点设置为无动物
return successMove ;
}
if((k-1)==0){ //前面位置上是已经到达终点的动物
return false;
}
if(allCanAtPoint[k-2].isHaveAnimal()==false) {//前方隔位上没有动物
this.setAtPoint(allCanAtPoint[k-2]); //动物到达allCanAtPoint[k-2]点
successMove = true;
p.setAtPointAnimal(null); //p点设置为无动物
return successMove ;
}
return successMove ;
}
}
这里是引用
package ch1.data;
public class Point{
int x,y;
boolean haveAnimal;
Animal animal=null; //在该点位置上的动物
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
public boolean isHaveAnimal(){
return haveAnimal;
}
public void setIsHaveAnimal(boolean boo){
haveAnimal=boo;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public void setAtPointAnimal(Animal animal){
this.animal=animal;
if(animal!=null) {
haveAnimal = true;
}
else {
haveAnimal = false;
}
}
public Animal getAtPointAnimal(){
return animal;
}
}
这里是引用
package ch1.data;
import javax.swing.JPanel;
public abstract class ViewForAnimal extends JPanel {
public abstract void setAnimal(Animal animal);
public abstract void setImage(String name);
public abstract Animal getAnimal();
public abstract void setAnimalViewLocation(int x,int y);
public abstract void setAnimalViewSize(int w,int h);
}
1.3测试类
package ch1.test;
import ch1.data.*;
public class AppTest {
public static void main(String [] args) {
Point [] point = new Point[7];//创建7个点
for(int i=0;i<point.length;i++) {
point[i] = new Point();
point[i].setX(i);
point[i].setY(10);
}
Animal [] left = new Animal[3]; //3个左边动物
Animal [] right = new Animal[3];
for(int i =0;i<left.length;i++ ){ //把左边动物放在点上
left[i] = new LeftAnimal();
left[i].setName("猫"+i);
left[i].setAtPoint(point[i]);
left[i].setAllCanAtPoint(point);
}
for(int i =0;i<right.length;i++ ){ //把右边动物放在点上
right[i] = new RightAnimal();
right[i].setName("狗"+i);
right[i].setAtPoint(point[4+i]);
right[i].setAllCanAtPoint(point);
}
input(point);
if(right[0].move()) //让右边的第1个动物运动
input(point); //输出各个点上有无动物的信息
if(left[2].move())
input(point);
if(left[1].move())
input(point);
if(right[0].move())
input(point);
if(right[1].move())
input(point);
if(right[2].move())
input(point);
if(left[2].move())
input(point);
if(left[1].move())
input(point);
if(left[0].move())
input(point);
if(right[0].move())
input(point);
if(right[1].move())
input(point);
if(right[2].move())
input(point);
if(left[1].move())
input(point);
if(left[0].move())
input(point);
if(right[2].move())
input(point);
}
static void input(Point [] point){
for(int i=0;i<point.length;i++){
Animal animal=point[i].getAtPointAnimal();
if(animal!=null)
System.out.print(animal.getName());
else
System.out.print(" ");
}
System.out.println();
}
}
1.4视图设计
package ch1.view;
import java.awt.*;
import ch1.data.*;
public class AnimalView extends ViewForAnimal{
Animal animal; //要绘制图像的动物
Image image; //给动物绘制的图像
Toolkit tool; //负责绘制图像的toolkit对象
public AnimalView() {
tool = getToolkit();
}
public void setAnimal(Animal animal){
this.animal = animal;
}
public void setImage(String name){
image = tool.getImage(name);
repaint();
}
public Animal getAnimal() {
return animal;
}
public void setAnimalViewLocation(int x,int y){
setLocation(x,y);
}
public void setAnimalViewSize(int w,int h){
setSize(w,h);
}
public void paintComponent(Graphics g){//组件,绘制图像的方法(自动执行)
super.paintComponent(g);
int w=getBounds().width;
int h=getBounds().height;
g.drawImage(image,0,0,w,h,this);
}
}
package ch1.view;
import javax.swing.*;
import java.awt.*;
import ch1.data.Animal;
import ch1.data.Point;
import ch1.data.ViewForAnimal;
import ch1.data.LeftAnimal;
import ch1.data.RightAnimal;
import java.util.*;
import java.awt.geom.*;
public class GamePanel extends JPanel {
public int animalCount = -1;//动物总数
public Point [] point ;//放置动物的点
Animal [] leftAnimal,rightAnimal;
public ViewForAnimal [] leftAnimalView,rightAnimalView;//动物视图
public int width =90,height=70; //动物视图的大小
public int gap = 2; //动物之间的间隙
public JButton buttonRedo;//撤销按钮
public JButton buttonReStart;//重新开始游戏
public Stack<Point> saveAnimalStep; //存放动物走动的位置,以便恢复
HandleAnimalMove handleAnimalMove;//负责处理MouseEvent的监视器
HandleRedo handleRedo; //负责ActionEvent的监视器
HandleReStart handleReStart; //负责ActionEvent的监视器
public GamePanel(){
setLayout(null);
buttonRedo = new JButton("撤销");
buttonReStart = new JButton("重新开始");
saveAnimalStep = new Stack<Point>();
}
public void setAnimalCount(int n){
if(n%2 != 0 ||n<=1) {
System.out.println(n+"的个数不合理");
System.exit(0);
}
removeAll(); //首先移出曾添加到该容器中的全部组件
animalCount = n;
initPoitAndAnimal(); //初始化动物和位置对象
initLeftAnimalView(); //初始化左边的动物视图
initRightAnimalView();
registerListener(); //注册监视器
}
private void initPoitAndAnimal(){//初始化动物和位置对象
point = new Point[animalCount+1];
int posionX = width; //点的位置的x坐标
int posionY = height;
for(int i=0;i<point.length;i++) {
point[i] = new Point();
point[i].setX(posionX);
point[i].setY(posionY);
posionX = posionX+width+gap;
}
int m = animalCount/2;
leftAnimal = new LeftAnimal[m];
rightAnimal = new RightAnimal[m];
for(int i =0;i<leftAnimal.length;i++ ){
leftAnimal[i] = new LeftAnimal();
leftAnimal[i].setAtPoint(point[i]);
leftAnimal[i].setAllCanAtPoint(point);
}
for(int i =0;i<rightAnimal.length;i++ ){
rightAnimal[i] = new RightAnimal();
rightAnimal[i].setAtPoint(point[m+1+i]);
rightAnimal[i].setAllCanAtPoint(point);
}
}
private void initLeftAnimalView(){//初始化左边的动物视图
int m = animalCount/2;
leftAnimalView = new ViewForAnimal[m];
for(int i =0;i<leftAnimalView.length;i++ ){
leftAnimalView[i] = new AnimalView();
leftAnimal[i].setAnimalView(leftAnimalView[i]);
Point p = leftAnimal[i].getAtPoint();
int x = p.getX();
int y = p.getY();
add(leftAnimalView[i]);
//动物视图所在位置和动物所在点相同:
leftAnimalView[i].setAnimalViewLocation(x,y);
leftAnimalView[i].setAnimalViewSize(width,height);
}
}
private void initRightAnimalView(){//初始化右边的动物视图
int m = animalCount/2;
rightAnimalView = new ViewForAnimal[m];
for(int i =0;i<rightAnimalView.length;i++ ){
rightAnimalView[i] = new AnimalView();
rightAnimal[i].setAnimalView(rightAnimalView[i]);
Point p = rightAnimal[i].getAtPoint();
int x = p.getX();
int y = p.getY();
add(rightAnimalView[i]);
rightAnimalView[i].setAnimalViewLocation(x,y);
rightAnimalView[i].setAnimalViewSize(width,height);
}
}
private void registerListener(){
handleAnimalMove = new HandleAnimalMove(this);
//监视用户在动物视图上触发的MouseEvent事件:
for(int i =0;i<rightAnimalView.length;i++ ){
rightAnimalView[i].addMouseListener(handleAnimalMove);
}
for(int i =0;i<leftAnimalView.length;i++ ){
leftAnimalView[i].addMouseListener(handleAnimalMove);
}
handleRedo = new HandleRedo(this);
handleReStart = new HandleReStart(this);
//监视用户在按钮上触发的ActionEvent事件:
buttonRedo.addActionListener(handleRedo);
buttonReStart.addActionListener(handleReStart);
}
public void setLeftAnimalImage(String pic){
if(animalCount==-1)
return;
for(int i =0;i<leftAnimalView.length;i++ ){
leftAnimalView[i].setImage(pic);
}
}
public void setRightAnimalImage(String pic){
if(animalCount==-1)
return;
for(int i =0;i<rightAnimalView.length;i++ ){
rightAnimalView[i].setImage(pic);
}
}
public void paintComponent(Graphics g){
int penHeight =12; //画笔的高度
super.paintComponent(g);
int xStart =width+gap;
int yStart =2*height+penHeight/2;
int xEnd =(animalCount+2)*width+(animalCount+1)*2;
int yEnd =2*height+penHeight/2;
Line2D line=new Line2D.Double(xStart,yStart,xEnd,yEnd);
Graphics2D g_2d=(Graphics2D)g;
g_2d.setColor(Color.blue);
BasicStroke bs=
new BasicStroke(penHeight,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);
g_2d.setStroke(bs);
g_2d.draw(line);
}
}
package ch1.view;
import java.awt.event.*;
import javax.swing.JOptionPane;
import ch1.data.Point;
import ch1.data.Animal;
import ch1.data.ViewForAnimal;
import ch1.data.LeftAnimal;
import ch1.data.RightAnimal;
public class HandleAnimalMove extends MouseAdapter {
GamePanel panel;
HandleAnimalMove(GamePanel panel){
this.panel = panel;
}
public void mousePressed(MouseEvent e){
ViewForAnimal animalView = (ViewForAnimal)e.getSource();
Animal animal = animalView.getAnimal();
Point pStart = animal.getAtPoint(); //得到动物移动前所在点
if(animal.move()) {
Point pEnd = animal.getAtPoint();//得到动物移动后所在点
int x = pEnd.getX();
int y = pEnd.getY();
animalView.setAnimalViewLocation(x,y);//让动物视图所在位置和动物所在点相同
panel.saveAnimalStep.push(pStart);
panel.saveAnimalStep.push(pEnd);
}
}
public void mouseReleased(MouseEvent e){
boolean success = true;
int n =panel.animalCount/2;
for(int i=0;i<n;i++){
Animal animal=panel.point[i].getAtPointAnimal();
success = success&&(animal instanceof RightAnimal);
animal=panel.point[n+1+i].getAtPointAnimal();
success = success&&(animal instanceof LeftAnimal);
if(success == false)
break;
}
if(success) {
JOptionPane.showMessageDialog(null,"您成功了","消息框",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
package ch1.view;
import ch1.data.Point;
import ch1.data.Animal;
import ch1.data.ViewForAnimal;
import java.awt.event.*;
public class HandleRedo implements ActionListener {
GamePanel panel;
HandleRedo(GamePanel panel){
this.panel = panel;
}
public void actionPerformed(ActionEvent e){ //撤销移动动物的操作
if(panel.saveAnimalStep.empty())
return;
Point pEnd = panel.saveAnimalStep.pop();
Point pStart = panel.saveAnimalStep.pop();
Animal animal = pEnd.getAtPointAnimal();
pEnd.setIsHaveAnimal(false);
pEnd.setAtPointAnimal(null);
animal.setAtPoint(pStart);
ViewForAnimal animalView =animal.getAnimalView();
int x = pStart.getX();
int y = pStart.getY();
animalView.setAnimalViewLocation(x,y);//让动物视图所在位置和动物所在点相同
}
}
package ch1.view;
import ch1.data.ViewForAnimal;
import java.awt.event.*;
public class HandleReStart implements ActionListener {
GamePanel panel;
HandleReStart(GamePanel panel){
this.panel = panel;
}
public void actionPerformed(ActionEvent e){ //处理重新开始
panel.saveAnimalStep.clear();
for(int i=0;i<panel.point.length;i++) {
panel.point[i].setIsHaveAnimal(false);
}
for(int i =0;i<panel.leftAnimal.length;i++ ){
panel.leftAnimal[i].setAtPoint(panel.point[i]);
int x = panel.point[i].getX();
int y = panel.point[i].getY();
//让动物视图所在位置和动物所在点相同
ViewForAnimal animalView =panel.leftAnimal[i].getAnimalView();
animalView.setAnimalViewLocation(x,y);
}
for(int i =0;i<panel.rightAnimal.length;i++ ){
int m = panel.animalCount/2;
panel.rightAnimal[i].setAtPoint(panel.point[m+1+i]);
int x = panel.point[m+1+i].getX();
int y = panel.point[m+1+i].getY();
//让动物视图所在位置和动物所在点相同
ViewForAnimal animalView =panel.rightAnimal[i].getAnimalView();
animalView.setAnimalViewLocation(x,y);
}
}
}
1.5GUI程序
package ch1.gui;
import javax.swing.*;
import java.awt.*;
import ch1.view.GamePanel;
public class AppWindow extends JFrame {
GamePanel gamePanel;
public AppWindow(){
setTitle("动物换位游戏");
gamePanel = new GamePanel();
gamePanel.setAnimalCount(6);
gamePanel.setLeftAnimalImage("image/cat.jpg");
gamePanel.setRightAnimalImage("image/dog.jpg");
add(gamePanel,BorderLayout.CENTER);
gamePanel.setBackground(Color.white);
JPanel northP = new JPanel();
northP.add(gamePanel.buttonReStart);
northP.add(gamePanel.buttonRedo);
add(northP,BorderLayout.NORTH);
setBounds(60,60,9*gamePanel.width+9*gamePanel.gap,300);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String args[] ){
AppWindow win = new AppWindow();
}
}
1.6拓展程序