/* * TrainFull.java * * DATE : 2009/10/14 09:34 */ import com.docomostar.*; import com.docomostar.ui.*; import com.docomostar.io.*; import com.docomostar.media.*; import java.io.*; import javax.microedition.io.Connector; /** * TrainFull * * @author NAME */ public class TrainFull extends StarApplication { public void started(int launchType) { /* * The program of StarApplication is written here. */ Display.setCurrent((Frame)(new MainCanvas())); } } /** * MainCanvas * */ class MainCanvas extends Canvas { byte [][][][] data; //時刻データ String [] line; //路線名データ String ltxt; //文字入力用 ImageReference[] imRef; //画像 int x = 0; //カーソル位置 int y = 0; //カーソル位置 byte lnum = 0; //選択している路線 int dtype = 0; //選択している曜日 int iHour = -1; //選択している時間(-1:選択なし) int iTime = 0; //選択している分 byte iVal = 0; //変更中の分 MainCanvas() { int i; setSoftLabel(SOFT_KEY_1, "ミニアプリ"); setSoftLabel(SOFT_KEY_2, "終了"); line = new String[10]; //10路線 data = new byte[10][22][10][3]; //10路線*22時間*10個*3曜日 loadData(); imRef = new ImageReference[3]; setBackground(Graphics.getColorOfName(Graphics.LIME)); imRef[0]=ImageReference.createImageReference("resource:///arrow.gif"); imRef[1]=ImageReference.createImageReference("resource:///sel_0.gif"); imRef[2]=ImageReference.createImageReference("resource:///sel_1.gif"); } public void paint(Graphics g) { int a,i,j; Font titleFont = Font.getFont(Font.FACE_SYSTEM,32); g.lock(); //背景色 g.clearRect(0, 0, Display.getWidth(), Display.getHeight()); //路線名 g.setColor(Graphics.getColorOfRGB(255, 255, 255)); g.fillRect(80, 20, 480-80*2, 40); g.setColor(Graphics.getColorOfRGB(0, 0, 0)); g.drawRect(80, 20, 480-80*2, 40); g.drawString(line[lnum], (Display.getWidth() - titleFont.getBBoxWidth(line[lnum])) / 2+12+20, 60-8); //曜日 g.setColor(Graphics.getColorOfRGB(255, 255, 255)); g.fillRect(130, 70, 60, 40); g.fillRect(130+80, 70, 60, 40); g.fillRect(130+160, 70, 60, 40); g.setColor(Graphics.getColorOfRGB(0, 0, 0)); g.drawRect(130, 70, 60, 40); g.drawRect(130+80, 70, 60, 40); g.drawRect(130+160, 70, 60, 40); if (dtype==0) { g.setColor(Graphics.getColorOfRGB(0, 0, 0)); } else { g.setColor(Graphics.getColorOfRGB(128, 128, 128)); } g.drawString("平日", 138, 70+30); if (dtype==1) { g.setColor(Graphics.getColorOfRGB(0, 0, 0)); } else { g.setColor(Graphics.getColorOfRGB(128, 128, 128)); } g.drawString("土曜", 138+80, 70+30); if (dtype==2) { g.setColor(Graphics.getColorOfRGB(0, 0, 0)); } else { g.setColor(Graphics.getColorOfRGB(128, 128, 128)); } g.drawString("休日", 138+160, 70+30); //時刻表 g.setColor(Graphics.getColorOfRGB(255, 255, 255)); g.fillRect(40, 120, 480-40*2, 32*22); g.setColor(Graphics.getColorOfRGB(0, 0, 0)); g.drawRect(40, 120, 480-40*2, 32*22); for (i=0; i<23 ;i++){ g.drawLine(40, 120+32*i, 480-40, 120+32*i); } g.drawLine(40+40, 120, 40+40, 120+32*22); for (i=0; i<22 ;i++){ if ((i+4)%24 < 10) { a = 46+8; } else { a = 46; } g.drawString("" + (i+4)%24, a, 120+32*i+28); } //時刻表示 for (i=0;i<22;i++){ for (j=0;j<10;j++){ if (data[lnum][i][j][dtype]>=0){ g.drawString(""+data[lnum][i][j][dtype]/10, 92+j*32, 146+i*32); g.drawString(""+data[lnum][i][j][dtype]%10, 106+j*32, 146+i*32); } } } //カーソル・コマンド g.drawImageReference(imRef[1],20-6,20-4,false); g.drawImageReference(imRef[2],400+16,20-4,false); if (y==0){ g.drawImageReference(imRef[0],240+80,16+20,false); } else if (x==0 && y==1){ g.drawImageReference(imRef[0],160,90,false); } else if (x==1 && y==1){ g.drawImageReference(imRef[0],160+80,90,false); } else if (x==2 && y==1){ g.drawImageReference(imRef[0],160+160,90,false); } else if (y>1){ if (iHour<0) { g.drawImageReference(imRef[0],104+x*32,70+y*32,false); } else { //時刻変更 g.setColor(Graphics.getColorOfRGB(255, 255, 255)); g.fillRect(88+iTime*32, 122+iHour*32, 32, 29); g.setColor(Graphics.getColorOfRGB(0, 0, 0)); g.drawRect(88+iTime*32, 122+iHour*32, 32, 29); g.drawString(""+iVal/10, 92+iTime*32, 146+iHour*32); g.drawString(""+iVal%10, 106+iTime*32, 146+iHour*32); } } g.unlock(true); } public void processEvent(int type, int param) { if (type == Display.KEY_RELEASED_EVENT) { if (param == Display.KEY_SELECT) { //曜日切り替え if (x==0 && y==1) { dtype = 0; } else if (x==1 && y==1) { dtype = 1; } else if (x==2 && y==1) { dtype = 2; //路線名入力 } else if (y==0) { ltxt = ""; imeOn(ltxt,TextBox.DISPLAY_ANY,TextBox.KANA); } else if (y>1) { //時刻入力開始 if (iHour<0) { iHour = y-2; iTime = x; x = 1; if (data[lnum][iHour][iTime][dtype]>0){ iVal = data[lnum][iHour][iTime][dtype]; } else { iVal = 0; } } else { //時刻入力終了 if (iVal<60){ data[lnum][iHour][iTime][dtype] = iVal; sortData(); } iHour = -1; x = iTime; } } } else if (param == Display.KEY_LEFT) { if (x>0) { x--; } if (y==0) { lnum--; if (lnum<0) lnum = 9; } } else if (param == Display.KEY_RIGHT) { if ((y<2 && x<2) || (y>1 && x<9)) { x++; } if (y==0){ lnum++; if (lnum>9) lnum = 0; } } else if (param == Display.KEY_UP) { if (y>0) { y--; if (y<2 && x>2) x=2; } } else if (param == Display.KEY_DOWN) { if (y<23) { y++; } } else if (param == Display.KEY_0) { iVal = keyInput(iVal,0,x); x = 1 - x; } else if (param == Display.KEY_1) { iVal = keyInput(iVal,1,x); x = 1 - x; } else if (param == Display.KEY_2) { iVal = keyInput(iVal,2,x); x = 1 - x; } else if (param == Display.KEY_3) { iVal = keyInput(iVal,3,x); x = 1 - x; } else if (param == Display.KEY_4) { iVal = keyInput(iVal,4,x); x = 1 - x; } else if (param == Display.KEY_5) { iVal = keyInput(iVal,5,x); x = 1 - x; } else if (param == Display.KEY_6) { iVal = keyInput(iVal,6,x); x = 1 - x; } else if (param == Display.KEY_7) { iVal = keyInput(iVal,7,x); x = 1 - x; } else if (param == Display.KEY_8) { iVal = keyInput(iVal,8,x); x = 1 - x; } else if (param == Display.KEY_9) { iVal = keyInput(iVal,9,x); x = 1 - x; } else if (param == Display.KEY_SOFT1) { /* ミニアプリに切り替えます */ saveData(); (StarApplication.getThisStarApplication()).changeAppType(StarApplication.STAR_TYPE_MINIAPPLI, null); } else if (param == Display.KEY_SOFT2) { if (iHour > 0){ iHour = -1; x = iTime; } else { saveData(); (StarApplication.getThisStarApplication()).terminate(); } } repaint(); } } //時刻表データの読み込み public void loadData() { int i,j,k,l,ps; try{ for (i=0;i<10;i++){ // テキストデータを、scratchpadに書込 ps = 32 * i; DataInputStream w_datainput = Connector.openDataInputStream("scratchpad:///0;pos="+ps); line[i] = w_datainput.readUTF(); w_datainput.close(); } DataInputStream w_datainput = Connector.openDataInputStream("scratchpad:///0;pos=320"); for (i=0;i<10;i++){ for (j=0;j<22;j++){ for (k=0;k<10;k++){ for (l=0;l<3;l++){ data[i][j][k][l] = w_datainput.readByte(); } } } } lnum = w_datainput.readByte(); w_datainput.close(); if (line[0].equals("")){ line[0] = "平田⇒松本"; for (i=1;i<10;i++){ line[i] = "新規"+i; } //時刻初期化 for (i=0;i<10;i++){ for (j=0;j<22;j++){ for (k=0;k<10;k++){ for (l=0;l<3;l++){ data[i][j][k][l] = -1; } } } } } } catch(IOException ie) { System.out.println("load err!!"); } } //時刻表データの書き込み public void saveData() { int i,j,k,l,ps; try{ for (i=0;i<10;i++){ // テキストデータを、scratchpadに書込 ps = 32 * i; DataOutputStream w_dataoutput = Connector.openDataOutputStream("scratchpad:///0;pos="+ps); w_dataoutput.writeUTF(line[i]); w_dataoutput.close(); } DataOutputStream w_dataoutput = Connector.openDataOutputStream("scratchpad:///0;pos=320"); for (i=0;i<10;i++){ for (j=0;j<22;j++){ for (k=0;k<10;k++){ for (l=0;l<3;l++){ w_dataoutput.writeByte(data[i][j][k][l]); } } } } w_dataoutput.writeByte(lnum); w_dataoutput.close(); } catch(IOException ie) { System.out.println("save err!!"); } } public void processIMEEvent(int type,String inputText){ ltxt=inputText;//入力された文字をセット if (ltxt.length() > 10) { line[lnum] = ltxt.substring(0,10); } else { line[lnum] = ltxt; } repaint(); } //時刻表データのソート public void sortData() { int k,m; byte a; for (k=0;k<9;k++){ for (m=k+1;m<10;m++){ if ( ((data[lnum][iHour][k][dtype]<0) && data[lnum][iHour][m][dtype]>=0) || ((data[lnum][iHour][k][dtype]>data[lnum][iHour][m][dtype])&& data[lnum][iHour][m][dtype]>=0) ){ a = data[lnum][iHour][k][dtype]; data[lnum][iHour][k][dtype] = data[lnum][iHour][m][dtype]; data[lnum][iHour][m][dtype] = a; } } } } //テンキー入力処理 public byte keyInput(byte o_val,int key,int pos) { byte val; if (pos==1) { val = (byte)(o_val%10+key*10); } else { val = (byte)(o_val-o_val%10+key); } return val; } }