今からはじめるプログラミング12

自分のスケジュールの中では、前回のフィールドの表示で、

フィールドの移動も実装したかったのですが、

怠け者なので、結局1日寝てしまい。

今頃に実装したのです。(寝ぼけていて日本語がおかしいのは、申し訳ありませんが、寝ぼけていなくてもおかしいです。)

 

そしてその実装もちょっと微妙です。

うぅ。

とりあえず

FieldMapをがらっと変えて(追加して)ます。

---------------------------------------------------

package sample7;

 

import javax.swing.JPanel;

import javax.swing.JTable;

 

public class FieldMap extends JPanel {

 

private JTable tblField = null;

 

private String map = new String {

{ "2", "2", "2", "2", "2" },

{ "2", "1", "1", "1", "2" },

{ "2", "1", "1", "1", "2" },

{ "2", "1", "1", "1", "2" },

{ "2", "2", "2", "2", "2" }, };

 

public String getMap() {

return this.map;

}

public static final int V_NORTH =1;

public static final int V_SOUTH =2;

public static final int V_EAST =3;

public static final int V_WEST =4;

 

public void shift(int vector) {

if(vector==V_SOUTH) {

//save

String sv =new String[5];

for(int i = 0;i<5;i++) {

sv[i] = map[0][i];

map[0][i] = map[1][i];

map[1][i] = map[2][i];

map[2][i] = map[3][i];

map[3][i] = map[4][i];

map[4][i] = sv[i];

}

}

if(vector==V_NORTH) {

//save

String sv =new String[5];

for(int i = 0;i<5;i++) {

sv[i] = map[4][i];

map[4][i] = map[3][i];

map[3][i] = map[2][i];

map[2][i] = map[1][i];

map[1][i] = map[0][i];

map[0][i] = sv[i];

}

}

if(vector==V_EAST) {

//save

String sv =new String[5];

for(int i = 0;i<5;i++) {

sv[i] = map[i][0];

map[i][0] = map[i][1];

map[i][1] = map[i][2];

map[i][2] = map[i][3];

map[i][3] = map[i][4];

map[i][4] = sv[i];

}

}

if(vector==V_WEST) {

//save

String sv =new String[5];

for(int i = 0;i<5;i++) {

sv[i] = map[i][4];

map[i][4] = map[i][3];

map[i][3] = map[i][2];

map[i][2] = map[i][1];

map[i][1] = map[i][0];

map[i][0] = sv[i];

}

}

 

}

 

public void init() {

String map = this.getMap();

tblField = new JTable(5, 5);

//テーブルに値を設定

for (int i = 0; i < 5; i++) {

for (int j = 0; j < 5; j++) {

tblField.setValueAt(map[i][j], i, j);

 

}

}

//テーブルにレンダラーを設定

for (int j = 0; j < 5; j++) {

tblField.getColumnModel().getColumn(j).setCellRenderer(new FieldRenderer());

tblField.getColumnModel().getColumn(j).setPreferredWidth(32);

}

//最後にキャラクターを表示する(キャラクターのコードを0として)

 

tblField.setValueAt("0", 2, 2);//まんなか

//テーブルをパネル(自分)に追加

this.add(tblField);

}

 

public void resetValue(String map) {

//テーブルに値を設定

for (int i = 0; i < 5; i++) {

for (int j = 0; j < 5; j++) {

tblField.setValueAt(map[i][j], i, j);

 

}

}

 

//最後にキャラクターを表示する(キャラクターのコードを0として)

 

tblField.setValueAt("0", 2, 2);//まんなか

}

 

public FieldMap() {

this.init();

}

}

---------------------------------------------------

次に画面(MainFrame)これはまぁかんたん?

---------------------------------------------------

package sample7;

 

import java.awt.BorderLayout;

import java.awt.Dimension;

 

import javax.swing.JButton;

import javax.swing.JFrame;

 

public class MainFrame extends JFrame {

 

private FieldMap filedMap = null;

 

public MainFrame() {

 

this.filedMap = new FieldMap();

 

this.setTitle("field");

this.setLayout(new BorderLayout());

this.getContentPane().add(filedMap,BorderLayout.CENTER);

JButton btnEast = new JButton("東");

JButton btnWast = new JButton("西");

JButton btnSouth = new JButton("南");

JButton btnNorth = new JButton("北");

 

btnEast.addActionListener(new EastListener(this.filedMap));

 

this.getContentPane().add(btnEast,BorderLayout.EAST);

this.getContentPane().add(btnWast,BorderLayout.WEST);

this.getContentPane().add(btnSouth,BorderLayout.SOUTH);

this.getContentPane().add(btnNorth,BorderLayout.NORTH);

 

this.setSize(new Dimension(400,450));

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

 

}

 

}

---------------------------------------------------

そしてマップを動かしているリスナーさん(EastListener)

 

---------------------------------------------------

 

package sample7;

 

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

public class EastListener implements ActionListener {

 

private FieldMap filedMap = null;

 

public EastListener(FieldMap filedMap) {

this.filedMap = filedMap;

}

 

@Override

public void actionPerformed(ActionEvent e) {

// 2次元テーブルの値を東に移動

this.filedMap.shift(FieldMap.V_EAST);

this.filedMap.resetValue(filedMap.getMap());

 

 

}

 

}

---------------------------------------------------

 忘れてはいけない起動クラス(MainClass7)

まぁ画面を表示しているだけですが。

---------------------------------------------------

package sample7;

 

public class MainClass7 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

new MainFrame();

}

 

}

---------------------------------------------------

どうですかね、そんなわけで”東”ボタンしか実装していません。

コードは現在googleドライブにアップ中です。(回線がとまってしまっている?)終わったら追記します。ひとつひとつ参照だとめんどくさいので、zip形式にまとめました。

夜勤前の睡眠前なので、なかなか雑なプログラム、対応ですいませんが、

まぁブレスレット、ブレスレットでお願いします。

 

ところで、eclipseでjarファイルを作ると画像が表示されなくて、パスの設定だと思うのですが、どうすると楽にできるかな。いやこれだけいろんなプロジェクトに参加して、リリース担当とか一度もやってなくて・・・。

antとかmavenとかの知識がおざなりです。。。。

 

 

f:id:yo2an:20210410092223p:plain

フィールドの移動(移動前)

f:id:yo2an:20210410092223p:plain

フィールドの移動(東ボタン押下)