📄 groupbutton.java
字号:
/*
* 创建日期 2004-11-22
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package game;
import java.util.*;
import java.awt.*;
import javax.swing.*;
/**
* @author Jerry703
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class GroupButton extends JPanel{
public int MaxX;
public int MaxY;
public SingleButton[][] groupbutton;
public GridLayout layout;
public Random random = new Random();
public OptionItem[] selectitem;
public int sum;
public GameListener listener;
public int count;
public GroupButton(int x,int y){
this.MaxX = x;
this.MaxY = y;
this.setSelect();
listener = new GameListener(this);
groupbutton = new SingleButton[MaxX][MaxY];
layout = new GridLayout(MaxX,MaxY);
this.setLayout(layout);
this.setButton();
this.setBackground(Color.blue);
this.repaint();
}
public void setSelect(){
selectitem = new OptionItem[11];
for (int i=0;i<selectitem.length;++i){
selectitem[i] = new OptionItem();
selectitem[i].str = (char)(65 + i);
String path = "image/" + i + ".jpg";
java.net.URL imgURL=GroupButton.class.getResource(path);
selectitem[i].img = new ImageIcon(imgURL);
}
}
/*protected static ImageIcon createImageIcon(String path){
java.net.URL imgURL=GroupButton.class.getResource(path);
if(imgURL!=null){
return new ImageIcon(imgURL);
}else{
return null;
}
}*/
/**
* @return
*/
public void setButton(){
try{
this.sum = this.MaxX * this.MaxY;
for (int i=0;i<this.MaxX;++i){
for (int j=0;j<this.MaxY;++j){
groupbutton[i][j] = new SingleButton("%",j,i);
groupbutton[i][j].addActionListener(listener);
groupbutton[i][j].setBackground(Color.GRAY);
//groupbutton[i][j].empty = false;
this.add(groupbutton[i][j]);
}
}
for (int i=0;i<MaxX;++i){
int inttemp;
char strtemp;
ImageIcon imgtemp;
for (int j=0;j<MaxY;++j){
if (groupbutton[i][j].getText() == "%"){
inttemp = (int)(random.nextFloat() * 11);
strtemp = selectitem[inttemp].str;
imgtemp = selectitem[inttemp].img;
groupbutton[i][j].setText(null);
groupbutton[i][j].setIcon(imgtemp);
groupbutton[i][j].setType(strtemp);
groupbutton[i][j].repaint();
findEmpty(strtemp,imgtemp);
this.sum = this.sum - 2;
if (sum <= 0)
break;
}
}
if (sum <= 0)
break;
}
count = this.MaxX * this.MaxY;
}catch(Exception e){
e.printStackTrace();
}
}
public void findEmpty(char strtemp,ImageIcon imgtemp) throws Exception{
for (int i=0;i<MaxX;++i){
int findx = (int)(random.nextFloat() * MaxX);
int findy = (int)(random.nextFloat() * MaxY);
if (groupbutton[findx][findy].getText() == "%"){
groupbutton[findx][findy].setIcon(imgtemp);
groupbutton[findx][findy].setText(null);
//groupbutton[findx][findy] = new SingleButton(imgtemp,findy,findx);
groupbutton[findx][findy].setType(strtemp);
return;
}
}
int i=0;
int j=0;
for (i=MaxX-1;i>=0;--i){
for (j=MaxY-1;j>=0;--j){
if (groupbutton[i][j].getText() == "%")
break;
}
if (j<0)
continue;
if (groupbutton[i][j].getText() == "%")
break;
}
if (i<0)
throw new Exception();
groupbutton[i][j].setText(null);
groupbutton[i][j].setIcon(imgtemp);
//groupbutton[i][j] = new SingleButton(imgtemp,j,i);
groupbutton[i][j].setType(strtemp);
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -