📄 startup.java
字号:
/*
* Created on 9-okt-2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package gui;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
//import javax.swing.SwingUtilities;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import logic.ProblemSolver_Width;
import logic.ProblemSolver_GreedySearch;
import logic.ProblemSolver;
import logic.PuzzleState;
import logic.PuzzleNumber;
/**
*
* The graphical user interface
*/
public class StartUp extends JFrame{
private PuzzleState targetState, startState;
private static int initialNumberOfMoves = 15;
public StartUp()
{
targetState = new PuzzleState();
this.setATarget(targetState);
shuffle(StartUp.initialNumberOfMoves);
this.initializeGUI();
this.pack();
this.setVisible(true);
}
private void initializeGUI()
{
TopOfThisWindow topPanel = new TopOfThisWindow();
this.getContentPane().setLayout(new BorderLayout());
CenterOfThisWindow centerPanel = new CenterOfThisWindow();
this.getContentPane().add(topPanel, BorderLayout.NORTH);
this.getContentPane().add(centerPanel, BorderLayout.CENTER);
}
private void shuffle(int numberOfMoves)
{
startState = targetState.shuffle(numberOfMoves);
}
/*
* This method creates the puzzle that represents the 'solution'.
* In this case we make a puzzle:
* 1 2 3
* 9 # 4
* 7 6 5
*
* If you have to use another 'target', just change the implementation of this method.
*
* */
private void setATarget(PuzzleState puzzle)
{
int number = 1;
DecimalFormat formatter = new DecimalFormat("#00");
if(PuzzleState.NMBR_OF_ROWS == 3 && PuzzleState.NMBR_OF_COLUMNS == 3)
{
puzzle.setPuzzleNumber(new PuzzleNumber(formatter.format(1)), 0, 0);
puzzle.setPuzzleNumber(new PuzzleNumber(formatter.format(2)), 0, 1);
puzzle.setPuzzleNumber(new PuzzleNumber(formatter.format(3)), 0, 2);
puzzle.setPuzzleNumber(new PuzzleNumber(formatter.format(8)), 1, 0);
puzzle.setPuzzleNumber(null, 1, 1);
puzzle.setPuzzleNumber(new PuzzleNumber(formatter.format(4)), 1, 2);
puzzle.setPuzzleNumber(new PuzzleNumber(formatter.format(7)), 2, 0);
puzzle.setPuzzleNumber(new PuzzleNumber(formatter.format(6)), 2, 1);
puzzle.setPuzzleNumber(new PuzzleNumber(formatter.format(5)), 2, 2);
}else
{
for(int i = 0; i < PuzzleState.NMBR_OF_ROWS; i++)
{
for(int j = 0; j < PuzzleState.NMBR_OF_COLUMNS; j++)
{
puzzle.setPuzzleNumber(new PuzzleNumber(formatter.format(number)), i, j);
number++;
}
}
puzzle.setPuzzleNumber(null, PuzzleState.NMBR_OF_ROWS - 1, PuzzleState.NMBR_OF_COLUMNS - 1);
}
}
/*
* The main class that launches the application.
* */
public static void main(String[] args) {
StartUp app = new StartUp();
app.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
/*
* Inner class that shows the source and target puzzle + the number of shuffles.
* */
class TopOfThisWindow extends JPanel implements ActionListener
{
private JEditorPane editorPaneStartsituation, editorPaneGoal;
private JButton btnShuffle;
private JFormattedTextField txtNumberOfMoves;
private JPanel puzzlegames, shufflePanel;
public TopOfThisWindow()
{
editorPaneStartsituation = new JEditorPane();
editorPaneStartsituation.setOpaque(false);
editorPaneStartsituation.setToolTipText("This is the 'start' position");
editorPaneStartsituation.setEditable(false);
HTMLEditorKit editor1 = new HTMLEditorKit();
editor1.install(editorPaneStartsituation);
editorPaneStartsituation.setContentType("text/html");
editorPaneStartsituation.setText(startState.getHtml());
NumberFormat nmbrMoves = NumberFormat.getIntegerInstance();
DecimalFormat nmbrView = new DecimalFormat("#,###,### moves");
txtNumberOfMoves = new JFormattedTextField(new DefaultFormatterFactory(
new NumberFormatter(nmbrView),
new NumberFormatter(nmbrView),
new NumberFormatter(nmbrMoves)));
txtNumberOfMoves.setValue(new Integer(StartUp.initialNumberOfMoves));
txtNumberOfMoves.setColumns(6);
txtNumberOfMoves.setToolTipText("Enter the number of random moves that should be performed during the 'shuffle'");
txtNumberOfMoves.addActionListener(this);
btnShuffle = new JButton("Shuffle");
btnShuffle.addActionListener(this);
editorPaneGoal = new JEditorPane();
editorPaneGoal.setOpaque(false);
editorPaneGoal.setToolTipText("This is the 'endpoint' position");
editorPaneGoal.setEditable(false);
HTMLEditorKit editor2 = new HTMLEditorKit();
editor2.install(editorPaneGoal);
editorPaneGoal.setContentType("text/html");
editorPaneGoal.setText(targetState.getHtml());
puzzlegames = new JPanel();
puzzlegames.setLayout(new FlowLayout(FlowLayout.CENTER));
puzzlegames.add(editorPaneGoal);
puzzlegames.add(editorPaneStartsituation);
shufflePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
shufflePanel.add(txtNumberOfMoves);
shufflePanel.add(btnShuffle);
this.setLayout(new BorderLayout());
this.add(shufflePanel, BorderLayout.EAST);
this.add(puzzlegames, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent event) {
Number nmbr = (Number)txtNumberOfMoves.getValue();
shuffle(nmbr.intValue());
editorPaneStartsituation.setText(startState.getHtml());
}
}
class CenterOfThisWindow extends JPanel implements ActionListener
{
private JTabbedPane tabbedPane;
private JButton btnSearchSolution;
public CenterOfThisWindow()
{
tabbedPane = new JTabbedPane();
tabbedPane.addTab("Width search", new TabbedPaneTab(new ProblemSolver_Width()));
tabbedPane.addTab("Greedy search", new TabbedPaneTab(new ProblemSolver_GreedySearch()));
this.setLayout(new BorderLayout());
btnSearchSolution = new JButton("Search solution");
btnSearchSolution.addActionListener(this);
this.add(tabbedPane, BorderLayout.CENTER);
this.add(btnSearchSolution, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent arg0) {
TabbedPaneTab tab = (TabbedPaneTab)tabbedPane.getSelectedComponent();
tab.showSolution();
}
}
class TabbedPaneTab extends JPanel implements ActionListener
{
private ProblemSolver problemSolver;
private JEditorPane editorPaneSolutionPath, editorPaneSummary;
private PuzzleState solution;
private JButton btnNext, btnPrevious;
private JPanel centerPanel;
public TabbedPaneTab(ProblemSolver problemSolver)
{
this.problemSolver = problemSolver;
editorPaneSolutionPath = new JEditorPane();
editorPaneSolutionPath.setEditable(false);
editorPaneSolutionPath.setOpaque(false);
HTMLEditorKit editor1 = new HTMLEditorKit();
editor1.install(editorPaneSolutionPath);
editorPaneSolutionPath.setContentType("text/html");
editorPaneSummary = new JEditorPane();
editorPaneSummary.setEditable(false);
editorPaneSummary.setOpaque(false);
HTMLEditorKit editor2 = new HTMLEditorKit();
editor2.install(editorPaneSummary);
editorPaneSummary.setContentType("text/html");
btnNext = new JButton("Next");
btnPrevious = new JButton("Previous");
btnNext.addActionListener(this);
btnPrevious.addActionListener(this);
centerPanel = new JPanel(new BorderLayout());
centerPanel.add(editorPaneSolutionPath, BorderLayout.WEST);
centerPanel.add(editorPaneSummary, BorderLayout.EAST);
this.setLayout(new BorderLayout());
this.add(centerPanel, BorderLayout.CENTER);
this.add(btnNext, BorderLayout.EAST);
this.add(btnPrevious, BorderLayout.WEST);
}
public Dimension getPreferredSize()
{
return new Dimension(500,200);
}
public void showSolution()
{
PuzzleState solution;
try{
solution = problemSolver.getSolution(startState, targetState);
}catch(OutOfMemoryError ex)
{
JOptionPane.showMessageDialog(null, "Out of memory. Could not find a solution \n" + ex.getMessage()+ "\n\nnodes expanded: " + problemSolver.getNumberOfNodesExpanded(), "error", JOptionPane.ERROR_MESSAGE);
return;
}
if(solution != null)
{
editorPaneSolutionPath.setText(solution.getHtml());
editorPaneSummary.setText(problemSolver.getSummaryInHtml());
}
else
JOptionPane.showMessageDialog(null, "ERROR: Could not find a solution!", "error", JOptionPane.ERROR_MESSAGE);
}
public void actionPerformed(ActionEvent event) {
PuzzleState state = null;
if(event.getSource() == btnNext)
{
state = problemSolver.getNext();
}else if(event.getSource() == btnPrevious)
{
state = problemSolver.getPrevious();
}
if(state != null)
{ editorPaneSolutionPath.setOpaque(false);
editorPaneSolutionPath.setText(state.getHtml());
}else
JOptionPane.showMessageDialog(null, "No next/previous node available...", "error", JOptionPane.WARNING_MESSAGE);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -