📄 mainframe.java
字号:
try{
Thread.sleep(sleepTime);
}catch(Exception e){
e.printStackTrace();
}
scrLabel.setIcon(new ImageIcon(ImagePath + '\\'+ "NULL.jpg"));
dstLabel.setIcon(new ImageIcon(ImagePath + '\\'+ "NULL.jpg"));
ImageArray.setNULL(scrX,scrY);
ImageArray.setNULL(dstX,dstY);
line = 0;
super.paint(g);
}
else if(line == 2){
g.setColor(Color.red);
g.drawLine(start.x,start.y,first.x,first.y);
g.drawLine(first.x,first.y,finish.x,finish.y);
try{
Thread.sleep(sleepTime);
}catch(Exception e){
}
scrLabel.setIcon(new ImageIcon(ImagePath +'\\'+ "NULL.jpg"));
dstLabel.setIcon(new ImageIcon(ImagePath +'\\'+ "NULL.jpg"));
ImageArray.setNULL(scrX,scrY);
ImageArray.setNULL(dstX,dstY);
line = 0;
super.paint(g);
}
else if(line == 3){
g.setColor(Color.red);
g.drawLine(start.x,start.y,second.x,second.y);
g.drawLine(second.x,second.y,first.x,first.y);
g.drawLine(first.x,first.y,finish.x,finish.y);
try{
Thread.sleep(sleepTime);
}catch(Exception e){
}
scrLabel.setIcon(new ImageIcon(ImagePath +'\\'+ "NULL.jpg"));
dstLabel.setIcon(new ImageIcon(ImagePath +'\\'+ "NULL.jpg"));
ImageArray.setNULL(scrX,scrY);
ImageArray.setNULL(dstX,dstY);
line = 0;
super.paint(g);
}
}
public void showPassDialog(){
progressBarThread.end();
JOptionPane.showMessageDialog(null, "挑战成功!");
jButtonStart.setEnabled(true);
jButtonArrange.setEnabled(false);
jProgressBar.setValue(maxTime);
}
class ProgressBarThread extends Thread{
private int curTime = maxTime;
boolean go = true;
public void minusTime(int n){
curTime = curTime - n;
}
public void addTime(int n){
curTime = curTime + n;
if(curTime > maxTime)
curTime = maxTime;
}
public void end(){
go = false;
}
public void run(){
while(go){
if(curTime >= 0){
try {
jProgressBar.setValue(curTime--);
sleep(200);
} catch (InterruptedException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
else{
jProgressBar.setValue(0);
JOptionPane.showMessageDialog(null, "时间耗尽!挑战失败!");
go = false;
showBlankDesktop();
jButtonStart.setEnabled(true);
jProgressBar.setValue(maxTime);
jButtonArrange.setEnabled(false);
}
}
}
}
void showBlankDesktop(){
for(int j=1; j <= HEIGHT;j++){
for(int i=1; i <= WIDTH; i++){
ImageName = ImagePath + '\\' + "NULL.jpg";
((JLabel)jPanel.getComponent((j-1)*WIDTH + i-1)).setIcon(new ImageIcon(ImageName));
((JLabel)jPanel.getComponent((j-1)*WIDTH + i-1)).setBorder(null);
}
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel = new javax.swing.JPanel();
jProgressBar = new javax.swing.JProgressBar();
jSeparator1 = new javax.swing.JSeparator();
jLabel1 = new javax.swing.JLabel();
jButtonArrange = new javax.swing.JButton();
jButtonStart = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("连连看");
setBounds(new java.awt.Rectangle(20, 40, 1000, 700));
setResizable(false);
jPanel.setLayout(new java.awt.GridLayout(9, 16, 5, 4));
jProgressBar.setMaximum(500);
jProgressBar.setPreferredSize(new java.awt.Dimension(146, 10));
jLabel1.setText("时间:");
jButtonArrange.setText("重新排列");
jButtonArrange.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonArrangeActionPerformed(evt);
}
});
jButtonStart.setText("开始游戏");
jButtonStart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonStartActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addComponent(jPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 880, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(48, 48, 48)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 614, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addComponent(jButtonStart)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonArrange))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 955, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
.addComponent(jButtonStart)
.addComponent(jButtonArrange)
.addComponent(jLabel1)
.addComponent(jProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(45, 45, 45)
.addComponent(jPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(60, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButtonStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStartActionPerformed
// TODO add your handling code here:
reStartGame();
jButtonStart.setEnabled(false);
}//GEN-LAST:event_jButtonStartActionPerformed
private void jButtonArrangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonArrangeActionPerformed
// TODO add your handling code here:
ImageArray.reArrange();
for(int j=1; j <= HEIGHT;j++){
for(int i=1; i <= WIDTH; i++){
int val = ImageArray.getImageArray(i, j);
if( val == -1)
ImageName = ImagePath + '\\' + "NULL.jpg";
else
ImageName = ImagePath + '\\' + val + ".jpg";
((JLabel)jPanel.getComponent((j-1)*WIDTH + i-1)).setIcon(new ImageIcon(ImageName));
}
}
progressBarThread.minusTime(maxTime/8);
}//GEN-LAST:event_jButtonArrangeActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButtonArrange;
private javax.swing.JButton jButtonStart;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel;
private javax.swing.JProgressBar jProgressBar;
private javax.swing.JSeparator jSeparator1;
// End of variables declaration//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -