java
1package picross; 2 3import static picross.Commonconst.HI; 4import static picross.Commonconst.MASS; 5import static picross.Commonconst.SIZE; 6import static picross.Commonconst.WI; 7 8import java.awt.BorderLayout; 9import java.awt.Color; 10import java.awt.Dimension; 11import java.awt.Graphics; 12import java.awt.event.ActionEvent; 13import java.awt.event.ActionListener; 14import java.awt.event.MouseEvent; 15import java.awt.event.MouseListener; 16import java.io.BufferedWriter; 17import java.io.File; 18import java.io.FileNotFoundException; 19import java.io.FileWriter; 20import java.io.IOException; 21import java.awt.FileDialog; 22import java.awt.Frame; 23 24import javax.imageio.stream.FileCacheImageInputStream; 25import javax.swing.JButton; 26import javax.swing.JFileChooser; 27import javax.swing.JFrame; 28import javax.swing.JPanel; 29 30 31 32public class Picross extends JFrame implements ActionListener { 33 34 JPanel buttonpanel = new JPanel(); 35 JButton btn = new JButton("CSV出力"); 36 37 public Picross() { 38 //フレームの設定 39 setTitle("ピクロス"); 40 setDefaultCloseOperation(EXIT_ON_CLOSE); 41 42 //ボタンの場所の設定 43 btn.addActionListener(this); 44 buttonpanel.add(btn); 45 add(buttonpanel,BorderLayout.SOUTH); 46 47 //ボードの場所の設定 48 Board board = new Board(); 49 add(board,BorderLayout.CENTER); 50 51 //全てをまとめる 52 pack(); 53 } 54 55 public static void main(String[] args) { 56 // TODO 自動生成されたメソッド・スタブ 57 Picross picross = new Picross(); 58 picross.setVisible(true); 59 } 60 61 @Override 62 public void actionPerformed(ActionEvent e) { 63 // TODO 自動生成されたメソッド・スタブ 64 System.out.println("テスト成功です"); 65 try{ 66 FileOutputStream fos =new FileOutputStream("CSV出力.txt"); 67 OutputStreamWriter osw= new OutputStreamWriter(fos, "Shift_JIS"); 68 BufferedWriter bfw =new BufferedWriter(osw); 69 for(int i=0;i<boardarray.length;i++){ 70 71 72 73 } 74 75 JFileChooser fileChooser = new JFileChooser(); 76 //SaveDialogを表示させる 77 fileChooser.showSaveDialog(null); 78 79 80 } 81 class Board extends JPanel implements MouseListener { 82 int[][] boardarray = new int[MASS][MASS]; 83 JButton btn = new JButton("CSV出力"); 84 85 public Board() { 86 // TODO 自動生成されたコンストラクター・スタブ 87 setPreferredSize(new Dimension(WI, HI)); 88 89 90 addMouseListener(this); 91 92 boardarray[5][5] = 1; 93 } 94 95 public void paint(Graphics graphi) { 96 // graphi.setColor(Color.GRAY); 97 // graphi.fillRect(0, 0, HI, WI); 98 99 for (int x = 0; x < MASS; x++) { 100 for (int y = 0; y < MASS; y++) { 101 if (boardarray[x][y] == 1) { 102 graphi.setColor(Color.BLACK); 103 graphi.fillRect(x * SIZE, y * SIZE, SIZE, SIZE); 104 } else if (boardarray[x][y] == 0) { 105 graphi.setColor(Color.GRAY); 106 graphi.fillRect(x * SIZE, y * SIZE, SIZE, SIZE); 107 } 108 graphi.setColor(Color.ORANGE); 109 for (int i = 0; i < MASS; i++) { 110 graphi.drawLine(0, SIZE * i, WI, i * SIZE); 111 graphi.drawLine(i * SIZE, 0, i * SIZE, HI); 112 } 113 } 114 115 } 116 } 117 118 @Override 119 public void mouseClicked(MouseEvent e) { 120 // TODO 自動生成されたメソッド・スタブ 121 122 } 123 124 @Override 125 public void mousePressed(MouseEvent e) { 126 // TODO 自動生成されたメソッド・スタブ 127 int x = e.getX(); 128 int y = e.getY(); 129 130 x /= SIZE; 131 y /= SIZE; 132 133 if (boardarray[x][y] == 0) { 134 boardarray[x][y] = 1; 135 } else if (boardarray[x][y] == 1) { 136 boardarray[x][y] = 0; 137 } 138 repaint(); 139 System.out.println(x + "," + y); 140 } 141 142 @Override 143 public void mouseReleased(MouseEvent e) { 144 // TODO 自動生成されたメソッド・スタブ 145 146 } 147 148 @Override 149 public void mouseEntered(MouseEvent e) { 150 // TODO 自動生成されたメソッド・スタブ 151 152 } 153 154 @Override 155 public void mouseExited(MouseEvent e) { 156 // TODO 自動生成されたメソッド・スタブ 157 158 } 159 160 } 161} 162
上記のコードでboard arrayの配列の値を全てCSVファイルで出力したいのですが、多重配列の要素の取得ができずに困っています。boardarray.lengthだと一時配列分しか取れないのでスマートな方法があればと思い投稿しました。
いつも、レベルが低くて申し訳ありませんが、よろしくお願いします。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/07 00:07 編集