forked from KeepMoving/AlgorithmsLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackInCycle.java
More file actions
156 lines (136 loc) · 4.07 KB
/
Copy pathPackInCycle.java
File metadata and controls
156 lines (136 loc) · 4.07 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
package packInCycle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PackInCycle
{
//创建一个主函数
public static void main(String[] args)
{
try {
System.out.print("请输入矩阵边长:N=");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(in.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("IOException:" + e.getMessage());
}
//创建一个登录对象
PackInCycle lg = new PackInCycle();
//调用其中的方法
try {
lg.UI();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Exception:" + e.getMessage());
}
}
public void UI() throws Exception
{
//先创建一个窗体
javax.swing.JFrame jf = new javax.swing.JFrame("螺旋打印数组");
//设置窗体的大小
jf.setSize(N*40, N*40);
//创建一个流式布局管理器
java.awt.FlowLayout fl = new java.awt.FlowLayout();
jf.setLayout(fl);
//创建一个表格对象
table = new javax.swing.JTable();
//创建一个表格模型对象
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);
}
//将表格添加到窗体中
jf.add(table);
//关闭窗体时退出程序
jf.setDefaultCloseOperation(3);
//设置窗体出现时的位置
jf.setLocationRelativeTo(null);
//将窗体设置为可见
jf.setVisible(true);
//得到表格模型对象
javax.swing.table.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(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 Integer N;
private static javax.swing.JTable table;
private static MyModel model;
}