GUIグリッドのサイズを25x25から40x40に拡大しようとしていますが、GUIグリッドサイズをコード内に配置しようとしていましたが、何らかの理由でこれを実行できませんでしたGUIのグリッドサイズを変更するために何を変更する必要があるのかを誰かが理解できるように助けてくれれば、本当にそれをお勧めします。Java GUIグリッドサイズを増やす
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MouseTeam
*/
public class GUI extends JFrame {
private int mouseX = 0, mouseY = 0;
private JButton startButton; // start button!
private JButton[][] buttons; // have all the grid buttons
private JPanel gridPanel;// the panel holding the grid of buttons
private JPanel[][] mainGrid; // each panel holds a button from button variable matrix of buttons
private JLabel numberOfLeftFood, numberOfSteps; // labels in the gui
private ArrayList<Object[]> bestGenerationsAndGrid; // holds all best of each 10 generations
private JTextField terminalProbability, crossoverProbability, mutationProbability,
gridWidth, gridHeight, maxDepth, populationSize, numberOfGenerations;
private JLabel generationNoLabel;
/**
* initilizes the primere look of the gui and it's characteristics
*/
public GUI() {
int width = Parameters.gridWidth;
int height = Parameters.gridHeight;
gridPanel = new JPanel();
buttons = new JButton[width][height];
gridPanel.setLayout(new GridLayout(width, height));
mainGrid = new JPanel[width][height];
setSize(600, 600);
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
//--------------------------------------------------------------------------//
JPanel right = new javax.swing.JPanel();
startButton = new javax.swing.JButton();
terminalProbability = new javax.swing.JTextField();
crossoverProbability = new javax.swing.JTextField();
mutationProbability = new javax.swing.JTextField();
gridWidth = new javax.swing.JTextField();
gridHeight = new javax.swing.JTextField();
maxDepth = new javax.swing.JTextField();
populationSize = new javax.swing.JTextField();
numberOfGenerations = new javax.swing.JTextField();
JLabel jLabel1 = new javax.swing.JLabel();
JLabel jLabel2 = new javax.swing.JLabel();
JLabel jLabel3 = new javax.swing.JLabel();
JLabel jLabel4 = new javax.swing.JLabel();
JLabel jLabel5 = new javax.swing.JLabel();
JLabel jLabel6 = new javax.swing.JLabel();
JLabel jLabel7 = new javax.swing.JLabel();
JLabel jLabel8 = new javax.swing.JLabel();
generationNoLabel = new javax.swing.JLabel();
numberOfLeftFood = new javax.swing.JLabel();
numberOfSteps = new javax.swing.JLabel();
JPanel center = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
terminalProbability.setText("0.4");
startButton.setBackground(new java.awt.Color(0, 0, 0));
startButton.setForeground(new java.awt.Color(255, 255, 0));
startButton.setText("Start");
startButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startButtonAction(evt);
}
});
crossoverProbability.setText("0.8");
mutationProbability.setText("0.05");
gridWidth.setText("25");
gridHeight.setText("25");
maxDepth.setText("20");
populationSize.setText("100");
numberOfGenerations.setText("20");
jLabel1.setText("Terminal Probability");
jLabel2.setText("Mutation Probability");
jLabel3.setText("Crossover Probability");
jLabel4.setText("Grid Width");
jLabel5.setText("Grid Height");
jLabel6.setText("Max Depth");
jLabel7.setText("Population Size");
jLabel8.setText("Number Of Generations");
generationNoLabel.setText("Generation number");
generationNoLabel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
generationNoLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
generationNoLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
numberOfLeftFood.setText("Number of left food");
numberOfLeftFood.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
numberOfLeftFood.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
numberOfLeftFood.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
numberOfSteps.setText("Number of steps");
numberOfSteps.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
numberOfSteps.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
numberOfSteps.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
javax.swing.GroupLayout rightLayout = new javax.swing.GroupLayout(right);
right.setLayout(rightLayout);
rightLayout.setHorizontalGroup(
rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(rightLayout.createSequentialGroup()
.addContainerGap()
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(generationNoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(startButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(rightLayout.createSequentialGroup()
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(rightLayout.createSequentialGroup()
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5)
.addComponent(jLabel6)
.addComponent(jLabel7)
.addComponent(jLabel1))
.addGap(5, 5, 5))
.addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(numberOfGenerations, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(maxDepth, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(gridHeight, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(gridWidth, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(mutationProbability, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(terminalProbability, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(crossoverProbability, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(populationSize, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(numberOfLeftFood, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(numberOfSteps, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
rightLayout.setVerticalGroup(
rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(rightLayout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(startButton)
.addGap(18, 18, 18)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(terminalProbability, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(mutationProbability, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(crossoverProbability, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(gridWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(gridHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addGap(18, 18, 18)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(maxDepth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addGap(18, 18, 18)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(populationSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(numberOfGenerations, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8))
.addGap(18, 18, 18)
.addComponent(generationNoLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(numberOfLeftFood)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(numberOfSteps)
.addContainerGap(65, Short.MAX_VALUE))
);
//---------------------------------------------------------------------------------------//
initGrid();
add(gridPanel, BorderLayout.CENTER);
add(right, BorderLayout.EAST);
}
/**
*take cheese coordinates and fill the grid with the cheese
*/
public void putCheese(ArrayList<Coordinate> cheese) {
for (int i = 0; i < cheese.size(); ++i) {
Coordinate coord = cheese.get(i);
updatDisplay(coord.i, coord.j, "2.png");
}
}
/**
* Make a button with a resizible images
*/
public static JButton makeButton(String path) {
try {
BufferedImage master;
if (path == "") {
path = "default.png";
}
master = ImageIO.read(new File(path));
JButton btn = new JButton() {
@Override
public Dimension getPreferredSize() {
return new Dimension(90, 50);
}
};
btn.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
JButton btn = (JButton) e.getComponent();
Dimension size = btn.getSize();
Insets insets = btn.getInsets();
size.width -= insets.left + insets.right;
size.height -= insets.top + insets.bottom;
if (size.width > size.height) {
size.width = -1;
} else {
size.height = -1;
}
Image scaled = master.getScaledInstance(size.width, size.height, java.awt.Image.SCALE_SMOOTH);
btn.setIcon(new ImageIcon(scaled));
}
});
return btn;
} catch (IOException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
/**
* start mouse move
*/
public void startMouse() {
MouseThread mTh = new MouseThread();
mTh.start();
}
/**
* handle Start button click, with initilize Parameters fields and displaying the grids variables and start it..
*
* @param evt
*/
public void startButtonAction(java.awt.event.ActionEvent evt) {
this.generationNoLabel.setText("Please wait..");
//initilize Parameters fields
Parameters.terminalProbability = Double.parseDouble(terminalProbability.getText());
Parameters.crossoverProbability = Double.parseDouble(crossoverProbability.getText());
Parameters.mutationProbability = Double.parseDouble(mutationProbability.getText());
if ((Parameters.terminalProbability < 0 || Parameters.terminalProbability > 1)
|| (Parameters.crossoverProbability < 0 || Parameters.crossoverProbability > 1)
|| (Parameters.mutationProbability < 0 || Parameters.mutationProbability > 1)) {
JOptionPane.showMessageDialog(null, "Please Enter probabilities between 0 and 1 inclusive");
return;
}
Parameters.gridWidth = Integer.parseInt(gridWidth.getText());
Parameters.gridHeight = Integer.parseInt(gridHeight.getText());
if (Parameters.gridWidth > 25 || Parameters.gridHeight > 25) {
JOptionPane.showMessageDialog(null, "Max recommended grid size is 25 * 25!");
return;
}
Parameters.maxDepth = Integer.parseInt(maxDepth.getText());
Parameters.populationSize = Integer.parseInt(populationSize.getText());
Parameters.numberOfGenerations = Integer.parseInt(numberOfGenerations.getText());
startButton.setEnabled(false);
// initilize main program objects like grid and tournament selection, population etc..
Grid g = new Grid(Parameters.gridHeight, Parameters.gridWidth, Parameters.cheeseFraction);
// System.out.println(g.getNumberOfCheeseInGrid());
TournamentSelection sel = new TournamentSelection(Parameters.crossoverProbability);
Population p = new Population(Parameters.populationSize, g, sel);
//now starting evolution
Evolution e = new Evolution(p, Parameters.numberOfGenerations);
e.evolve(this);
//start actual mouse simulation
startMouse();
// startButton.setEnabled(true);
}
/**
* update cell at x,y with picture that have path
*/
public void updatDisplay(int x, int y, String path) {
mainGrid[x][y].removeAll();
buttons[x][y] = makeButton(path);
buttons[x][y].setBackground(Color.green);
mainGrid[x][y].add(buttons[x][y]);
}
/**
* change visited cell to red
*/
public void updateVisitedButton(int x, int y) {
mainGrid[x][y].removeAll();
buttons[x][y].removeComponentListener(null);
buttons[x][y] = new JButton();
buttons[x][y].setBackground(Color.red);
mainGrid[x][y].add(buttons[x][y]);
}
/**
* reinitilize the grid with the given cheese positions
*
* @param cheese
*/
public void initGrid() {
gridPanel.removeAll();
int width = Parameters.gridWidth;
int height = Parameters.gridHeight;
buttons = new JButton[width][height];
gridPanel.setLayout(new GridLayout(width, height));
mainGrid = new JPanel[width][height];
//make all cells green again and put mouse at position 0 0
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
mainGrid[i][j] = new JPanel();
mainGrid[i][j].setLayout(new GridLayout(1, 1));
buttons[i][j] = new JButton();
buttons[i][j].setBackground(Color.green);
mainGrid[i][j].add(buttons[i][j]);
gridPanel.add(mainGrid[i][j]);
}
}
add(gridPanel, BorderLayout.CENTER);
updatDisplay(0, 0, "right.png");
validate();
repaint();
}
public void setBestGenerationsAndGrid(ArrayList<Object[]> bestGenerationsAndGrid) {
this.bestGenerationsAndGrid = bestGenerationsAndGrid;
}
/**
* the class that handles mouse moving on the grid
*/
class MouseThread extends Thread {
Coordinate coord;
boolean waited = false;
public MouseThread() {
}
@Override
public void run() {
//start actual mouse simulation
Object[] temp;
ArrayList<Coordinate> path, grid;
int[] fitness;
for (int j = 0; j < bestGenerationsAndGrid.size(); ++j) {
//have the best of the ith * 10 path and the correpsonding grid
temp = bestGenerationsAndGrid.get(j);
path = (ArrayList<Coordinate>) temp[0];
fitness = (int[]) temp[1];
grid = (ArrayList< Coordinate>) temp[2];
//make all grid green
initGrid();
//put cheese on the grid and the mouse
putCheese(grid);
generationNoLabel.setText("Best of the first " + ((j + 1) * 10) + " generations");
numberOfLeftFood.setText("Number of food left: " + fitness[0]);
numberOfSteps.setText("Number of Steps: " + fitness[1]);
//start simulation
for (int i = 0; i < path.size(); ++i) {
coord = path.get(i);
try {
if (!waited) {
Thread.sleep(3000);
waited = true;
} else {
Thread.sleep(100);
}
updateVisitedButton(mouseX, mouseY);
updatDisplay(coord.i, coord.j, "right.png");
validate();
repaint();
mouseX = coord.i;
mouseY = coord.j;
} catch (InterruptedException ex) {
JOptionPane.showMessageDialog(null, "error");
}
}
}
}
}
}
残りはグリッドクラスのために十分ではありません誰かが指摘できますか?どのようにGUIのグリッドサイズを増やす? はあなた
これは何ですか? – gpasch
はい、「コード内にGUIグリッドサイズを配置しようとしましたが、何らかの理由でこれを実行できませんでした」。私はそれに答えていると思う –