// ====================================================================
//            JAVA APPLET PROGRAM BASE SOURCE     BakuretuKen          
// ====================================================================

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.awt.image.*;
import java.util.StringTokenizer;
import java.net.MalformedURLException;
import java.io.*;

// ===============================================================

public class novel extends Applet {
	
	// グローバル変数
	
	// テキスト保存
	String scene[];
	// 画面表示文字列
	String screen[];
	// 読み込み行番号
	int now_line;
	
	// -----------------------------------------------------------
	public void init() {  // 初期処理
		
		// 変数の初期化
		scene = new String[1000];
		screen = new String[15];
		now_line = 0;
		
		// テキストファイル start.txt の読み込み
		LoadScene("start.txt");
		// 画面表示のための文字列設定（0行から）
		now_line = SetText( 0 );
		// 再作画
		repaint();
		
		// マウス処理アクションリスナー定義と登録
		addMouseListener(new MouseAction());
	} // init()
	
	// -----------------------------------------------------------
	public void paint(Graphics g) {  // 作画処理
		
		// 背景を灰色に塗る
		g.setColor( Color.gray );
		g.fillRect( 0, 0, 480, 360 );
		
		// 画面に文章表示
		g.setFont(new Font( "Dialog", Font.BOLD, 17 ));
		for ( int i=0; i<15; i++ ) {
			if( screen[i]!=null ){
				g.setColor( Color.black );
				g.drawString( screen[i], 3+2, (i*22)+22+1); // 文字影
				g.setColor( Color.white );
				g.drawString( screen[i], 3, (i*22)+22); // 文字
			}
		}
		
	} // paint()
	
	// -----------------------------------------------------------
	public void update(Graphics g) {  // 再作画処理
		paint( g );
	} // update()
	
	// -----------------------------------------------------------
	// テキストファイル読み込み
	public void LoadScene( String filename ) {
		
		BufferedReader ds = null;
		String s = null;
		
		// 読み込み行番号初期化
		now_line = 0;
		
		// まず scene[] の全てに null を入れて初期化する
		for(int i=0; i<1000; i++) {
			scene[i] = null;
		}
		
		// テキストファイルを読み込むための初期宣言
		try {
			InputStream is = new URL(getCodeBase(), filename ).openStream();
			InputStreamReader myin = new InputStreamReader( is );
			ds = new BufferedReader(myin);
		} catch ( IOException e ) {
			System.out.println("テキストファイル読み込みエラー  " + filename );
		}
		
		// 実際のテキストファイル読み込み readLine()
		for(int i=0; i<1000; i++) {
			try {
				s = ds.readLine();
			} catch ( IOException e ) {
				System.out.println("テキストファイル読み込みエラー" );
			}
			if ( s==null ) break;
			scene[i] = s;
		}
		
	}// LoadScene()
	
	// -----------------------------------------------------------
	// 文章を表示する為、screen[] に文章を入れる関数
	public int SetText( int line ) {
		
		// まず screen[] を全てに null を入れて初期化する
		for ( int i=0; i<15; i++ ) {
			screen[i] = null;
		}
		
		// scene[] から screen[] に文字列をコピーする（指定された行から 15行）
		for ( int i=0; i<15; i++ ) {
			if (scene[line+i].equals("END")) return 9999;
			screen[i] = scene[line+i];
		}
		
		// もしコピーした最終行の下の行が null だったら、返り値 9999
		if (scene[line+15].equals("END")) return 9999;
		
		// 最終行の下の行番号が返り値
		return line+15;
		
	} // SetText()
	
// -----------------------------------------------------------
// マウス処理クラス(MouseAction Class)
class MouseAction extends MouseAdapter{
	
	public void mousePressed(MouseEvent e) {
		// マウスダウンイベント
		
		// now_line が 9999 だったら何もしない
		if( now_line==9999 ) return;
		// 画面表示のための文字列設定（now_line行から）
		now_line = SetText( now_line );
		// 再作画
		repaint();
		
	} // mousePressed()
	
	public void mouseReleased(MouseEvent e) {
		// マウスアップイベント
		
	} // mouseReleased()
	
} // myWindowAction

// ===============================================================
}  // End of Class

