今からはじめるプログラミング47(ノベルゲームふたたび?)
唐突に、SEの仕事を探したのですが、
やっぱり、どうしても業務アプリケーションのプロジェクトに誘導されるんだよね。。。
まぁ経歴を見れば、そうしたくもなるだろうけれど。
そして希望はとおらないw
なので、やはりSE復帰はやめておきました。
ふと思ったのですが、あまりゲームを意識しないで、単純な画面を作って、それを単純な仕組みで画面遷移できるようにすれば、ノベルゲームもっと簡単に作れないか。
なんとなく出来そうなきがしたので、3連休の2日くらい考えながらコーディングしてみたw
今回はかなり「がー」と書いたので、解説が一言もないのですが、概要を書くと、
・contentフォルダにある「img.png」という画像ファイルを中央に表示する。
・同じく「text.txt」ファイルを下部に表示する。
・サブディレクトリの名前のボタンを右側に列挙する。
・名前のボタンを押下するとサブディレクトリ内の画像とテキストで同じ構成の画面を表示する。
というクラスです。
さて、画面遷移の仕組みがご理解いただけたでしょうか。
Windowsをはじめ、ほとんどのOSのファイルシステムは、ツリー形式で構成されるので、それをそのまま画面遷移に利用した、という感じです。
「content」以下に「img.png」「text.txt」というファイルを置けば、画面ができて、同じ構成のサブフォルダを作れば、画面遷移ができる。
なかなかいいアイデアではないでしょうか?とか自分で思いました。
Frameクラス
------------------------------------------------------------------------------
package sample29;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Frame extends JFrame implements ActionListener {
private static final String IMG_FILE = "img.png";
private static final String TXT_FILE ="text.txt";
private String fileTxt = null;
private String imageFile = null;
private File subFolders = null;
private List<JButton> btnList = null;
private File crntFile = null;
private boolean isRoot = true;
public void setRoot() {
isRoot = true;
}
public void setNtRoot() {
isRoot = false;
}
public Frame(String dir) {
this.initialLoading(dir);
this.constPnls();
}
private void constPnls() {
Container container = this.getContentPane();
container.setLayout(new BorderLayout());
JPanel pnlImg = new JPanel();
ImageIcon icon = new ImageIcon(imageFile);
// ラベルにIconを設定
JLabel label = new JLabel(icon);
pnlImg.add(label);
container.add(pnlImg, BorderLayout.CENTER);
JTextArea area = new JTextArea(fileTxt);
JScrollPane scrol = new JScrollPane(area);
JPanel pnlTxt = new JPanel();
pnlTxt.add(scrol);
scrol.setPreferredSize(new Dimension(600,50));
pnlTxt.setPreferredSize(new Dimension(600,50));
container.add(pnlTxt, BorderLayout.SOUTH);
JPanel pnlBtns = new JPanel();
pnlBtns.setLayout(new GridLayout(btnList.size(), 1));
for (int i = 0; i < btnList.size(); i++) {
pnlBtns.add(btnList.get(i));
}
container.add(pnlBtns, BorderLayout.EAST);
JButton btnCls = new JButton("close");
btnCls.setActionCommand("close");
btnCls.addActionListener(this);
container.add(btnCls, BorderLayout.WEST);
this.setSize(new Dimension(600, 600));
}
private void initialLoading(String dir) {
crntFile = new File(dir);
this.subFolders = crntFile.listFiles();
if (this.subFolders == null) {
System.exit(-1);
}
int fileCnt = this.subFolders.length;
btnList = new ArrayList<JButton>();
for (int i = 0; i < fileCnt; i++) {
File file = this.subFolders[i];
if (file.isDirectory()) {
// add Action
String actionName = file.getName();
JButton btn = new JButton(actionName);
btn.addActionListener(this);
btn.setActionCommand(actionName);
btnList.add(btn);
}
if (IMG_FILE.contentEquals(file.getName())) {
// set image
imageFile = file.getAbsolutePath();
}
if (TXT_FILE.contentEquals(file.getName())) {
// load Text
fileTxt = this.loadTxtFile(file);
}
}
}
private String loadTxtFile(File file) {
if (file.exists()) {
FileReader fr;
try {
fr = new FileReader(file);
} catch (FileNotFoundException e) {
System.out.println("ふぁいるがないです");
return "";
}
BufferedReader reader = new BufferedReader(fr);
StringBuffer buffer = new StringBuffer();
try {
while (reader.ready()) {
buffer.append(reader.readLine());
buffer.append(System.lineSeparator());
}
} catch (IOException e) {
System.out.println("よめません[" + file.getName() + "]");
return "";
} finally {
try {
reader.close();
fr.close();
} catch (IOException e) {
System.out.println("とじれないかった[" + file.getName() + "]");
return "";
}
}
return buffer.toString();
}
return "";
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String command = e.getActionCommand();
if ("close".equals(command)) {
this.setVisible(false);
if (isRoot) {
System.exit(0);
}
return;
}
String path = this.crntFile.getAbsolutePath() + "/" + command;
Frame newFrame = new Frame(path);
newFrame.setNtRoot();
newFrame.setVisible(true);
newFrame.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
}
public static void main(String args) {
Frame frame = new Frame("content");
frame.setRoot();
frame.setVisible(true);
frame.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
}
}
------------------------------------------------------------------------------
いかがでしょうか。
だいぶ簡単にいけそうですよね。ただサイズが固定になっているので、そこはご注意ください。
プロジェクトフォルダは、下記でお願いします。