我是靠谱客的博主 负责寒风,最近开发中收集的这篇文章主要介绍[StoryToolKit] STK工具箱的源代码,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

以下代码是 《StoryToolKit》 附带的工具箱的源代码,虽然有待改进的地方不少,但我相信值得大家借鉴的地方同样很多

 

里面很能体现了我写代码的特点:精简变量,精简代码结构,最大化兼容性

我所有的界面仅使用AWT组件,这样做界面有3点好处:

1。发布独立安装包时可以精简虚拟机

2。可以兼容所有的虚拟机——这里给只会用swing的人普及一下知识:IBM的java虚拟机对SWT支持不错,但对swing的支持不行;而Linux下OpenJDK执行Swing的效率是极其低下的

3。最大化配合本机用户界面

 

大家不妨看看我的内部类,匿名类以及一个文件多个类的使用方法,这样做的好处是方便管理java代码文件——虽然编译出来的类没差。

以及一些界面小技巧:文本框仅支持数字,动画显示窗口,鼠标拖曳窗口及部件,等等等等……

 

以下代码全部包括在一个java源文件内,包含main函数,可以直接执行

 

import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Label;
import java.awt.List;
import java.awt.MouseInfo;
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;

public class FunctionListForm
{
	
	public static Frame window = null;
	public static Button PackFilesBtn=new Button("Pack Files"),
						CheckPackedFilesBtn=new Button("View Package"),
						LocateImageBtn=new Button("Calculate Location"),
						CreateAnimeBtn=new Button("Create Animation");
	private static boolean animing=false, isMin=false;
	private static int mouseLocateInForm_X=0, mouseLocateInForm_Y=0;
	public static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 

	public static void main(String[] args)
	{
		window = new Frame(){public void paint(Graphics g) {
			if(animing)
				return;
			g.setColor(Color.BLACK);
			g.drawRect(0, 0, 199, 599);
			g.fill3DRect(0, 0, 200, 20, true);
			g.setColor(Color.WHITE);
			g.drawString(window.getTitle(), 4, 14);
		}};
		window.setSize(200, 0);
		window.setTitle("ToolBox - StoryToolKit");
		window.setLayout(null);
		window.setLocation(50, 50);
		window.setUndecorated(true);
		window.setBackground(Color.LIGHT_GRAY);
		window.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e)
		{
			window.dispose();
			System.exit(0);
		}});
		window.addMouseListener(new MouseAdapter(){public void mousePressed(MouseEvent e) {
			mouseLocateInForm_X = e.getX();
			mouseLocateInForm_Y = e.getY();
		}});
		window.addMouseMotionListener(new MouseMotionAdapter(){public void mouseDragged(MouseEvent e) {
			window.setLocation(MouseInfo.getPointerInfo().getLocation().x-mouseLocateInForm_X,
					MouseInfo.getPointerInfo().getLocation().y-mouseLocateInForm_Y);
		}});
		
		Button minBtn = new Button();
		minBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			animing=true;
			if(isMin)
			{
				synchronized (this) {try{
					for(int i=20; i<600; i+=50)
					{
						window.setSize(200, i);
						wait(20);
					}
					animing=false;
					window.setSize(200, 600);
					isMin=false;
				} catch(Exception e){}}
			}
			else
			{
				synchronized (this) {try{
					for(int i=600; i>0; i-=50)
					{
						window.setSize(200, i);
						wait(20);
					}
					animing=false;
					window.setSize(200, 20);
					isMin=true;
				} catch(Exception e){}}
			}
		}});
		minBtn.setLocation(180,1);
		minBtn.setSize(19, 18);
		minBtn.setFocusable(false);
		window.add(minBtn);
		
		final Button exitBtn = new Button("Exit");
		exitBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			exitBtn.setEnabled(false);
			new Thread(){public void run(){
				synchronized (this) {try{
					animing=true;
					for(int i=600; i>0; i-=50)
					{
						window.setSize(200, i);
						wait(20);
					}
					System.exit(0);
				} catch(Exception e){}}
			}}.start();
		}});
		exitBtn.setLocation(1,579);
		exitBtn.setSize(198, 20);
		window.add(exitBtn);
		
		PackFilesBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			PackFilesBtn.setEnabled(false);
			new PackFiles();
		}});
		PackFilesBtn.setLocation(1,22);
		PackFilesBtn.setSize(198, 19);
		window.add(PackFilesBtn);
		
		CheckPackedFilesBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			CheckPackedFilesBtn.setEnabled(false);
			new CheckPackedFiles();
		}});
		CheckPackedFilesBtn.setLocation(1,42);
		CheckPackedFilesBtn.setSize(198, 19);
		window.add(CheckPackedFilesBtn);
		
		LocateImageBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			LocateImageBtn.setEnabled(false);
			new CalLocation();
		}});
		LocateImageBtn.setLocation(1,62);
		LocateImageBtn.setSize(198, 19);
		window.add(LocateImageBtn);
		
		CreateAnimeBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			CreateAnimeBtn.setEnabled(false);
			new AnimeCreater();
		}});
		CreateAnimeBtn.setLocation(1,82);
		CreateAnimeBtn.setSize(198, 19);
//		CreateAnimeBtn.setEnabled(false);
		window.add(CreateAnimeBtn);
		
		window.setVisible(true);
		new Thread(){public void run(){
			synchronized (this) {try{
				animing=true;
				for(int i=0; i<600; i+=50)
				{
					window.setSize(200, i);
					wait(20);
				}
				animing=false;
				window.setSize(200, 600);
			} catch(Exception e){}}
		}}.start();
	}
	
}

class MsgBox
{
	private int mouseLocateInForm_X=0, mouseLocateInForm_Y=0;
	public MsgBox(String msg)
	{
		final Window msgBox = new Window(FunctionListForm.window){public void paint(Graphics g) {
			g.drawRect(0, 0, 299, 149);
		}};
		msgBox.setSize(300, 150);
		msgBox.setLayout(null);
		msgBox.setLocation((FunctionListForm.screenSize.width-300)/2, (FunctionListForm.screenSize.height-150)/2);
		msgBox.addMouseListener(new MouseAdapter(){public void mousePressed(MouseEvent e) {
			mouseLocateInForm_X = e.getX();
			mouseLocateInForm_Y = e.getY();
		}});
		msgBox.addMouseMotionListener(new MouseMotionAdapter(){public void mouseDragged(MouseEvent e) {
			msgBox.setLocation(MouseInfo.getPointerInfo().getLocation().x-mouseLocateInForm_X,
					MouseInfo.getPointerInfo().getLocation().y-mouseLocateInForm_Y);
		}});
		
		Label msgLabel = new Label(msg);
		msgLabel.setSize(260, 100);
		msgLabel.setLocation(20, 10);
		msgLabel.addMouseListener(new MouseAdapter(){public void mousePressed(MouseEvent e) {
			mouseLocateInForm_X = e.getX()+20;
			mouseLocateInForm_Y = e.getY()+10;
		}});
		msgLabel.addMouseMotionListener(new MouseMotionAdapter(){public void mouseDragged(MouseEvent e) {
			msgBox.setLocation(MouseInfo.getPointerInfo().getLocation().x-mouseLocateInForm_X,
					MouseInfo.getPointerInfo().getLocation().y-mouseLocateInForm_Y);
		}});
		msgBox.add(msgLabel);
		
		final Button okBtn = new Button("OK");
		okBtn.setSize(100, 20);
		okBtn.setLocation(100, 115);
		okBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			msgBox.dispose();
		}});
		msgBox.add(okBtn);
		msgBox.setVisible(true);
		okBtn.requestFocus();
	}
}

class PackFiles
{
	private Frame window = null;
	private Choice typeChice = new Choice();
	private TextField distPathInput = new TextField();
	private List fileList = new List();
	private Label statusLabel = new Label("No start");
	private Button addBtn = new Button("Add"), upBtn = new Button("Up"), delBtn = new Button("Del"),
						downBtn = new Button("Down"), startBtn = new Button("Pack");
	
	public PackFiles()
	{
		window = new Frame(){ public void paint(Graphics g) {
			g.drawLine(20, 100, 480, 100);
			g.drawLine(20, 320, 480, 320);
		}};
		window.setSize(500, 400);
		window.setLayout(null);
		window.setLocation((FunctionListForm.screenSize.width-500)/2, (FunctionListForm.screenSize.height-400)/2);
		window.setBackground(Color.LIGHT_GRAY);
		window.setResizable(false);
		window.setTitle("PackFiles - StoryToolKit");
		window.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e)
		{
			window.dispose();
			FunctionListForm.PackFilesBtn.setEnabled(true);
		}});

		Label typeLabel = new Label("Target type:");
		typeLabel.setSize(80, 20);
		typeLabel.setLocation(20, 40);
		window.add(typeLabel);
		
		typeChice.setSize(60, 20);
		typeChice.setLocation(110, 40);
		typeChice.add("Image");
		typeChice.add("Audio");
		typeChice.add("Video");
		typeChice.add("Text");
		window.add(typeChice);

		Label pathLabel = new Label("Source path:");
		pathLabel.setSize(80, 20);
		pathLabel.setLocation(20, 70);
		window.add(pathLabel);
		
		final TextField pathInput = new TextField();
		pathInput.setSize(310, 20);
		pathInput.setLocation(110, 70);
		window.add(pathInput);
		
		addBtn.setSize(50, 20);
		addBtn.setLocation(430, 70);
		addBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			if(addFile(pathInput.getText()))
				pathInput.setText(null);
		}});
		window.add(addBtn);
		
		fileList.setSize(390, 200);
		fileList.setLocation(30, 110);
		window.add(fileList);
		
		upBtn.setSize(40, 70);
		upBtn.setLocation(435, 110);
		upBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			upItem();
		}});
		window.add(upBtn);
		
		delBtn.setSize(40, 40);
		delBtn.setLocation(435, 190);
		delBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			int index = fileList.getSelectedIndex();
			if(index>-1)
			{
				fileList.remove(index);
				String tFilePath;
				for(int i=index; i<fileList.getItemCount(); i++)
				{
					tFilePath = fileList.getItem(i);
					tFilePath = tFilePath.substring(tFilePath.indexOf(" - ")+3);
					fileList.remove(i);
					fileList.add(i + " - " + tFilePath, i);
				}
			}
		}});
		window.add(delBtn);
		
		downBtn.setSize(40, 70);
		downBtn.setLocation(435, 240);
		downBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			downItem();
		}});
		window.add(downBtn);

		Label distPathLabel = new Label("Packed file save path:");
		distPathLabel.setSize(120, 20);
		distPathLabel.setLocation(20, 330);
		window.add(distPathLabel);
		
		distPathInput.setSize(330, 20);
		distPathInput.setLocation(150, 330);
		window.add(distPathInput);

		Label statusTitleLabel = new Label("Status:");
		statusTitleLabel.setSize(60, 20);
		statusTitleLabel.setLocation(20, 360);
		window.add(statusTitleLabel);

		statusLabel.setSize(330, 20);
		statusLabel.setLocation(90, 360);
		window.add(statusLabel);
		
		startBtn.setSize(50, 20);
		startBtn.setLocation(430, 360);
		startBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			PackingConfirm(distPathInput.getText());
		}});
		window.add(startBtn);
		
		window.setVisible(true);
	}
	
	private boolean addFile(String filePath)
	{
		if(filePath==null || filePath.trim().length()==0)
		{
			new MsgBox("Please input file path.");
			return false;
		}
		File file = new File(filePath);
		if(file.exists())
		{
			if(file.isDirectory())
			{
				File[] files = file.listFiles();
				for(int i=0; i<files.length; i++)
				{
					if(!files[i].isDirectory())
						fileList.add(fileList.getItemCount() + " - " + files[i].getAbsolutePath());
				}
				return true;
			}
			else
			{
				fileList.add(fileList.getItemCount() + " - " + filePath);
				return true;
			}
		}
		else
		{
			new MsgBox("File could not be found.");
			return false;
		}
	}
	
	private void upItem()
	{
		int index = fileList.getSelectedIndex();
		if(index<1)
			return;
		String tFilePath = fileList.getItem(index);
		tFilePath = tFilePath.substring(tFilePath.indexOf(" - ")+3);
		fileList.remove(index);
		fileList.add((index-1) + " - " + tFilePath, index-1);
		for(int i=index; i<fileList.getItemCount(); i++)
		{
			tFilePath = fileList.getItem(i);
			tFilePath = tFilePath.substring(tFilePath.indexOf(" - ")+3);
			fileList.remove(i);
			fileList.add(i + " - " + tFilePath, i);
		}
		fileList.select(index-1);
	}
	
	private void downItem()
	{
		int index = fileList.getSelectedIndex();
		if(index<0||index==(fileList.getItemCount()-1))
			return;
		String tFilePath = fileList.getItem(index);
		tFilePath = tFilePath.substring(tFilePath.indexOf(" - ")+3);
		fileList.remove(index);
		fileList.add((index+1) + " - " + tFilePath, index+1);
		for(int i=index; i<fileList.getItemCount(); i++)
		{
			tFilePath = fileList.getItem(i);
			tFilePath = tFilePath.substring(tFilePath.indexOf(" - ")+3);
			fileList.remove(i);
			fileList.add(i + " - " + tFilePath, i);
		}
		fileList.select(index+1);
	}

	private void PackingConfirm(String filePath)
	{
		if(fileList.getItemCount()==0)
		{
			new MsgBox("No files need to be packed.");
			return;
		}
		if(filePath==null || filePath.trim().length()==0)
		{
			new MsgBox("Please input file path.");
			return;
		}
		if(filePath.replace("\", "/").indexOf("/")<0)
		{
			new MsgBox("Target file format error.");
			return;
		}
		File saveFile = new File(filePath);
		if(saveFile.exists())
		{
			if(saveFile.isDirectory())
				new MsgBox("Target file could not be directory.");
			else
				new MsgBox("Target file already exist.");
			return;
		}
		File folder = new File(saveFile.getParent());
		if(!folder.exists() && !folder.mkdirs())
		{
			new MsgBox("Fail to crete target folder.");
			return;
		}
		final Window confirmBox = new Window(window){public void paint(Graphics g) {
			g.drawRect(1, 1, 398, 198);
		}};
		confirmBox.setSize(400, 200);
		confirmBox.setLayout(null);
		confirmBox.setLocation(530, 400);
		confirmBox.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e)
		{
			confirmBox.dispose();
		}});

		Label typeLabel = new Label("File type:");
		typeLabel.setSize(60, 20);
		typeLabel.setLocation(20, 30);
		confirmBox.add(typeLabel);

		Label typeValueLabel = new Label(typeChice.getSelectedItem());
		typeValueLabel.setSize(60, 20);
		typeValueLabel.setLocation(90, 30);
		confirmBox.add(typeValueLabel);

		Label pathLabel = new Label("Save target:");
		pathLabel.setSize(60, 20);
		pathLabel.setLocation(20, 60);
		confirmBox.add(pathLabel);

		Label pathValueLabel = new Label(distPathInput.getText());
		pathValueLabel.setSize(60, 20);
		pathValueLabel.setLocation(90, 60);
		confirmBox.add(pathValueLabel);

		Label countLabel = new Label("File count:");
		countLabel.setSize(60, 20);
		countLabel.setLocation(20, 90);
		confirmBox.add(countLabel);

		Label countValueLabel = new Label(String.valueOf(fileList.getItemCount()));
		countValueLabel.setSize(60, 20);
		countValueLabel.setLocation(90, 90);
		confirmBox.add(countValueLabel);
		
		final Button okBtn = new Button("OK");
		okBtn.setSize(100, 20);
		okBtn.setLocation(70, 150);
		okBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			confirmBox.dispose();
			typeChice.setEnabled(false);
			addBtn.setEnabled(false);
			upBtn.setEnabled(false);
			delBtn.setEnabled(false);
			downBtn.setEnabled(false);
			startBtn.setEnabled(false);
			distPathInput.setEnabled(false);
			new Thread(){public void run(){
				process();
			}}.start();
		}});
		confirmBox.add(okBtn);
		
		final Button cancelBtn = new Button("Cancel");
		cancelBtn.setSize(100, 20);
		cancelBtn.setLocation(230, 150);
		cancelBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			confirmBox.dispose();
		}});
		confirmBox.add(cancelBtn);
		
		confirmBox.setVisible(true);
	}
	
	private void process()
	{
		FileOutputStream fos = null;
		try {
			int fileType = typeChice.getSelectedIndex();
			fos = new FileOutputStream(distPathInput.getText());

			statusLabel.setText("Counting index ......");
			int fileCount = fileList.getItemCount();
			fos.write(fileList.getItemCount()/0x100);
			fos.write(fileList.getItemCount()%0x100);
			fos.write(fileType);

			int index=fileCount*4+3;
			for(int i=0; i<fileCount; i++)
			{
				File file = new File(fileList.getItem(i).substring(fileList.getItem(i).indexOf(" - ")+3));
				if(index>Integer.MAX_VALUE)
					throw new Exception("Target file too large.");
				int lInt=0;
				
				fos.write(index/0x1000000);
				lInt=index%0x1000000;
				
				fos.write(lInt/0x10000);
				lInt=index%0x10000;
				
				fos.write(lInt/0x100);
				lInt=index%0x100;
				
				fos.write(lInt);
				
				System.out.println();
				index += (int)file.length();
			}

			for(int i=0; i<fileCount; i++)
			{
				File file = new File(fileList.getItem(i).substring(fileList.getItem(i).indexOf(" - ")+3));
				byte[] content = new byte[(int)file.length()];
				statusLabel.setText("Packing file ["+i+"] with length: "+content.length);
				FileInputStream fis = new FileInputStream(file);
				fis.read(content);
				for(int j=0; j<content.length; j++)
					content[j]=(byte)(content[j]+fileType+7);
				fos.write(content);
				fis.close();
			}
			fos.flush();
			statusLabel.setText("Files packed completed successful");
		} catch (Exception e) {
			statusLabel.setText("Error:"+e.toString());
			new File(distPathInput.getText()).delete();
		}
		try {
			fos.close();
		} catch (Exception e1) {}
		typeChice.setEnabled(true);
		addBtn.setEnabled(true);
		upBtn.setEnabled(true);
		delBtn.setEnabled(true);
		downBtn.setEnabled(true);
		startBtn.setEnabled(true);
		distPathInput.setEnabled(true);
	}

}

class CheckPackedFiles
{
	private Frame window=null;
	private Choice indexChice = new Choice(), typeChice = new Choice();
	private String loadedPackagePath = null;
	private Button viewBtn = new Button("View"), unpackBtn = new Button("Unpack");

	public CheckPackedFiles()
	{
		window = new Frame(){ public void paint(Graphics g) {
			g.drawLine(20, 70, 480, 70);
		}};
		window.setSize(500, 110);
		window.setLayout(null);
		window.setLocation((FunctionListForm.screenSize.width-500)/2, (FunctionListForm.screenSize.height-110)/2);
		window.setResizable(false);
		window.setBackground(Color.LIGHT_GRAY);
		window.setTitle("PackageViewer - StoryToolKit");
		window.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e)
		{
			window.dispose();
			FunctionListForm.CheckPackedFilesBtn.setEnabled(true);
		}});

		Label pathLabel = new Label("Package path:");
		pathLabel.setSize(80, 20);
		pathLabel.setLocation(20, 40);
		window.add(pathLabel);
		
		final TextField pathInput = new TextField();
		pathInput.setSize(310, 20);
		pathInput.setLocation(110, 40);
		window.add(pathInput);
		
		final Button loadBtn = new Button("Load");
		loadBtn.setSize(50, 20);
		loadBtn.setLocation(430, 40);
		loadBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			loadBtn.setEnabled(false);
			pathInput.setEnabled(false);
			indexChice.removeAll();
			typeChice.setEnabled(true);
			new Thread(){public void run(){
				loadPackage(pathInput.getText());
				loadBtn.setEnabled(true);
				pathInput.setEnabled(true);
			}}.start();
		}});
		window.add(loadBtn);

		Label indexLabel = new Label("File index:");
		indexLabel.setSize(80, 20);
		indexLabel.setLocation(20, 80);
		window.add(indexLabel);
		
		indexChice.setSize(50, 20);
		indexChice.setLocation(110, 80);
		window.add(indexChice);

		Label typeLabel = new Label("Package type:");
		typeLabel.setSize(80, 20);
		typeLabel.setLocation(180, 80);
		window.add(typeLabel);
		
		typeChice.setSize(60, 20);
		typeChice.setLocation(270, 80);
		typeChice.add("Image");
		typeChice.add("Audio");
		typeChice.add("Video");
		typeChice.add("Text");
		window.add(typeChice);
		
		viewBtn.setSize(50, 20);
		viewBtn.setLocation(370, 80);
		viewBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			if(loadedPackagePath==null)
				return;
			indexChice.setEnabled(false);
			viewBtn.setEnabled(false);
			unpackBtn.setEnabled(false);
			loadBtn.setEnabled(false);
			new Thread(){public void run(){
				startFile(indexChice.getSelectedIndex(), typeChice.getSelectedIndex());
				indexChice.setEnabled(true);
				viewBtn.setEnabled(true);
				unpackBtn.setEnabled(true);
				loadBtn.setEnabled(true);
			}}.start();
		}});
		window.add(viewBtn);
		
		unpackBtn.setSize(50, 20);
		unpackBtn.setLocation(430, 80);
		unpackBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			if(loadedPackagePath==null)
				return;
			indexChice.setEnabled(false);
			viewBtn.setEnabled(false);
			unpackBtn.setEnabled(false);
			loadBtn.setEnabled(false);
			new Thread(){public void run(){
				unpackAll(typeChice.getSelectedIndex());
				indexChice.setEnabled(true);
				viewBtn.setEnabled(true);
				unpackBtn.setEnabled(true);
				loadBtn.setEnabled(true);
			}}.start();
		}});
		window.add(unpackBtn);
		
		window.setVisible(true);
	}
	
	private void loadPackage(String filePath)
	{
		if(filePath==null || filePath.trim().length()==0)
		{
			new MsgBox("Please input the package path.");
			loadedPackagePath=null;
			return;
		}
		File file = new File(filePath);
		if(!file.exists() || !file.isFile())
		{
			new MsgBox("Package could not be found.");
			loadedPackagePath=null;
			return;
		}
		try {
			FileInputStream fis = new FileInputStream(file); 
			int count = fis.read();
			count = count*0x100+fis.read();
			typeChice.select(fis.read());
			fis.close();
			typeChice.setEnabled(false);
			for(int i=0; i<count; i++)
				indexChice.add(String.valueOf(i));
			loadedPackagePath=filePath;
		} catch (Exception e) {
			new MsgBox(e.toString());
			loadedPackagePath=null;
		}
	}
	
	private void startFile(int index, int type)
	{
		try {
			File f = new File(loadedPackagePath); 
			FileInputStream fis = new FileInputStream(f); 
			int count = fis.read();
			count = count*0x100+fis.read();
			fis.skip(index*4+1);
			
			int i1 = fis.read(), i2 = fis.read(), i3 = fis.read(), i4 = fis.read();
			int fIndex = i1*0x1000000+i2*0x10000+i3*0x100+i4;
			int fLength = 0;
			System.out.println(fIndex);
			
			if(index==count-1)
			{
				fLength = (int)f.length()-fIndex;
			}
			else
			{
				int ii1 = fis.read(), ii2 = fis.read(), ii3 = fis.read(), ii4 = fis.read();
				fLength = ii1*0x1000000+ii2*0x10000+ii3*0x100+ii4-fIndex;
				System.out.println(fLength);
				System.out.println((count-index-2)*4);
				fis.skip((count-index-2)*4);
			}
			System.out.println(fIndex-count*4-3);
			fis.skip(fIndex-count*4-3);
			byte[] bytes = new byte[fLength];
			fis.read(bytes);
			fis.close();
			File folder = new File("extract");
			folder.mkdirs();
			FileOutputStream writer = new FileOutputStream("extract/"+f.getName()+"."+index);	
			for(int i=0; i<bytes.length; i++)
			{
				bytes[i]=(byte)(bytes[i]-7-type);
				writer.write(bytes[i]);
			}
			writer.flush();
			writer.close();
			
			Runtime.getRuntime().exec("explorer "+folder.getAbsolutePath()).waitFor();
			
		} catch (Exception e) {
			new MsgBox(e.toString());
			e.printStackTrace();
		}
	}
	
	private void unpackAll(int type)
	{
		try {
			File f = new File(loadedPackagePath); 
			FileInputStream fis = new FileInputStream(f); 
			int count = fis.read();
			count = count*0x100+fis.read();
			fis.skip(1);
			int[] fileLengthes = new int[count];
			int i1 = fis.read(), i2 = fis.read(), i3 = fis.read(), i4 = fis.read();
			int fIndex = i1*0x1000000+i2*0x10000+i3*0x100+i4;
			for(int i=0; i<count; i++)
			{
				if(i==count-1)
				{
					fileLengthes[i] = (int)f.length()-fIndex;
				}
				else
				{
					int ii1 = fis.read(), ii2 = fis.read(), ii3 = fis.read(), ii4 = fis.read();
					int tIndex = ii1*0x1000000+ii2*0x10000+ii3*0x100+ii4;
					fileLengthes[i] = tIndex-fIndex;
					fIndex = tIndex;
				}
			}
			File path = new File("extract");
			path.mkdirs();
			for(int i=0; i<count; i++)
			{
				byte[] bytes = new byte[fileLengthes[i]];
				fis.read(bytes);
				FileOutputStream writer = new FileOutputStream("extract/"+f.getName()+"."+i);	
				for(int j=0; j<bytes.length; j++)
				{
					bytes[j]=(byte)(bytes[j]-7-type);
					writer.write(bytes[j]);
				}
				writer.flush();
				writer.close();
			}
			fis.close();
			
			Runtime.getRuntime().exec("explorer "+path.getAbsolutePath()).waitFor();
			
		} catch (Exception e) {
			new MsgBox(e.toString());
			e.printStackTrace();
		}
	}
}

class CalLocation
{
	private Frame window = null;
	private ArrayList images = new ArrayList();
	private TextField pathInput=new TextField(), strInput=new TextField(), sizeInput=new TextField("20"), fontInput=new TextField(),
				xInput=new TextField(), yInput=new TextField();
	private Button loadBtn=new Button("Open"), addBtn=new Button("Add"), delBtn=new Button("Delete"), locateBtn=new Button("Locate");
	private Label sizeLabel=new Label("Size:"), colorLabel=new Label("Color:"), fontLabel=new Label("Font:"),
				infoLabel = new Label("No selected"), xLabel = new Label("X:"), yLabel = new Label("Y:");
	private Choice colorChoice=new Choice();
	private ImageBean selectedImg = null;
	private Panel imgPanel = new Panel(){public void paint(Graphics g) {
		g.fillRect(0, 0, imgPanel.getWidth(), imgPanel.getHeight());
		for(int i=0; i<images.size(); i++)
		{
			ImageBean img = (ImageBean)images.get(i);
			g.drawImage(img.image, img.position_X, img.position_Y, img.width, img.height, null);
		}
		if(selectedImg!=null)
		{
			g.setColor(Color.WHITE);
			g.drawRect(selectedImg.position_X, selectedImg.position_Y, selectedImg.width, selectedImg.height);
		}
	}};
	private int mouseLocateInImg_X, mouseLocateInImg_Y;
	private ScrollPane sp = new ScrollPane(1);
	private boolean sizeKeyIned=false;
	private int sizeSelectIndex=0;
	
	public CalLocation()
	{
		window = new Frame(){ public void paint(Graphics g) {
			if(window.getWidth()<500 || window.getHeight()<300)
				window.setSize(500, 300);
			pathInput.setSize(window.getWidth()-190, 20);
			loadBtn.setLocation(window.getWidth()-70, 40);
			strInput.setSize((window.getWidth()-220)/2-120, 20);
			sizeLabel.setLocation((window.getWidth()-220)/2, 70);
			sizeInput.setLocation((window.getWidth()-220)/2+40, 70);
			colorLabel.setLocation((window.getWidth()-220)/2+100, 70);
			colorChoice.setLocation((window.getWidth()-220)/2+140, 70);
			fontLabel.setLocation((window.getWidth()-220)/2+200, 70);
			fontInput.setLocation((window.getWidth()-220)/2+240, 70);
			fontInput.setSize(window.getWidth()/2-210, 20);
			addBtn.setLocation(window.getWidth()-70, 70);
			infoLabel.setSize(window.getWidth()-260, 20);
			infoLabel.setLocation(20, window.getHeight()-30);
			sp.setSize(window.getWidth()-40, window.getHeight()-170);
			sp.doLayout();
			delBtn.setLocation(window.getWidth()-120, window.getHeight()-60);
			xLabel.setLocation(window.getWidth()-240, window.getHeight()-30);
			xInput.setLocation(window.getWidth()-220, window.getHeight()-30);
			yLabel.setLocation(window.getWidth()-160, window.getHeight()-30);
			yInput.setLocation(window.getWidth()-140, window.getHeight()-30);
			locateBtn.setLocation(window.getWidth()-80, window.getHeight()-30);
		}};
		window.setLayout(null);
		window.setSize(640, 480);
		window.setLocation((FunctionListForm.screenSize.width-640)/2, (FunctionListForm.screenSize.height-480)/2);
		window.setBackground(Color.LIGHT_GRAY);
		window.setTitle("Locater - StoryToolKit");
		window.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e)
		{
			window.dispose();
			FunctionListForm.LocateImageBtn.setEnabled(true);
		}});
		window.addComponentListener(new ComponentAdapter(){public void componentResized(ComponentEvent e) {
			window.repaint();
		}});

		Label pathLabel = new Label("Image path:");
		pathLabel.setSize(80, 20);
		pathLabel.setLocation(20, 40);
		window.add(pathLabel);
		
		pathInput.setLocation(110, 40);
		pathInput.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e) {
			if(e.getKeyCode()==KeyEvent.VK_ENTER)
				OpenImage(pathInput.getText());
		}});
		window.add(pathInput);
		
		loadBtn.setSize(50, 20);
		loadBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			OpenImage(pathInput.getText());
		}});
		window.add(loadBtn);

		Label strLabel = new Label("Text string:");
		strLabel.setSize(80, 20);
		strLabel.setLocation(20, 70);
		window.add(strLabel);
		
		strInput.setLocation(110, 70);
		window.add(strInput);

		sizeLabel.setSize(40, 20);
		window.add(sizeLabel);
		
		sizeInput.setSize(50, 20);
		sizeInput.addTextListener(new TextListener(){public void textValueChanged(TextEvent e) {
			if(!sizeKeyIned)
				return;
			StringBuffer buffer = new StringBuffer(sizeInput.getText());
			for(int i=0; i<buffer.length(); i++)
			{
				char c = buffer.charAt(i);
				if(c<'0'||c>'9')
				{
					buffer.deleteCharAt(i--);
					sizeSelectIndex--;
				}
				else if(i==0&&c=='0')
				{
					buffer.deleteCharAt(i--);
					sizeSelectIndex--;
				}
			}
			sizeKeyIned=false;
			sizeInput.setText(buffer.toString());
			sizeInput.setSelectionStart(sizeSelectIndex);
		}});
		sizeInput.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e) {
			sizeKeyIned=true;
			sizeSelectIndex = sizeInput.getSelectionStart()+1;
		}});
		sizeInput.addFocusListener(new FocusAdapter(){public void focusLost(FocusEvent e) {
			if(sizeInput.getText().equals(""))
				sizeInput.setText("20");
		}});
		window.add(sizeInput);
		
		colorLabel.setSize(40, 20);
		window.add(colorLabel);
		
		colorChoice.setSize(50, 20);
		colorChoice.add("BLACK");
		colorChoice.add("WHITE");
		colorChoice.add("RED");
		colorChoice.add("GREEN");
		colorChoice.add("BLUE");
		window.add(colorChoice);
		
		fontLabel.setSize(40, 20);
		window.add(fontLabel);
		
		window.add(fontInput);
		
		addBtn.setSize(50, 20);
		addBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			addBtn.setEnabled(false);
			addString(strInput.getText(), fontInput.getText());
			addBtn.setEnabled(true);
		}});
		window.add(addBtn);
		
		delBtn.setSize(100, 20);
		delBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			if(selectedImg!=null)
			{
				images.remove(selectedImg);
				noImgSelected();
			}
		}});
		window.add(delBtn);
		
		delBtn.setSize(100, 20);
		delBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			if(selectedImg!=null)
			{
				images.remove(selectedImg);
				noImgSelected();
			}
		}});
		window.add(delBtn);
		
//		imgPanel.setLocation(20, 70);
		imgPanel.addMouseListener(new MouseAdapter(){public void mousePressed(MouseEvent e) {
			selectImg(e.getX(), e.getY());
			if(selectedImg!=null)
			{
				mouseLocateInImg_X = e.getX()-selectedImg.position_X;
				mouseLocateInImg_Y = e.getY()-selectedImg.position_Y;
			}
		}});
		imgPanel.addMouseMotionListener(new MouseMotionAdapter(){public void mouseDragged(MouseEvent e) {
			if(selectedImg!=null)
			{
				selectedImg.position_X=e.getX()-mouseLocateInImg_X;
				selectedImg.position_Y=e.getY()-mouseLocateInImg_Y;
				xInput.setText(String.valueOf(selectedImg.position_X));
				yInput.setText(String.valueOf(selectedImg.position_Y));
				imgPanel.repaint();
			}
		}});
		imgPanel.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e) {
			if(selectedImg==null)
				return;
			switch(e.getKeyCode())
			{
			case KeyEvent.VK_UP:
				yInput.setText(String.valueOf(--selectedImg.position_Y));
				break;
			case KeyEvent.VK_DOWN:
				yInput.setText(String.valueOf(++selectedImg.position_Y));
				break;
			case KeyEvent.VK_LEFT:
				xInput.setText(String.valueOf(--selectedImg.position_X));
				break;
			case KeyEvent.VK_RIGHT:
				xInput.setText(String.valueOf(++selectedImg.position_X));
				break;
			case KeyEvent.VK_DELETE:
				if(selectedImg!=null)
				{
					images.remove(selectedImg);
					noImgSelected();
				}
				break;
			}
			imgPanel.repaint();
		}});
		
		sp.setLocation(20, 100);
		sp.add(imgPanel);
		window.add(sp);
		

		xLabel.setSize(20, 20);
		window.add(xLabel);
		
		xInput.setSize(50, 20);
		xInput.addFocusListener(new FocusAdapter(){public void focusLost(FocusEvent e) {
			if(selectedImg!=null)
			{
				try {
					selectedImg.position_X=Integer.parseInt(xInput.getText());
					imgPanel.repaint();
				} catch (Exception e1) {
					new MsgBox("Number format error.");
				}
			}
		}});
		xInput.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e) {
			if(e.getKeyCode()==KeyEvent.VK_ENTER && selectedImg!=null)
			{
				try {
					selectedImg.position_X=Integer.parseInt(xInput.getText());
					imgPanel.repaint();
				} catch (Exception e1) {
					new MsgBox("Number format error.");
				}
			}
		}});
		window.add(xInput);

		yLabel.setSize(20, 20);
		window.add(yLabel);
		
		yInput.setSize(50, 20);
		yInput.addFocusListener(new FocusAdapter(){public void focusLost(FocusEvent e) {
			if(selectedImg!=null)
			{
				try {
					selectedImg.position_Y=Integer.parseInt(yInput.getText());
					imgPanel.repaint();
				} catch (Exception e1) {
					new MsgBox("Number format error.");
				}
			}
		}});
		yInput.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e) {
			if(e.getKeyCode()==KeyEvent.VK_ENTER && selectedImg!=null)
			{
				try {
					selectedImg.position_Y=Integer.parseInt(yInput.getText());
					imgPanel.repaint();
				} catch (Exception e1) {
					new MsgBox("Number format error.");
				}
			}
		}});
		window.add(yInput);
		window.add(infoLabel);
		
		locateBtn.setSize(60, 20);
		locateBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			try {
				selectedImg.position_X=Integer.parseInt(xInput.getText());
				selectedImg.position_Y=Integer.parseInt(yInput.getText());
				imgPanel.repaint();
			} catch (Exception e1) {
				new MsgBox("Number format error.");
			}
		}});
		window.add(locateBtn);
		
		window.setVisible(true);
	}
	
	private class ImageBean
	{
		private Image image;
		private String fileName;
		private int position_X;
		private int position_Y;
		private int width;
		private int height;
	}
	
	private void OpenImage(String filePath)
	{
		if(filePath==null || filePath.trim().length()==0)
		{
			new MsgBox("Please input the image path.");
			return;
		}
		File file = new File(filePath);
		if(!file.exists() || !file.isFile())
		{
			new MsgBox("Image could not be found.");
			return;
		}
		ImageBean img = new ImageBean();
		try {
			img.image = ImageIO.read(file);
			img.fileName = file.getName();
			img.position_X=0;
			img.position_Y=0;
			img.width = img.image.getWidth(null);
			img.height = img.image.getHeight(null);
			images.add(img);
			pathInput.setText(null);
			imgPanel.setPreferredSize(new Dimension(Math.max(img.width, imgPanel.getWidth()),Math.max(img.height, imgPanel.getHeight())));
			imgPanel.repaint();
			sp.doLayout();
			window.repaint();
		} catch (Exception e) {
			new MsgBox(e.toString());
			e.printStackTrace();
		}
	}
	
	private void addString(String string, String fontFile)
	{
		if(string.trim().length()==0)
		{
			new MsgBox("Please input the text.");
			return;
		}
		int fontSize = Integer.parseInt(sizeInput.getText());
		Font font=null;
		if(fontFile.trim().length()>0)
		{
			File file = new File(fontFile);
			if(!file.exists() || !file.isFile())
			{
				new MsgBox("Font file could not be found.");
				return;
			}
			try {
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
				font = Font.createFont(Font.TRUETYPE_FONT, bis);
				font = font.deriveFont(Font.PLAIN, fontSize);
				bis.close();
			} catch (Exception e) {
				new MsgBox(e.toString());
				return;
			}
		}
		else
		{
			font=new Font("", Font.PLAIN, fontSize);
		}
		FontMetrics metrics = new BufferedImage(1,1,BufferedImage.TYPE_INT_ARGB).getGraphics().getFontMetrics(font);
		int width = metrics.stringWidth(string);
		int height = metrics.getHeight();
		BufferedImage strImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
		Graphics g = strImg.getGraphics();
		g.setFont(font);
		switch(colorChoice.getSelectedIndex())
		{
		case 0:
			g.setColor(Color.BLACK);
			break;
		case 1:
			g.setColor(Color.WHITE);
			break;
		case 2:
			g.setColor(Color.RED);
			break;
		case 3:
			g.setColor(Color.GREEN);
			break;
		case 4:
			g.setColor(Color.BLUE);
			break;
		}
		g.drawString(string, 0, -(int) (font.getStringBounds(string, ((Graphics2D)g).getFontRenderContext()).getY()));
		ImageBean img = new ImageBean();
			img.image = strImg;
			img.fileName = string;
			img.position_X=0;
			img.position_Y=0;
			img.width = img.image.getWidth(null);
			img.height = img.image.getHeight(null);
			images.add(img);
			strInput.setText(null);
			imgPanel.setPreferredSize(new Dimension(Math.max(img.width, imgPanel.getWidth()),Math.max(img.height, imgPanel.getHeight())));
			imgPanel.repaint();
			sp.doLayout();
			window.repaint();
	}
	
	private void selectImg(int x, int y)
	{
		for(int i=images.size()-1; i>-1; i--)
		{
			ImageBean img = (ImageBean) images.get(i);
			if(img.position_X<x&&img.position_X+img.width>x&&img.position_Y<y&&img.position_Y+img.height>y)
			{
				selectedImg=img;
				infoLabel.setText(img.fileName+" ("+img.width+"x"+img.height+")");
				xInput.setText(String.valueOf(img.position_X));
				yInput.setText(String.valueOf(img.position_Y));
				imgPanel.repaint();
				return;
			}
		}
		noImgSelected();
	}
	
	private void noImgSelected()
	{
		infoLabel.setText("No selected");
		xInput.setText(null);
		yInput.setText(null);
		imgPanel.repaint();
		selectedImg=null;
	}
}

class AnimeCreater
{
	private Frame window = null;
	private List fileList = new List();
	private Button addBtn = new Button("Add"), upBtn = new Button("Up"), delBtn = new Button("Del"), downBtn = new Button("Down"), 
				playBtn = new Button("Play"), lPlayBtn = new Button("Loop-Play"), startBtn = new Button("Create");
	private ArrayList imageList = new ArrayList();
	private boolean sizeKeyIned=false, loopplaying=false;
	private int sizeSelectIndex, frameWidth=1, frameHeight=1;
	private ScrollPane imgSP = new ScrollPane(1);
	private Panel imgPanel = new Panel(){public void paint(Graphics g) {
		g.fillRect(0, 0, imgPanel.getWidth(), imgPanel.getHeight());
		int frameIndex = fileList.getSelectedIndex();
		if(frameIndex>-1&&frameIndex<imageList.size())
			g.drawImage((Image) imageList.get(frameIndex), 0, 0, null);
	}};
	
	public AnimeCreater()
	{
		window = new Frame(){ public void paint(Graphics g) {
			g.drawLine(20, 70, 580, 70);
			g.drawLine(300, 90, 300, 330);
			g.drawLine(20, 350, 580, 350);
		}};
		window.setSize(600, 400);
		window.setLayout(null);
		window.setLocation((FunctionListForm.screenSize.width-600)/2, (FunctionListForm.screenSize.height-400)/2);
		window.setBackground(Color.LIGHT_GRAY);
		window.setResizable(false);
		window.setTitle("PackFiles - StoryToolKit");
		window.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e)
		{
			window.dispose();
			FunctionListForm.CreateAnimeBtn.setEnabled(true);
		}});

		Label pathLabel = new Label("Image path:");
		pathLabel.setSize(80, 20);
		pathLabel.setLocation(20, 40);
		window.add(pathLabel);
		
		final TextField pathInput = new TextField();
		pathInput.setSize(410, 20);
		pathInput.setLocation(110, 40);
		pathInput.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e) {
			if(e.getKeyCode()==KeyEvent.VK_ENTER)
			{
				try {
					addFile(pathInput.getText());
					pathInput.setText(null);
				} catch (Exception e1) {
					new MsgBox(e1.toString());
					e1.printStackTrace();
					fileList.removeAll();
					imageList.clear();
				}
			}
		}});
		window.add(pathInput);
		
		addBtn.setSize(50, 20);
		addBtn.setLocation(530, 40);
		addBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			try {
				addFile(pathInput.getText());
				pathInput.setText(null);
			} catch (Exception e) {
				new MsgBox(e.toString());
				e.printStackTrace();
				fileList.removeAll();
				imageList.clear();
			}
		}});
		window.add(addBtn);
		
		fileList.setSize(200, 260);
		fileList.setLocation(20, 80);
		fileList.addItemListener(new ItemListener(){public void itemStateChanged(ItemEvent e) {
			imgPanel.repaint();	
		}});
		window.add(fileList);
		
		upBtn.setSize(40, 90);
		upBtn.setLocation(230, 80);
		upBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			upItem();
		}});
		window.add(upBtn);
		
		delBtn.setSize(40, 60);
		delBtn.setLocation(230, 180);
		delBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			int index = fileList.getSelectedIndex();
			if(index>-1)
			{
				fileList.remove(index);
				String tFilePath;
				for(int i=index; i<fileList.getItemCount(); i++)
				{
					tFilePath = fileList.getItem(i);
					tFilePath = tFilePath.substring(tFilePath.indexOf(" - ")+3);
					fileList.remove(i);
					fileList.add(i + " - " + tFilePath, i);
				}
			}
		}});
		window.add(delBtn);
		
		downBtn.setSize(40, 90);
		downBtn.setLocation(230, 250);
		downBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			downItem();
		}});
		window.add(downBtn);

		Label fpsLabel = new Label("FPS:");
		fpsLabel.setSize(35, 20);
		fpsLabel.setLocation(320, 80);
		window.add(fpsLabel);
		
		final TextField fpsInput = new TextField("60");
		fpsInput.setSize(50, 20);
		fpsInput.setLocation(355, 80);
		fpsInput.addTextListener(new TextListener(){public void textValueChanged(TextEvent e) {
			if(!sizeKeyIned)
				return;
			StringBuffer buffer = new StringBuffer(fpsInput.getText());
			for(int i=0; i<buffer.length(); i++)
			{
				char c = buffer.charAt(i);
				if(c<'0'||c>'9')
				{
					buffer.deleteCharAt(i--);
					sizeSelectIndex--;
				}
				else if(i==0&&c=='0')
				{
					buffer.deleteCharAt(i--);
					sizeSelectIndex--;
				}
			}
			sizeKeyIned=false;
			fpsInput.setText(buffer.toString());
			fpsInput.setSelectionStart(sizeSelectIndex);
		}});
		fpsInput.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e) {
			sizeKeyIned=true;
			sizeSelectIndex = fpsInput.getSelectionStart()+1;
		}});
		fpsInput.addFocusListener(new FocusAdapter(){public void focusLost(FocusEvent e) {
			if(fpsInput.getText().equals(""))
				fpsInput.setText("60");
		}});
		window.add(fpsInput);

		Label delayLabel = new Label("Duration:");
		delayLabel.setSize(60, 20);
		delayLabel.setLocation(470, 80);
		window.add(delayLabel);
		
		final TextField timeInput = new TextField("1");
		timeInput.setSize(50, 20);
		timeInput.setLocation(530, 80);
		timeInput.addTextListener(new TextListener(){public void textValueChanged(TextEvent e) {
			if(!sizeKeyIned)
				return;
			StringBuffer buffer = new StringBuffer(timeInput.getText());
			for(int i=0; i<buffer.length(); i++)
			{
				char c = buffer.charAt(i);
				if(c<'0'||c>'9')
				{
					buffer.deleteCharAt(i--);
					sizeSelectIndex--;
				}
				else if(i==0&&c=='0')
				{
					buffer.deleteCharAt(i--);
					sizeSelectIndex--;
				}
			}
			sizeKeyIned=false;
			timeInput.setText(buffer.toString());
			timeInput.setSelectionStart(sizeSelectIndex);
		}});
		timeInput.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e) {
			sizeKeyIned=true;
			sizeSelectIndex = timeInput.getSelectionStart()+1;
		}});
		timeInput.addFocusListener(new FocusAdapter(){public void focusLost(FocusEvent e) {
			if(timeInput.getText().equals(""))
				timeInput.setText("1");
		}});
		window.add(timeInput);
		
		imgSP.setSize(260, 200);
		imgSP.setLocation(320, 110);
		imgSP.add(imgPanel);
		window.add(imgSP);
		
		playBtn.setSize(100, 20);
		playBtn.setLocation(320, 320);
		playBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			if(fileList.getItemCount()<1)
				return;
			playBtn.setEnabled(false);
			lPlayBtn.setEnabled(false);
			upBtn.setEnabled(false);
			delBtn.setEnabled(false);
			downBtn.setEnabled(false);
			startBtn.setEnabled(false);
			fileList.select(0);
			new Thread(){public void run(){
				int waitTime = 1000*(Integer.parseInt(timeInput.getText()))/Integer.parseInt(fpsInput.getText());
				for(int i=0; i<fileList.getItemCount(); i++)
				{
					fileList.select(i);
					imgPanel.repaint();
					synchronized (this) {try {
						wait(waitTime);
					} catch (Exception e) {}}
				}
				playBtn.setEnabled(true);
				lPlayBtn.setEnabled(true);
				upBtn.setEnabled(true);
				delBtn.setEnabled(true);
				downBtn.setEnabled(true);
				startBtn.setEnabled(true);
			}}.start();
		}});
		window.add(playBtn);
		
		lPlayBtn.setSize(100, 20);
		lPlayBtn.setLocation(480, 320);
		lPlayBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			if(fileList.getItemCount()<1)
				return;
			if(loopplaying)
			{
				loopplaying=false;
				lPlayBtn.setLabel("Loop-Play");
			}
			else
			{
				playBtn.setEnabled(false);
				upBtn.setEnabled(false);
				delBtn.setEnabled(false);
				downBtn.setEnabled(false);
				startBtn.setEnabled(false);
				lPlayBtn.setLabel("Stop");
				loopplaying=true;
				new Thread(){public void run(){
					int waitTime = 1000*(Integer.parseInt(timeInput.getText()))/Integer.parseInt(fpsInput.getText());
					for(int i=0; i<fileList.getItemCount() && loopplaying; i++)
					{
						fileList.select(i);
						imgPanel.repaint();
						synchronized (this) {try {
							wait(waitTime);
						} catch (Exception e) {}}
						if(i==fileList.getItemCount()-1)
							i=-1;
					}
					playBtn.setEnabled(true);
					upBtn.setEnabled(true);
					delBtn.setEnabled(true);
					downBtn.setEnabled(true);
					startBtn.setEnabled(true);
				}}.start();
			}
		}});
		window.add(lPlayBtn);

		Label distPathLabel = new Label("Animation save path:");
		distPathLabel.setSize(120, 20);
		distPathLabel.setLocation(20, 360);
		window.add(distPathLabel);
		
		final TextField distPathInput = new TextField();
		distPathInput.setSize(380, 20);
		distPathInput.setLocation(140, 360);
		window.add(distPathInput);
		
		startBtn.setSize(50, 20);
		startBtn.setLocation(530, 360);
		startBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			createConfirm(distPathInput.getText(),fileList.getItemCount(), Integer.parseInt(timeInput.getText()));
		}});
		window.add(startBtn);
		
		window.setVisible(true);
	}
	
	private void addFile(String filePath) throws Exception
	{
		if(filePath==null || filePath.trim().length()==0)
		{
			new MsgBox("Please input file path.");
			return;
		}
		File file = new File(filePath);
		if(file.exists())
		{
			if(file.isDirectory())
			{
				File[] files = file.listFiles();
				for(int i=0; i<files.length; i++)
				{
					if(!files[i].isDirectory())
					{
						fileList.add(fileList.getItemCount() + " - " + files[i].getName());
						Image img = ImageIO.read(files[i]);
						imageList.add(img);
						imgPanel.setPreferredSize(new Dimension(Math.max(img.getWidth(null), imgPanel.getWidth()),
								Math.max(img.getHeight(null), imgPanel.getHeight())));
						frameWidth = Math.max(img.getWidth(null), frameWidth);
						frameHeight = Math.max(img.getHeight(null), frameHeight);
						imgSP.doLayout();
						fileList.select(fileList.getItemCount()-1);
						imgPanel.repaint();
					}
				}
			}
			else
			{
				fileList.add(fileList.getItemCount() + " - " + file.getName());
				Image img = ImageIO.read(file);
				imageList.add(img);
				imgPanel.setPreferredSize(new Dimension(Math.max(img.getWidth(null), imgPanel.getWidth()),
						Math.max(img.getHeight(null), imgPanel.getHeight())));
				frameWidth = Math.max(img.getWidth(null), frameWidth);
				frameHeight = Math.max(img.getHeight(null), frameHeight);
				imgSP.doLayout();
				fileList.select(fileList.getItemCount()-1);
				imgPanel.repaint();
			}
		}
		else
		{
			new MsgBox("File could not be found.");
		}
	}
	
	private void upItem()
	{
		int index = fileList.getSelectedIndex();
		if(index<1)
			return;
		String tFilePath = fileList.getItem(index);
		tFilePath = tFilePath.substring(tFilePath.indexOf(" - ")+3);
		fileList.remove(index);
		fileList.add((index-1) + " - " + tFilePath, index-1);
		imageList.add(index-1, imageList.remove(index));
		for(int i=index; i<fileList.getItemCount(); i++)
		{
			tFilePath = fileList.getItem(i);
			tFilePath = tFilePath.substring(tFilePath.indexOf(" - ")+3);
			fileList.remove(i);
			fileList.add(i + " - " + tFilePath, i);
		}
		fileList.select(index-1);
		imgPanel.repaint();
	}
	
	private void downItem()
	{
		int index = fileList.getSelectedIndex();
		if(index<0||index==(fileList.getItemCount()-1))
			return;
		String tFilePath = fileList.getItem(index);
		tFilePath = tFilePath.substring(tFilePath.indexOf(" - ")+3);
		fileList.remove(index);
		fileList.add((index+1) + " - " + tFilePath, index+1);
		imageList.add(index+1, imageList.remove(index));
		for(int i=index; i<fileList.getItemCount(); i++)
		{
			tFilePath = fileList.getItem(i);
			tFilePath = tFilePath.substring(tFilePath.indexOf(" - ")+3);
			fileList.remove(i);
			fileList.add(i + " - " + tFilePath, i);
		}
		fileList.select(index+1);
		imgPanel.repaint();
	}

	private void createConfirm(final String filePath, final int count, final int duration)
	{
		if(count==0)
		{
			new MsgBox("No image to be created.");
			return;
		}
		if(filePath==null || filePath.trim().length()==0)
		{
			new MsgBox("Please input file path.");
			return;
		}
		if(filePath.replace("\", "/").indexOf("/")<0)
		{
			new MsgBox("Target file format error.");
			return;
		}
		File saveFile = new File(filePath);
		if(saveFile.exists())
		{
			if(saveFile.isDirectory())
				new MsgBox("Target file could not be directory.");
			else
				new MsgBox("Target file already exist.");
			return;
		}
		File folder = new File(saveFile.getParent());
		if(!folder.exists() && !folder.mkdirs())
		{
			new MsgBox("Fail to crete target folder.");
			return;
		}
		final Window confirmBox = new Window(window){public void paint(Graphics g) {
			g.drawRect(0, 0, 399, 199);
		}};
		confirmBox.setSize(400, 200);
		confirmBox.setLayout(null);
		confirmBox.setLocation((FunctionListForm.screenSize.width-400)/2, (FunctionListForm.screenSize.height-200)/2);

		Label typeLabel = new Label("Save target:");
		typeLabel.setSize(100, 20);
		typeLabel.setLocation(20, 30);
		confirmBox.add(typeLabel);

		Label typeValueLabel = new Label(filePath);
		typeValueLabel.setSize(260, 20);
		typeValueLabel.setLocation(120, 30);
		confirmBox.add(typeValueLabel);

		Label pathLabel = new Label("Frame count:");
		pathLabel.setSize(100, 20);
		pathLabel.setLocation(20, 60);
		confirmBox.add(pathLabel);

		Label pathValueLabel = new Label(String.valueOf(count));
		pathValueLabel.setSize(260, 20);
		pathValueLabel.setLocation(120, 60);
		confirmBox.add(pathValueLabel);

		Label countLabel = new Label("Frame duration:");
		countLabel.setSize(100, 20);
		countLabel.setLocation(20, 90);
		confirmBox.add(countLabel);

		Label countValueLabel = new Label(String.valueOf(duration));
		countValueLabel.setSize(260, 20);
		countValueLabel.setLocation(120, 90);
		confirmBox.add(countValueLabel);
		
		final Button okBtn = new Button("OK");
		okBtn.setSize(100, 20);
		okBtn.setLocation(70, 150);
		okBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			confirmBox.dispose();
			new Thread(){public void run(){
				process(filePath, count, duration);
			}}.start();
		}});
		confirmBox.add(okBtn);
		
		final Button cancelBtn = new Button("Cancel");
		cancelBtn.setSize(100, 20);
		cancelBtn.setLocation(230, 150);
		cancelBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
			confirmBox.dispose();
		}});
		confirmBox.add(cancelBtn);
		
		confirmBox.setVisible(true);
		okBtn.requestFocus();
	}
	
	private void process(String savePath, int frameCount, int duration)
	{
		playBtn.setEnabled(false);
		lPlayBtn.setEnabled(false);
		upBtn.setEnabled(false);
		delBtn.setEnabled(false);
		downBtn.setEnabled(false);
		startBtn.setEnabled(false);

		BufferedImage fileImg = new BufferedImage(frameWidth*frameCount, frameHeight, BufferedImage.TYPE_INT_ARGB); 
		Graphics g = fileImg.getGraphics();
		for(int i=0; i<frameCount; i++)
		{
			g.drawImage((Image) imageList.get(i), i*frameWidth, 0, null);
		}
		g.dispose();
		File imgFile = new File(savePath);
		try {
			ImageIO.write(fileImg, "png", imgFile);
			FileWriter writer = new FileWriter(savePath+".pf");
			writer.write("#Please do not amend manual.n");
			writer.write("IMAGE_PATH="+imgFile.getName()+".pfn");
			writer.write("FRAME_COUNT="+frameCount+"n");
			writer.write("FRAME_DURATION="+duration);
			writer.flush();
			writer.close();
		} catch (Exception e) {
			new MsgBox(e.toString());
			e.printStackTrace();
			new File(savePath).delete();
			new File(imgFile.getName()+".pf").delete();
		} 
		new MsgBox("Completed");
		
		playBtn.setEnabled(true);
		lPlayBtn.setEnabled(true);
		upBtn.setEnabled(true);
		delBtn.setEnabled(true);
		downBtn.setEnabled(true);
		startBtn.setEnabled(true);
	}

}


最后

以上就是负责寒风为你收集整理的[StoryToolKit] STK工具箱的源代码的全部内容,希望文章能够帮你解决[StoryToolKit] STK工具箱的源代码所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部