/**
   * Constructor creates graphic components and adds them to the JPanel this class inherits from.
   */
  public SlidePuzzleGUI(String solver) {
    // default settings for internal attributes: unsolved 15 puzzle
    nPuzzle = 15;
    solved = false;
    this.solver = solver;
    // create graphics
    // create menu bar with pull down menu for puzzle configuration
    // this needs a menu and a menu item in it
    menuBar = new JMenuBar();
    menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
    // create a menu and add it to the menu bar.
    JMenu menu = new JMenu("Menu");
    menu.setMnemonic(KeyEvent.VK_M);
    // create menu item
    JMenuItem eMenuItem = new JMenuItem("Configuration");
    eMenuItem.setMnemonic(KeyEvent.VK_C);
    eMenuItem.setToolTipText("Set Puzzle Configuration");
    eMenuItem.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent event) {
            createFrame(); // create configuration frame
          }
        });
    menu.add(eMenuItem);
    menuBar.add(menu);

    // create button to solve puzzle in a step by step manner
    // The listener is too lengthy for an anonymous class,
    // code for the listener resides in internal NewGameAction class.
    JButton newGameButton = new JButton("Solve");
    ActionListener gameAction = new NewGameAction();
    newGameButton.addActionListener(gameAction);

    // create control panel that holds the solve button
    JPanel controlPanel = new JPanel();
    controlPanel.setLayout(new FlowLayout());
    controlPanel.add(newGameButton);

    // create graphics panel
    initalPuzzleGraphics = new GraphicsPanel(nPuzzle, PanelType.INITIALPANEL);
    intermediatePuzzleGraphics = new GraphicsPanel(nPuzzle, PanelType.INTERMEDIATEPANEL);
    finalPuzzleGraphics = new GraphicsPanel(nPuzzle, PanelType.GOALPANEL);
    // create and configure a solver
    constructPuzzleSolver();
    // the panel uses a borderlayout
    // the menubar goes on top
    setLayout(new BorderLayout());
    add(menuBar, BorderLayout.NORTH);
    add(controlPanel, BorderLayout.SOUTH);
    add(initalPuzzleGraphics, BorderLayout.WEST);
    add(intermediatePuzzleGraphics, BorderLayout.CENTER);
    add(finalPuzzleGraphics, BorderLayout.EAST);
  }
Пример #2
0
    /** Create the UI for this editor */
    void makeUI() {

      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.setBorder(new LineBorder(Color.blue));
      getContentPane().add(mainPanel, BorderLayout.CENTER);

      // the map and associated toolbar
      npEditControl = new NPController();
      mapEditPanel = npEditControl.getNavigatedPanel(); // here's where the map will be drawn
      mapEditPanel.setPreferredSize(new Dimension(250, 250));
      mapEditPanel.setSelectRegionMode(true);
      JToolBar navToolbar = mapEditPanel.getNavToolBar();
      navToolbar.setFloatable(false);
      JToolBar moveToolbar = mapEditPanel.getMoveToolBar();
      moveToolbar.setFloatable(false);
      // toolbar.remove("setReference");

      JPanel toolbar = new JPanel();
      List localMaps = maps;
      if (localMaps == null) {
        localMaps = getDefaultMaps();
      }
      JMenu mapMenu = new JMenu("Maps");
      JMenuBar menuHolder = new JMenuBar();
      menuHolder.setBorder(null);
      menuHolder.add(mapMenu);
      toolbar.add(menuHolder);
      for (int mapIdx = 0; mapIdx < localMaps.size(); mapIdx++) {
        final MapData mapData = (MapData) localMaps.get(mapIdx);
        final JCheckBoxMenuItem cbx =
            new JCheckBoxMenuItem(mapData.getDescription(), mapData.getVisible());
        if (mapData.getVisible()) {
          toggleMap(mapData, true);
        }
        mapMenu.add(cbx);
        cbx.addItemListener(
            new ItemListener() {
              public void itemStateChanged(ItemEvent event) {
                toggleMap(mapData, cbx.isSelected());
              }
            });
      }
      GuiUtils.limitMenuSize(mapMenu, "Maps ", 20);

      toolbar.add(navToolbar);
      toolbar.add(moveToolbar);

      JPanel mapSide = new JPanel();
      mapSide.setLayout(new BorderLayout());
      TitledBorder mapBorder =
          new TitledBorder(
              standardBorder, "Edit Projection", TitledBorder.ABOVE_TOP, TitledBorder.CENTER);
      mapSide.setBorder(mapBorder);
      mapSide.add(toolbar, BorderLayout.NORTH);
      mapSide.add(mapEditPanel, BorderLayout.CENTER);
      mainPanel.add(mapSide, BorderLayout.WEST);

      // the projection parameters

      // the Projection name
      JLabel nameLabel = GuiUtils.rLabel("Name: ");
      nameTF = new JTextField(20);

      // the list of Projection classes is kept in a comboBox
      typeLabel = GuiUtils.rLabel("Type: ");
      projClassCB = new JComboBox();
      // standard list of projection classes
      List classNames = getDefaultProjections();
      for (int i = 0; i < classNames.size(); i++) {
        String className = (String) classNames.get(i);
        try {
          projClassCB.addItem(new ProjectionClass(className));
        } catch (ClassNotFoundException ee) {
          System.err.println("ProjectionManager failed on " + className + " " + ee);
        } catch (IntrospectionException ee) {
          System.err.println("ProjectionManager failed on " + className + " " + ee);
        }
      }
      GuiUtils.tmpInsets = new Insets(4, 4, 4, 4);
      JPanel topPanel =
          GuiUtils.doLayout(
              new Component[] {nameLabel, nameTF, typeLabel, projClassCB},
              2,
              GuiUtils.WT_N,
              GuiUtils.WT_N);

      // the Projection parameter area
      paramPanel = new JPanel();
      paramPanel.setLayout(new BorderLayout());
      paramPanel.setBorder(
          new TitledBorder(
              standardBorder,
              "Projection Parameters",
              TitledBorder.ABOVE_TOP,
              TitledBorder.CENTER));

      // the bottom button panel
      JPanel buttPanel = new JPanel();
      JButton acceptButton = new JButton("Save");
      JButton previewButton = new JButton("Preview");
      JButton cancelButton = new JButton("Cancel");
      buttPanel.add(acceptButton, null);
      buttPanel.add(previewButton, null);
      buttPanel.add(cancelButton, null);

      JPanel mainBox = GuiUtils.topCenterBottom(topPanel, paramPanel, buttPanel);
      mainPanel.add(mainBox, BorderLayout.CENTER);
      pack();

      // enable event listeners when we're done constructing the UI
      projClassCB.addActionListener(
          new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              ProjectionClass selectClass = (ProjectionClass) projClassCB.getSelectedItem();
              setProjection(selectClass.makeDefaultProjection());
            }
          });

      acceptButton.addActionListener(
          new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
              accept();
            }
          });
      previewButton.addActionListener(
          new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
              ProjectionClass projClass = findProjectionClass(editProjection);
              if (null != projClass) {
                setProjFromDialog(projClass, editProjection);
                setProjection(editProjection);
              }
            }
          });
      cancelButton.addActionListener(
          new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
              NewProjectionDialog.this.setVisible(false);
            }
          });
    }
Пример #3
0
  public PuzzelHA() {

    window = new JFrame("PuzzelHA");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // window.setSize(800, 600);

    // -----------------Menü erstellen---------------------------------------
    JMenuBar menu = new JMenuBar();
    menu.setBorder(new LineBorder(Color.BLACK, 1));

    JMenu spiel = new JMenu("Spiel");
    JMenuItem reset = new JMenuItem("Reset");
    reset.addActionListener(this);
    reset.setActionCommand("reset");
    spiel.add(reset);

    JMenu ansicht = new JMenu("Ansicht");
    JMenu bild = new JMenu("BildNr.");
    ansicht.add(bild);
    ButtonGroup bg1 = new ButtonGroup();

    for (int j = 1; j <= bilder.size(); j++) {
      JRadioButtonMenuItem bn = new JRadioButtonMenuItem("" + j);
      bn.setName("" + j);
      bn.setActionCommand("b");
      bn.addActionListener(this);
      bg1.add(bn);
      bild.add(bn);
    }

    // --------------RadioButtons für Puzzelgröße----------------------------------
    JMenu pg = new JMenu("Puzzelgröße");
    ansicht.add(pg);
    ButtonGroup bg2 = new ButtonGroup();
    JRadioButtonMenuItem pgb1 = new JRadioButtonMenuItem("2x3");
    pgb1.setActionCommand("g1");
    pgb1.addActionListener(this);
    bg2.add(pgb1);
    pg.add(pgb1);

    for (int i = 2; i <= 4; i++) {
      JRadioButtonMenuItem pgb = new JRadioButtonMenuItem("" + (i * 2) + "x" + (i * 2 - 1));
      pgb.setActionCommand("g" + i);
      pgb.addActionListener(this);
      bg2.add(pgb);
      pg.add(pgb);
    }

    JMenu help = new JMenu("?");
    JMenuItem info = new JMenuItem("Info");
    info.addActionListener(this);
    info.setActionCommand("?");
    help.add(info);

    menu.add(spiel);
    menu.add(ansicht);
    menu.add(help);
    window.add(menu);
    window.setJMenuBar(menu);

    // ---------------- Menü END-------------------------------------

    baueButton();

    window.setResizable(false);
    window.setLocationRelativeTo(null);
    window.pack();
    window.setVisible(true);
  }