-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainJFrame.java
148 lines (119 loc) · 4.23 KB
/
MainJFrame.java
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
package com.mason.ui;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
public class MainJFrame extends JFrame implements KeyListener {
int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
Random ram = new Random ();
int steps = 0;
public MainJFrame (){
initWindow();
initMenu();
initImage();
this.setVisible (true);
}
private void initImage() {
JLabel stepCount = new JLabel("Steps: " + steps);
stepCount.setBounds(0, 0, 500, 25);
this.getContentPane().add(stepCount);
//打乱arr数组的顺序
for (int i = 0; i < arr.length; i++) {
int index1 = ram.nextInt(arr.length);
int temp = arr[i];
arr[i] = arr[index1];
arr[index1] = temp;
}
for (int i = 0; i < arr.length; i++) {
int temp = arr[i];
ImageIcon pic = new ImageIcon ("src/Images/Me/" + temp + ".jpeg");
JLabel picLabel = new JLabel(pic);
if (i <= 2){
picLabel.setBounds(256*(i), 25, 256, 256);
} else if (i <= 5) {
picLabel.setBounds(256*(i-3), 256 + 25, 256, 256);
} else {
picLabel.setBounds(256*(i-6), 256 * 2 + 25, 256, 256);
}
this.getContentPane().add(picLabel);
}
}
private void initMenu() {
JMenuBar menuBar = new JMenuBar();
JMenu options = new JMenu("Options");
JMenu about = new JMenu("About");
JMenu changePic = new JMenu("Change pictures");
JMenu restart = new JMenu("Restart");
JMenu reLogin = new JMenu("Relogin");
JMenu quit = new JMenu("quit");
JMenu scan = new JMenu("Wechat");
options.add(changePic);
options.add(restart);
options.add(reLogin);
options.add(quit);
about.add(scan);
menuBar.add(options);
menuBar.add(about);
this.setJMenuBar(menuBar);
}
private void initWindow() {
this.setSize(256*3, 256*3 + 75);
this.setTitle("Jigsaw");
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setLayout(null);
this.addKeyListener(this);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
int findBlank = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 9){
findBlank = i;
}
}
int input = e.getKeyCode();
if(input == 37 && (findBlank != 0 && findBlank != 3 && findBlank != 6)){
arr[findBlank] = arr[findBlank - 1];
arr[findBlank - 1] = 9;
steps++;
} else if (input == 38 && (findBlank != 0 && findBlank != 1 && findBlank != 2)) {
arr[findBlank] = arr[findBlank - 3];
arr[findBlank - 3] = 9;
steps++;
} else if (input == 39 && (findBlank != 2 && findBlank != 5 && findBlank != 8)) {
arr[findBlank] = arr[findBlank + 1];
arr[findBlank + 1] = 9;
steps++;
} else if (input == 40 && (findBlank != 6 && findBlank != 7 && findBlank != 8)) {
arr[findBlank] = arr[findBlank + 3];
arr[findBlank + 3] = 9;
steps++;
}
this.getContentPane().removeAll();
JLabel stepCount = new JLabel("steps: " + steps);
stepCount.setBounds(0, 0, 500, 25);
this.getContentPane().add(stepCount);
for (int i = 0; i < arr.length; i++) {
int temp = arr[i];
ImageIcon pic = new ImageIcon ("src/Images/Me/" + temp + ".jpeg");
JLabel picLabel = new JLabel(pic);
if (i <= 2){
picLabel.setBounds(256*(i), 25, 256, 256);
} else if (i <= 5) {
picLabel.setBounds(256*(i-3), 256 + 25, 256, 256);
} else {
picLabel.setBounds(256*(i-6), 256 * 2 +25, 256, 256);
}
this.getContentPane().add(picLabel);
this.getContentPane().repaint();
}
}
}