package chess.ui;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import chess.core.ChessBoard;
import chess.core.Chessman;
import chess.core.Grid;
public class MainFrame extends JFrame {
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setResizable(false);
frame.setTitle("Ready to Go?");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public MainFrame() {
super();
setBounds(100, 100, Display.WIDTH-20, Display.HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dis = new Display();
dis.setVisible(true);
contentBuff = new StringBuffer(1024*4);
contentBuff.append("(;GM[1]AP[StoneBase:SGFParser.3.0.1]SZ[19]CA[gb2312]HA[0]\n");
add(dis);
cur_Hand = Chessman.BLACK;
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
protected void processWindowEvent(final WindowEvent pEvent)
{
if(pEvent.getID() == WindowEvent.WINDOW_CLOSING)
{
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");
String filename;
filename = sdf.format(now)+".sgf";
contentBuff.append(')');
int selection = JOptionPane.showConfirmDialog(this,
"保存到: "+filename, "是否保存棋谱文件?",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(selection == JOptionPane.OK_OPTION)
{
try{
PrintWriter out = new PrintWriter(new File(filename));
out.write(contentBuff.toString());
out.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
else if(selection == JOptionPane.CANCEL_OPTION)
{
;
}
else
{
;
}
dispose();
}
else
{
super.processWindowEvent(pEvent);
}
}
private class MouseHandler extends MouseAdapter
{
public void mouseClicked(MouseEvent event)
{
if(event.getButton() == MouseEvent.BUTTON1)
if(!dis.board.grids[cur_W][cur_H].isOccupied())
{
dis.board.grids[cur_W][cur_H].putChess(new Chessman(cur_Hand));
if(cur_Hand == Chessman.BLACK)
{
contentBuff.append(";B["+(char)('a'+cur_W)+(char)('a'+cur_H)+"]");
cur_Hand = Chessman.WHITE;
}
else
{
contentBuff.append(";W["+(char)('a'+cur_W)+(char)('a'+cur_H)+"]");
cur_Hand = Chessman.BLACK;
}
dis.repaint();
}
if(event.getButton() == MouseEvent.BUTTON3)
if(dis.board.grids[cur_W][cur_H].isOccupied())
{
dis.board.grids[cur_W][cur_H].delChess();
dis.repaint();
}
}
}
private class MouseMotionHandler implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
cur_X = event.getX()-LEFT_BORDER;
cur_Y = event.getY()-TOP_BORDER;
cur_W = (cur_X-Grid.SPACE/2-1)/Grid.SPACE;
cur_H = (cur_Y-Grid.SPACE/2-1)/Grid.SPACE;
if(cur_W>=ChessBoard.STDLEN)
cur_W = ChessBoard.STDLEN-1;
if(cur_H>=ChessBoard.STDWID)
cur_H = ChessBoard.STDWID-1;
if(dis.board.grids[cur_W][cur_H].isOccupied())
setCursor(Display.noCursor);
else
{
if(cur_Hand == Chessman.BLACK)
setCursor(Display.blackCursor);
else
setCursor(Display.whiteCursor);
}
}
public void mouseDragged(MouseEvent event)
{
}
}
private static final int LEFT_BORDER = 3;
private static final int TOP_BORDER = 30;
private int cur_X, cur_Y;
private int cur_W, cur_H;
private boolean cur_Hand;
private Display dis;
private StringBuffer contentBuff;
}