⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 goapplet.java

📁 java 开发的围棋打谱程序 可供大家做参考
💻 JAVA
字号:
package org.nebula.goapplet;

import java.awt.event.*;
import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;

import org.nebula.cwt.*;
import org.nebula.cwt.grid.*;
import org.nebula.cutil.*;

public class GoApplet extends Applet implements ActionListener, ItemListener, GridListener {
	final int WAITING = 0;
	final int RUNNING = 1;

	final int DEMO = 0;
	final int TEST = 1;

	final boolean TOUCH_DEVICE = false;

	Button btNext, btPre, btFirst, btLast;
	TextArea txtComment;
	java.awt.List lstQipu;
	GoBoard board;
	DefaultBoardModel boardModel;

    Match match;
	Variation curVar;

	int runMode;

	public void init() {
		setLayout(new BorderLayout());
		
		boardModel = new DefaultBoardModel(GoBoardModel.MAX_BOARD_SIZE);
	    board = new GoBoard(this, boardModel);
	    board.setGridListener(this);
	    board.setSize(480, 480);

		add("Center", board);

		btNext = new Button("Next");
		btPre = new Button("Previous");
		btLast = new Button("Last");
		btFirst = new Button("First");        
		lstQipu = new java.awt.List();
		lstQipu.addItemListener(this);
		txtComment = new TextArea("", 20, 10, TextArea.SCROLLBARS_NONE);
		btNext.addActionListener(this);
		btPre.addActionListener(this);
		btLast.addActionListener(this);
		btFirst.addActionListener(this);
//        txtComment.setWrapStyleWord(true);
//        txtComment.setLineWrap(true);

        Panel listPanel = new Panel();
//        ScrollPane scrollPane = new ScrollPane();
        ScrollPane scrollPane = new ScrollPane(ScrollPane.SCROLLBARS_NEVER);
        scrollPane.add(lstQipu);
        scrollPane.setSize(150, 150);
        listPanel.add(scrollPane);
//        listPanel.add(lstQipu);
//        listPanel.setSize(txtComment.getWidth(), txtComment.getHeight());

        Panel infoPad = new Panel();
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gc = new GridBagConstraints();
        gc.fill = GridBagConstraints.BOTH;
        gc.insets = new Insets(5,15,5,15);
        infoPad.setLayout(gbl);

        GUITools.addComponentTo(infoPad, listPanel,    	gbl, gc, 0, 0, 2, 1, 0.0, 0.0);
        GUITools.addComponentTo(infoPad, txtComment,    gbl, gc, 0, 1, 2, 1, 0.0, 1.0);
        GUITools.addComponentTo(infoPad, btNext,     	gbl, gc, 0, 2, 1, 1, 0.5, 0.0);
        GUITools.addComponentTo(infoPad, btPre,     	gbl, gc, 1, 2, 1, 1, 0.5, 0.0);
        GUITools.addComponentTo(infoPad, btLast,     	gbl, gc, 0, 3, 1, 1, 0.5, 0.0);
        GUITools.addComponentTo(infoPad, btFirst,     	gbl, gc, 1, 3, 1, 1, 0.5, 0.0);
		add("East", infoPad);
		
		showAllQipu();
		//demoQipu("/a.sgf");
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btNext) {
			next();
			board.repaint();
		}
		else if (e.getSource() == btPre) {
			undo();
			board.repaint();
		}
		else if (e.getSource() == btFirst) {
			first();
			board.repaint();
		}
		else if (e.getSource() == btLast) {
			last();
			board.repaint();
		}
	}
	
	public void itemStateChanged(ItemEvent event) {
		demoQipu(lstQipu.getSelectedItem());
	}
	
	public void showAllQipu() {
        InputStream input = null;
        try {
        	String pack = getParameter("qipuPackage");
        	Logger.debug("qipu package: [" + pack + "]");
			URI filedata = new URI(getCodeBase() + pack);   
    		ZipUtil qipuSource = new ZipUtil(filedata);
    		Vector v = qipuSource.getEntries(".sgf");
    		for (int i = 0; i < v.size(); i++) {
    			String s = (String) v.elementAt(i);
    			lstQipu.add(s);
    		}
        }
        catch (Exception e) {
        	e.printStackTrace();
        }
	}
	
    public void demoQipu(String qipuFile) {
        InputStream input = null;
        try {
			URI filedata = new URI(getCodeBase() + "qipu.jar");   
        	
    		ZipUtil qipuSource = new ZipUtil(filedata);
    		input = qipuSource.open(qipuFile);
    		
//            input = getClass().getResourceAsStream(qipuFile);
            Match m  = SgfAdapter.readMatch(input);
            initDemo(m);
        }
        catch(Exception e){
            e.printStackTrace();
            match = null;
        }
        finally {
            try {
                input.close();
            }catch (Exception e){}
        }
    }

    public void initDemo(Match m) {
        match = m;
        boardModel.reset();
        showVariation(match);
    }

    public void exitDemo() {
    }

	/**
	 */
    public void cubicClicked(int col, int row){
    }
	public void downKeyReleased(){}
	public void upKeyReleased(){}
	public void rightKeyReleased(){}
	public void leftKeyReleased(){}
	public void selectKeyReleased(){}

    private void last() {
        while (curVar.hasNext()) {
            next();
        }
    }
    private void first() {
        while (curVar.hasPrevious()) {
            undo();
        }
    }

    /**
     * put stone, show number, tips and comment of current movement
     */
    private void showMovement() {
    	Logger.debug("showing movement");
        Movement current = curVar.getCurrentMovement();
        try {
            if (current.player != GoPlayer.UNKNOWN) {
            	Logger.debug("player is not unknown [" + current.col + "," + current.row + "]");
                boardModel.performPut(current.col, current.row, current.player);
            }
            else
            {
                for (int i = 0; i < current.getEraseCount(); i++) {
                    Movement.HandTip ht = current.eraseAt(i);
                    boardModel.forceRemove(ht.col, ht.row);
                }
        	}
        } catch (Exception e) {
        	e.printStackTrace();
        }

        Logger.debug("showing tips");
        showTips();
        
//    	updateState();
    }

    private void showVariation(Variation v) {
        curVar = v;
        curVar.curIndex = 0;
        showMovement();
    }

     private void next() {
    	Logger.debug("doing: next");
        if (!curVar.hasNext()) return;

    	hideTips();
        curVar.next();
        showMovement();
    }

    private void undo() {
        if (!curVar.hasPrevious()) return;

		hideTips();
        Movement current = curVar.getCurrentMovement();
        if (current.player != GoPlayer.UNKNOWN) {
            boardModel.undo();
        }
        else
        {
            for (int i = 0; i < current.getEraseCount(); i++) {
                boardModel.undo();
            }
    	}

        curVar.previous();
		showTips();
//		updateState();
    }

    private void closeVar() {
		if (curVar == match) return;

		hideTips();
        System.out.println("closing: [" + curVar.curIndex + "]");
        int steps = curVar.curIndex;
		for (int i = 0; i <= steps; i++) {
            Movement current = curVar.getCurrentMovement();
            System.out.println("undoing: [" + i + "," + current.player + "," + current.getEraseCount() + "]");
            if (current.player != GoPlayer.UNKNOWN) {
                boardModel.undo();
            }
            else
            {
                for (int j = 0; j < current.getEraseCount(); j++) {
                    System.out.println("restoring: [" + j);
                    boardModel.undo();
                }
        	}
            curVar.previous();
		}

		curVar = curVar.owner.owner;
		showTips();
 //   	updateState();
    }

    private void openVar() {
        Movement current = curVar.getCurrentMovement();
        if (current.getVarCount() == 0) return;

        showVariation(current.reset());
    }

    private void nextVar() {
		if (curVar == match) return;
        closeVar();

        Movement current = curVar.getCurrentMovement();
        showVariation(current.next());
    }
    private void preVar() {
		if (curVar == match) return;
        closeVar();

        Movement current = curVar.getCurrentMovement();
        showVariation(current.previous());
    }

    private void hideTips(){
        Movement current = curVar.getCurrentMovement();
        if (current == null) return;

		for (int i = 0; i < current.getTipCount(); i++) {
			Movement.HandTip t = current.tipAt(i);
			boardModel.setTip(t.col, t.row, GoPoint.NONE);
		}
    }
    private void showTips(){
        Movement current = curVar.getCurrentMovement();
        if (current == null) return;

        for (int i = 0; i < current.getForceCount(); i++) {
            Movement.HandTip t = current.forceAt(i);
            Logger.debug("force: [" + t.col + "," + t.row + "]");
            boardModel.forcePut(t.col, t.row, t.tip);
        }
        
        for (int i = 0; i < current.getTipCount(); i++) {
			Movement.HandTip t = current.tipAt(i);
			boardModel.setTip(t.col, t.row, t.tip);
            Logger.debug("set tip: [" + t.col + "," + t.row + "]");
		}
        
        String c = current.getComment();
        if (c == null) {
            return;
        }
        c.trim();
        if (c.length() == 0) {
            return;
        }

//        board.setComment(current.getComment());
        txtComment.setText(c);
//        new Thread(timer).start();
    }

 
}//class1.end 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -