mapgenerator.java
来自「主要是对于JAVA的编程的基本语言 希望能够帮得上你。」· Java 代码 · 共 224 行
JAVA
224 行
package swing;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
public class MapGenerator extends JFrame implements ActionListener {
/**
* 常量、变量定义部分
*/
static MapGenerator mg;
JPanel backPanel, centerPanel,leftPanel, rightPanel, bottomPanel;
JButton btnOK,btnGenerate,btnRandom,btnExit;
JButton[][] btnBlock;
JScrollPane scrollPane;
JTextArea txtMap;
JLabel lblRow, lblCol;
StringBuffer buffer;
JComboBox rowList, colList;
boolean[][] m_bSelected;
int[][] map;
int ROW, COL;
public static void main(String arg[]) {
mg=new MapGenerator("地图生成器V1.0_华卫_2008年4月8日");
}
public void setRow(int row){
ROW=row;
}
public void setCol(int col){
COL=col;
}
public void initialize(){
/**
* 设计地图生成器界面 *
*/
if(ROW==0){
ROW=50;
}
if(COL==0){
COL=50;
}
backPanel = new JPanel(new BorderLayout());
centerPanel=new JPanel(new GridLayout(1,2));
leftPanel = new JPanel(new GridLayout(ROW, COL));
rightPanel=new JPanel(new GridLayout(1,1));
bottomPanel = new JPanel();
rowList = new JComboBox();
for(int i=3; i<=50;i++){
rowList.addItem(String.valueOf(i));
}
colList = new JComboBox();
for(int i=3; i<=50;i++){
colList.addItem(String.valueOf(i));
}
rowList.setSelectedIndex(ROW-3);
colList.setSelectedIndex(COL-3);
btnOK=new JButton("确定");
btnGenerate = new JButton("按图生成地图数组");
btnRandom = new JButton("随机生成地图数组");
btnBlock = new JButton[ROW][COL];
btnExit=new JButton("退出");
m_bSelected = new boolean[ROW][COL];
map = new int[ROW][COL];
lblRow=new JLabel("设置行数:");
lblCol=new JLabel("设置列数:");
txtMap=new JTextArea("");
txtMap.setEditable(false);
txtMap.setFont(new Font("Gorgia",Font.BOLD,14));
scrollPane=new JScrollPane(txtMap,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
this.setContentPane(backPanel);
backPanel.add(centerPanel, "Center");
backPanel.add(bottomPanel, "South");
centerPanel.add(leftPanel);
centerPanel.add(rightPanel);
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
btnBlock[i][j] = new JButton("");
btnBlock[i][j].setBackground(Color.WHITE);
leftPanel.add(btnBlock[i][j]);
map[i][j]=0;
}
}
rightPanel.add(scrollPane);
bottomPanel.add(lblRow);
bottomPanel.add(rowList);
bottomPanel.add(lblCol);
bottomPanel.add(colList);
bottomPanel.add(btnOK);
bottomPanel.add(btnGenerate);
bottomPanel.add(btnRandom);
bottomPanel.add(btnExit);
// 设置窗口
this.setSize(900, 620);
this.setResizable(false);
this.setLocationRelativeTo(null); // 让窗口在屏幕居中
// 将窗口设置为可见的
this.setVisible(true);
// 注册监听器
this.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
btnOK.addActionListener(this);
btnGenerate.addActionListener(this);
btnRandom.addActionListener(this);
btnExit.addActionListener(this);
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
btnBlock[i][j].addActionListener(this);
}
}
}
public MapGenerator(String name) {
super(name);
initialize();
}
public void actionPerformed(ActionEvent e) {
buffer=new StringBuffer();
if (e.getSource() == btnGenerate) {
buffer.append("map["+ROW+"]["+COL+"]={\n");
for (int i = 0; i < ROW; i++) {
buffer.append("{");
for (int j = 0; j < COL; j++) {
if (j < COL - 1) {
buffer.append(map[i][j] + ", ");
} else if (i != ROW - 1) {
buffer.append(map[i][j] + "}, ");
} else {
buffer.append(map[i][j] + "}");
}
}
buffer.append("\n");
}
buffer.append("}");
txtMap.setText(buffer.toString());
}else if(e.getSource()==btnRandom){
Random r=new Random();
buffer.append("map["+ROW+"]["+COL+"]={\n");
for (int i = 0; i < ROW; i++) {
buffer.append("{");
for (int j = 0; j < COL; j++) {
int k=Math.abs(r.nextInt())%2;
if (j < COL - 1) {
buffer.append(k + ", ");
} else if (i != ROW - 1) {
buffer.append(k + "}, ");
} else {
buffer.append(k + "}");
}
if(k==1){
btnBlock[i][j].setBackground(Color.BLACK);
map[i][j]=1;
m_bSelected[i][j]=true;
}else{
btnBlock[i][j].setBackground(Color.WHITE);
map[i][j]=0;
m_bSelected[i][j]=false;
}
}
buffer.append("\n");
}
buffer.append("}");
txtMap.setText(buffer.toString());
}else if(e.getSource()==btnOK){
mg.setRow(rowList.getSelectedIndex()+3);
mg.setCol(colList.getSelectedIndex()+3);
initialize();
}else if(e.getSource()==btnExit){
System.exit(0);
}else{
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (e.getSource() == btnBlock[i][j]) {
m_bSelected[i][j] = !m_bSelected[i][j];
if (m_bSelected[i][j]) {
btnBlock[i][j].setBackground(Color.BLACK);
map[i][j] = 1;
} else {
btnBlock[i][j].setBackground(Color.WHITE);
map[i][j] = 0;
}
break;
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?