-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathCopyOfPackInCycle.java
More file actions
166 lines (146 loc) · 4.17 KB
/
Copy pathCopyOfPackInCycle.java
File metadata and controls
166 lines (146 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package packInCycle;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.TableModel;
public class CopyOfPackInCycle
{
//创建一个主函数
public static void main(String[] args)
{
try {
System.out.print("请输入矩阵边长:N=");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
final int num = Integer.parseInt(in.readLine());
System.out.println("num = " + num);
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new MyTableFrame(num);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
System.out.println("NumberFormatException:" + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("IOException:" + e.getMessage());
}
}
}
class MyTableFrame extends JFrame
{
public MyTableFrame(Integer N)
{
setTitle("螺旋打印数组");
setSize(N*40,N*40);
FlowLayout fly = new FlowLayout();
this.setLayout(fly);
model = new MyModel(N);
//将表格模型加到表格对象中
table.setModel(model);
//设置表格的高度
table.setRowHeight(30);
//逐一设置宽度
for(int col=0;col<table.getColumnCount();col++)
{
//得到表格列对象
javax.swing.table.TableColumn tablecol = table.getColumnModel().getColumn(col);
tablecol.setPreferredWidth(30);
}
//将表格添加到窗体中
this.add(table);
//关闭窗体时退出程序
this.setDefaultCloseOperation(3);
//设置窗体出现时的位置
this.setLocationRelativeTo(null);
//将窗体设置为可见
this.setVisible(true);
//得到表格模型对象
TableModel tablemodel = table.getModel();
//强制转化为MyModel对象
MyModel myModel = (MyModel)tablemodel;
String[][] data = myModel.GetData();
int tableRow = myModel.getRowCount();
int tableColumn = myModel.getColumnCount();
for(int i=0;i<tableRow;i++)
{
for(int j=0;j<tableColumn;j++)
{
data[i][j] = "0";
}
}
int row = 0;
int column = 0;
data[row][column] = "1";
String dir = "R";
table.updateUI();
int loopNum = N*N;
System.out.println("loopNum=" + loopNum);
for(Integer num=2; num<=loopNum; num++)
{
//得到MyModel中的二维数组
data = myModel.GetData();
if(data==null||table==null)
{
System.out.println("出错");
break;
}
if(dir.equalsIgnoreCase("R")){
if(row<0 || row==tableRow || column<0 || column+1==tableColumn || data[row][column+1] != "0")
{
dir = "D";
num--;
}else{
System.out.println("rrrrrrrrrrrrrrrrrrr");
data[row][column+1] = num.toString();
column++;
table.updateUI();
}
}else if(dir.equalsIgnoreCase("D")){
if(row<0 || row+1==tableRow || column<0 || column==tableColumn || data[row+1][column] != "0")
{
dir = "L";
num--;
}else{
System.out.println("ddddddddddddddddddd");
data[row+1][column] = num.toString();
row++;
table.updateUI();
}
}else if(dir.equalsIgnoreCase("L")){
if(row<0 || row==tableRow || column-1<0 || column==tableColumn || data[row][column-1] != "0")
{
dir = "T";
num--;
}else{
System.out.println("lllllllllllllllllll");
data[row][column-1] = num.toString();
column--;
table.updateUI();
}
}else if(dir.equalsIgnoreCase("T")){
if(row-1<0 || row==tableRow || column<0 || column==tableColumn || data[row-1][column] != "0")
{
dir = "R";
num--;
}else{
System.out.println("ttttttttttttttttttt");
data[row-1][column] = num.toString();
row--;
table.updateUI();
}
}
table.updateUI();
}
}
private static JTable table;
private static MyModel model;
}