1 package demo9;
2
3 import javax.swing.JPanel;
4 import java.awt.BorderLayout;
5 import javax.swing.JTextArea;
6 import javax.swing.JSpinner;
7 import javax.swing.SpinnerNumberModel;
8 import javax.swing.border.BevelBorder;
9
10 import fi.jyu.mit.ohj2.Mjonot;
11 import java.awt.Font;
12
13
14
19 public class PanelMatriisi extends JPanel {
20 private static final long serialVersionUID = 1L;
21
22 private final JPanel panelValinnat = new JPanel();
23 private final JTextArea textMatriisi = new JTextArea();
24 private final JSpinner spinner = new JSpinner();
25
26
29 public PanelMatriisi() {
30 setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
31 setLayout(new BorderLayout(0, 0));
32 textMatriisi.setFont(new Font("Monospaced", textMatriisi.getFont().getStyle(), 13));
33 textMatriisi.setRows(3);
34 textMatriisi.setText("-1 0 1\r\n-1 0 1\r\n-1 0 1");
35
36 add(textMatriisi, BorderLayout.CENTER);
37
38 add(panelValinnat, BorderLayout.SOUTH);
39 spinner.setModel(new SpinnerNumberModel(0, 0, 50, 1));
40
41 panelValinnat.add(spinner);
42
43 }
44
45
46
64 public static int[][] getMatriisi(String[] lines) {
65 int n = 0; final String erottimet = "[ \t,;:]+";
67 for (String line : lines) {
68 if ( line.trim().length() == 0 ) break;
69 n++;
70 }
71 if ( n == 0 ) return new int[][]{{1}};
72
73 String line = lines[0];
74 String[] items = line.split(erottimet);
75 int m = items.length;
76
77 int[][] tulos = new int[n][m];
78
79 for (int iy=0; iy<n; iy++) {
80 String[] cols = lines[iy].split(erottimet);
81 for (int ix = 0; ix < m; ix++) {
82 if ( ix >= cols.length) break;
83 tulos[iy][ix] = Mjonot.erotaInt(cols[ix], 0);
84 }
85 }
86
87 return tulos;
88 }
89
90
91
109 public static int[][] getMatriisi(String jono) {
110 return getMatriisi(jono.split("\n"));
111 }
112
113
114
118 public int[][] getMatriisi() {
119 return getMatriisi(textMatriisi.getText());
120 }
121
122
123
126 public int getValue() {
127 return (Integer)spinner.getValue();
128 }
129
130
131
135 public void setValue(int n) {
136 spinner.setValue(n);
137
138 }
139
140
141
145 public void setText(String jono) {
146 textMatriisi.setText(jono);
147 }
148
149 }
150