void initControls() { JMenuItem jmi; jmi = new JMenuItem("JImage Menu"); jmi.setEnabled(false); popupMenu.add(jmi); jmi = new JMenuItem("Fit"); jmi.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { fit = true; repaint(); } }); popupMenu.add(jmi); JMenu scaleMenu = new JMenu("Set Scale"); popupMenu.add(scaleMenu); int scales[] = new int[] {25, 50, 100, 200, 400, 800}; for (int i = 0; i < scales.length; i++) { jmi = new JMenuItem(scales[i] + " %"); jmi.addActionListener(new ScaleAction(scales[i])); scaleMenu.add(jmi); } MyListener l = new MyListener(); addMouseMotionListener(l); addMouseListener(l); addMouseWheelListener(l); addKeyListener(l); }
public void actionPerformed(ActionEvent event) { JMenuItem source = (JMenuItem) (event.getSource()); for (GraphView v : gp.getGraphViewList()) { if (v.getMenuText().equals(source.getText())) { v.view(); repaint(); return; } } for (GraphDrawer d : gp.getGraphDrawerList()) { if (d.getMenuText().equals(source.getText())) { d.layout(); repaint(); return; } } for (GraphUtility u : gp.getGraphUtilityList()) { if (u.getMenuText().equals(source.getText())) { u.apply(); repaint(); return; } } for (GraphExperiment ge : gp.getGraphExperimentList()) { if (ge.getMenuText().equals(source.getText())) { ge.experiment(); repaint(); return; } } }
private void addToRecent(String s) { for (int i = 0; i < iMaxRecent; i++) { if (s.equals(sRecent[i])) { return; } } if (sRecent[iRecent] != null) { mRecent.remove(iRecent); } sRecent[iRecent] = s; if (s.length() > 43) { s = s.substring(0, 40) + "..."; } JMenuItem item = new JMenuItem(s); item.setActionCommand("#" + iRecent); item.addActionListener(this); mRecent.insert(item, iRecent); iRecent = (iRecent + 1) % iMaxRecent; }
private void addMenuItems(JMenu f, String m[]) { Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); for (int i = 0; i < m.length; i++) { if (m[i].equals("--")) { f.addSeparator(); } else if (m[i].equals("---")) { // (ulrivo): full size on screen with less than 640 width if (d.width >= 640) { f.addSeparator(); } else { return; } } else { JMenuItem item = new JMenuItem(m[i].substring(1)); char c = m[i].charAt(0); if (c != '-') { item.setMnemonic(c); } item.addActionListener(this); f.add(item); } } }
// create the menu bar (changed to private) private static JMenuBar createMenuBar() { JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("File"); menuBar.add(menu); JMenuItem saveItem = new JMenuItem(" Save... "); saveItem.addActionListener(std); saveItem.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); menu.add(saveItem); return menuBar; }
/** Method to create the menu bar, menus, and menu items */ private void setUpMenuBar() { // create menu menuBar = new JMenuBar(); zoomMenu = new JMenu("Zoom"); twentyFive = new JMenuItem("25%"); fifty = new JMenuItem("50%"); seventyFive = new JMenuItem("75%"); hundred = new JMenuItem("100%"); hundred.setEnabled(false); hundredFifty = new JMenuItem("150%"); twoHundred = new JMenuItem("200%"); fiveHundred = new JMenuItem("500%"); // add the action listeners twentyFive.addActionListener(this); fifty.addActionListener(this); seventyFive.addActionListener(this); hundred.addActionListener(this); hundredFifty.addActionListener(this); twoHundred.addActionListener(this); fiveHundred.addActionListener(this); // add the menu items to the menus zoomMenu.add(twentyFive); zoomMenu.add(fifty); zoomMenu.add(seventyFive); zoomMenu.add(hundred); zoomMenu.add(hundredFifty); zoomMenu.add(twoHundred); zoomMenu.add(fiveHundred); menuBar.add(zoomMenu); // set the menu bar to this menu pictureFrame.setJMenuBar(menuBar); }
/** Method to enable all menu commands */ private void enableZoomItems() { twentyFive.setEnabled(true); fifty.setEnabled(true); seventyFive.setEnabled(true); hundred.setEnabled(true); hundredFifty.setEnabled(true); twoHundred.setEnabled(true); fiveHundred.setEnabled(true); }
/** * Esta funcion se usa para pintar la barra de seleccion de los menus. Esta aqui para no repetirla * en todas partes... */ static void pintaBarraMenu(Graphics g, JMenuItem menuItem, Color bgColor) { ButtonModel model = menuItem.getModel(); Color oldColor = g.getColor(); int menuWidth = menuItem.getWidth(); int menuHeight = menuItem.getHeight(); if (menuItem.isOpaque()) { g.setColor(menuItem.getBackground()); g.fillRect(0, 0, menuWidth, menuHeight); } if ((menuItem instanceof JMenu && !(((JMenu) menuItem).isTopLevelMenu()) && model.isSelected()) || model.isArmed()) { RoundRectangle2D.Float boton = new RoundRectangle2D.Float(); boton.x = 1; boton.y = 0; boton.width = menuWidth - 3; boton.height = menuHeight - 1; boton.arcwidth = 8; boton.archeight = 8; GradientPaint grad = new GradientPaint(1, 1, getBrilloMenu(), 0, menuHeight, getSombraMenu()); Graphics2D g2D = (Graphics2D) g; g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(bgColor); g2D.fill(boton); g.setColor(bgColor.darker()); g2D.draw(boton); g2D.setPaint(grad); g2D.fill(boton); g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT); } g.setColor(oldColor); }
/** * Controls the zoom menu bar * * @param a the ActionEvent */ public void actionPerformed(ActionEvent a) { if (a.getActionCommand().equals("Update")) { this.repaint(); } if (a.getActionCommand().equals("25%")) { this.zoom(.25); enableZoomItems(); twentyFive.setEnabled(false); } if (a.getActionCommand().equals("50%")) { this.zoom(.50); enableZoomItems(); fifty.setEnabled(false); } if (a.getActionCommand().equals("75%")) { this.zoom(.75); enableZoomItems(); seventyFive.setEnabled(false); } if (a.getActionCommand().equals("100%")) { this.zoom(1.0); enableZoomItems(); hundred.setEnabled(false); } if (a.getActionCommand().equals("150%")) { this.zoom(1.5); enableZoomItems(); hundredFifty.setEnabled(false); } if (a.getActionCommand().equals("200%")) { this.zoom(2.0); enableZoomItems(); twoHundred.setEnabled(false); } if (a.getActionCommand().equals("500%")) { this.zoom(5.0); enableZoomItems(); fiveHundred.setEnabled(false); } }
private void initMenu() { JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); // File Menu JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); menuBar.add(fileMenu); JMenuItem fileNewItem = new JMenuItem("New", KeyEvent.VK_N); fileNewItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK)); fileMenu.add(fileNewItem); JMenuItem fileOpenItem = new JMenuItem("Open...", KeyEvent.VK_O); fileOpenItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK)); fileMenu.add(fileOpenItem); JMenuItem fileOpenAdjacencyItem = new JMenuItem("Open Adjacency File..."); fileMenu.add(fileOpenAdjacencyItem); JMenuItem fileOpenWeightedAdjacencyItem = new JMenuItem("Open Weighted Adjacency File..."); fileMenu.add(fileOpenWeightedAdjacencyItem); JMenuItem fileOpenXMLItem = new JMenuItem("Open XML File..."); fileMenu.add(fileOpenXMLItem); JMenuItem fileSaveItem = new JMenuItem("Save", KeyEvent.VK_S); fileSaveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK)); fileMenu.add(fileSaveItem); JMenuItem fileSaveAsItem = new JMenuItem("Save As..."); fileMenu.add(fileSaveAsItem); JMenuItem fileSaveSimpleItem = new JMenuItem("Save Simple Graph..."); fileMenu.add(fileSaveSimpleItem); JMenuItem fileSaveXMLItem = new JMenuItem("Save XML File..."); fileMenu.add(fileSaveXMLItem); JMenuItem filePNGItem = new JMenuItem("Export to png"); fileMenu.add(filePNGItem); JMenuItem fileExitItem = new JMenuItem("Exit", KeyEvent.VK_X); fileExitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK)); fileMenu.add(fileExitItem); fileExitItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { fileExit(); } }); fileNewItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { fileNew(); } }); fileOpenItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { fileOpen(); } }); fileOpenXMLItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { fileOpenXML(); } }); fileSaveItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { fileSave(); } }); fileSaveAsItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { fileSaveAs(); } }); fileSaveSimpleItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { fileSaveSimple(); } }); fileSaveXMLItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { fileSaveXML(); } }); filePNGItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { filePNG(); } }); // Edit Menu JMenu editMenu = new JMenu("Edit"); editMenu.setMnemonic(KeyEvent.VK_E); menuBar.add(editMenu); JMenuItem editNodesItem = new JMenuItem("Edit Selected Nodes...", KeyEvent.VK_N); editNodesItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.SHIFT_MASK)); editMenu.add(editNodesItem); JMenuItem editEdgesItem = new JMenuItem("Edit Selected Edges...", KeyEvent.VK_E); editEdgesItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.SHIFT_MASK)); editMenu.add(editEdgesItem); JMenuItem editEdgeTypesItem = new JMenuItem("Edit Edge Types..."); editMenu.add(editEdgeTypesItem); JMenuItem editNodeTypesItem = new JMenuItem("Edit Node Types..."); editMenu.add(editNodeTypesItem); JMenuItem editMoveGraphItem = new JMenuItem("Move Graph..."); editMenu.add(editMoveGraphItem); JMenuItem editAddEdgeBendItem = new JMenuItem("Add Edge Bend"); editMenu.add(editAddEdgeBendItem); JMenuItem editRemoveEdgeBendsItem = new JMenuItem("Remove Edge Bends"); editMenu.add(editRemoveEdgeBendsItem); JMenuItem editSelectAllItem = new JMenuItem("Select All", KeyEvent.VK_A); editSelectAllItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK)); editMenu.add(editSelectAllItem); editNodesItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { gp.editNodes(gp.getSelection().getNodes()); } }); editEdgesItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { gp.editEdges(gp.getSelection().getEdges()); } }); editEdgeTypesItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { gp.editEdgeTypes(); } }); editNodeTypesItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { gp.editNodeTypes(); } }); editMoveGraphItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { gp.moveGraph(); } }); editAddEdgeBendItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { gp.addEdgeBend(); } }); editRemoveEdgeBendsItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { gp.removeEdgeBends(); } }); editSelectAllItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { editSelectAll(); } }); // View Menu JMenu viewMenu = new JMenu("View"); viewMenu.setMnemonic(KeyEvent.VK_V); menuBar.add(viewMenu); for (GraphView v : gp.getGraphViewList()) { JMenuItem menuItem = new JMenuItem(v.getMenuText(), v.getMnemonicKey()); menuItem.setAccelerator(KeyStroke.getKeyStroke(v.getAcceleratorKey(), 0)); menuItem.addActionListener(this); viewMenu.add(menuItem); } // Experiment Menu JMenu experimentsMenu = new JMenu("Experiments"); experimentsMenu.setMnemonic(KeyEvent.VK_E); menuBar.add(experimentsMenu); for (GraphExperiment ge : gp.getGraphExperimentList()) { JMenuItem menuItem = new JMenuItem(ge.getMenuText(), ge.getMnemonicKey()); menuItem.setAccelerator(KeyStroke.getKeyStroke(ge.getAcceleratorKey(), 0)); menuItem.addActionListener(this); experimentsMenu.add(menuItem); } // Utilities Menu JMenu utilitiesMenu = new JMenu("Utilities"); utilitiesMenu.setMnemonic(KeyEvent.VK_U); menuBar.add(utilitiesMenu); for (GraphUtility u : gp.getGraphUtilityList()) { JMenuItem menuItem = new JMenuItem(u.getMenuText(), u.getMnemonicKey()); menuItem.setAccelerator(KeyStroke.getKeyStroke(u.getAcceleratorKey(), 0)); menuItem.addActionListener(this); utilitiesMenu.add(menuItem); } JMenu layoutMenu = new JMenu("Layout"); layoutMenu.setMnemonic(KeyEvent.VK_L); menuBar.add(layoutMenu); for (GraphDrawer d : gp.getGraphDrawerList()) { JMenuItem menuItem = new JMenuItem(d.getMenuText(), d.getMnemonicKey()); menuItem.setAccelerator(KeyStroke.getKeyStroke(d.getAcceleratorKey(), 0)); menuItem.addActionListener(this); layoutMenu.add(menuItem); } }
public static void main(String args[]) throws Exception { final JFrame frame = new JFrame(); final JTextArea textArea = new JTextArea(30, 60); final JScrollPane scrollPane = new JScrollPane(textArea); frame.getContentPane().add(scrollPane); JMenuBar menuBar = new JMenuBar(); frame.setJMenuBar(menuBar); JMenu menu = new JMenu("File"); ScreenCapture.createImage(menu, "menu.jpg"); menuBar.add(menu); JMenuItem menuItem = new JMenuItem("Frame Image"); menu.add(menuItem); menuItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { // Let the menu close and repaint itself before taking the image new Thread() { public void run() { try { Thread.sleep(50); System.out.println("Creating frame.jpg"); frame.repaint(); ScreenCapture.createImage(frame, "frame.jpg"); } catch (Exception exc) { System.out.println(exc); } } }.start(); }; }); final JButton button = new JButton("Create Images"); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { System.out.println("Creating desktop.jpg"); ScreenCapture.createDesktopImage("desktop.jpg"); System.out.println("Creating frame.jpg"); ScreenCapture.createImage(frame, "frame.jpg"); System.out.println("Creating scrollpane.jpg"); ScreenCapture.createImage(scrollPane, "scrollpane.jpg"); System.out.println("Creating textarea.jpg"); ScreenCapture.createImage(textArea, "textarea.jpg"); System.out.println("Creating button.jpg"); ScreenCapture.createImage(button, "button.jpg"); button.setText("button refreshed"); button.paintImmediately(button.getBounds()); System.out.println("Creating refresh.jpg"); ScreenCapture.createImage(button, "refresh.jpg"); System.out.println("Creating region.jpg"); Rectangle r = new Rectangle(0, 0, 100, 16); ScreenCapture.createImage(textArea, r, "region.png"); } catch (Exception exc) { System.out.println(exc); } } }); frame.getContentPane().add(button, BorderLayout.SOUTH); try { FileReader fr = new FileReader("ScreenCapture.java"); BufferedReader br = new BufferedReader(fr); textArea.read(br, null); br.close(); } catch (Exception e) { } frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); }
public CBPopupMenu() { JMenuItem item; item = new JMenuItem("Standard Gradient"); item.setActionCommand("standard"); item.addActionListener(this); add(item); item = new JMenuItem("Invert map"); item.setActionCommand("invert"); item.addActionListener(this); add(item); item = new JMenuItem("Rainbow gradient"); item.setActionCommand("rainbow"); item.addActionListener(this); add(item); item = new JMenuItem("Greyscale gradient"); item.setActionCommand("grey"); item.addActionListener(this); add(item); item = new JMenuItem("Swap Background Black/White"); item.setActionCommand("swapBackground"); item.addActionListener(this); add(item); }
public ImageProcessingFrame() { setTitle("ImageProcessingTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); add( new JComponent() { public void paintComponent(Graphics g) { if (image != null) g.drawImage(image, 0, 0, null); } }); JMenu fileMenu = new JMenu("File"); JMenuItem openItem = new JMenuItem("Open"); openItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { openFile(); } }); fileMenu.add(openItem); JMenuItem exitItem = new JMenuItem("Exit"); exitItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); fileMenu.add(exitItem); JMenu editMenu = new JMenu("Edit"); JMenuItem blurItem = new JMenuItem("Blur"); blurItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { float weight = 1.0f / 9.0f; float[] elements = new float[9]; for (int i = 0; i < 9; i++) elements[i] = weight; convolve(elements); } }); editMenu.add(blurItem); JMenuItem sharpenItem = new JMenuItem("Sharpen"); sharpenItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { float[] elements = {0.0f, -1.0f, 0.0f, -1.0f, 5.f, -1.0f, 0.0f, -1.0f, 0.0f}; convolve(elements); } }); editMenu.add(sharpenItem); JMenuItem brightenItem = new JMenuItem("Brighten"); brightenItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { float a = 1.1f; // float b = 20.0f; float b = 0; RescaleOp op = new RescaleOp(a, b, null); filter(op); } }); editMenu.add(brightenItem); JMenuItem edgeDetectItem = new JMenuItem("Edge detect"); edgeDetectItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { float[] elements = {0.0f, -1.0f, 0.0f, -1.0f, 4.f, -1.0f, 0.0f, -1.0f, 0.0f}; convolve(elements); } }); editMenu.add(edgeDetectItem); JMenuItem negativeItem = new JMenuItem("Negative"); negativeItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { short[] negative = new short[256 * 1]; for (int i = 0; i < 256; i++) negative[i] = (short) (255 - i); ShortLookupTable table = new ShortLookupTable(0, negative); LookupOp op = new LookupOp(table, null); filter(op); } }); editMenu.add(negativeItem); JMenuItem rotateItem = new JMenuItem("Rotate"); rotateItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { if (image == null) return; AffineTransform transform = AffineTransform.getRotateInstance( Math.toRadians(5), image.getWidth() / 2, image.getHeight() / 2); AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC); filter(op); } }); editMenu.add(rotateItem); JMenuBar menuBar = new JMenuBar(); menuBar.add(fileMenu); menuBar.add(editMenu); setJMenuBar(menuBar); }
/** * Constructs a simple frame with the specified world and orientation. * * @param world the world view. * @param ao the axes orientation. */ public RSFFrame(World world, AxesOrientation ao) { super(new PlotPanel()); if (world == null) world = new World(); if (ao == null) ao = AxesOrientation.XRIGHT_YOUT_ZDOWN; _world = world; _view = new OrbitView(_world); _view.setAxesOrientation(ao); _canvas = new ViewCanvas(); _canvas.setView(_view); _canvas.setBackground(Color.WHITE); _points = new ArrayList<PointGroup>(); _lines = new ArrayList<LineGroup>(); _d = null; _tpx = null; _tpy = null; _ipg = null; _etc = null; _coord = null; ModeManager mm = new ModeManager(); mm.add(_canvas); OrbitViewMode ovm = new OrbitViewMode(mm); SelectDragMode sdm = new SelectDragMode(mm); JPopupMenu.setDefaultLightWeightPopupEnabled(false); ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic('F'); Action exitAction = new AbstractAction("Exit") { public void actionPerformed(ActionEvent event) { System.exit(0); } }; Action cubeAction = new AbstractAction("Add Cube") { public void actionPerformed(ActionEvent event) { String filename = chooseFile("."); if (filename != null) addRSFCube(filename); } }; Action lineAction = new AbstractAction("Add Line") { public void actionPerformed(ActionEvent event) { String filename = chooseFile("."); if (filename != null) addRSFLine(filename); } }; Action pointAction = new AbstractAction("Add Points") { public void actionPerformed(ActionEvent event) { String filename = chooseFile("."); if (filename != null) addRSFPoint(filename); } }; Action loadViewAction = new AbstractAction("Load Viewpoint") { public void actionPerformed(ActionEvent event) { String filename = chooseFile("."); if (filename != null) loadView(filename); } }; Action saveViewAction = new AbstractAction("Save Viewpoint") { public void actionPerformed(ActionEvent event) { JFileChooser chooser = new JFileChooser(new File(".")); int returnVal = chooser.showSaveDialog(new JFrame()); String filename = null; if (returnVal == JFileChooser.APPROVE_OPTION) { filename = chooser.getSelectedFile().getPath(); saveView(filename); } } }; Action saveFrameAction = new AbstractAction("Save to PNG") { public void actionPerformed(ActionEvent event) { JFileChooser chooser = new JFileChooser(new File(".")); int returnVal = chooser.showSaveDialog(new JFrame()); String filename = null; if (returnVal == JFileChooser.APPROVE_OPTION) { filename = chooser.getSelectedFile().getPath(); saveFrametoPNG(filename); } } }; JMenuItem cubeItem = fileMenu.add(cubeAction); cubeItem.setMnemonic('C'); JMenuItem lineItem = fileMenu.add(lineAction); lineItem.setMnemonic('L'); JMenuItem pointItem = fileMenu.add(pointAction); pointItem.setMnemonic('P'); JMenuItem saveViewItem = fileMenu.add(saveViewAction); saveViewItem.setMnemonic('V'); JMenuItem loadViewItem = fileMenu.add(loadViewAction); loadViewItem.setMnemonic('I'); JMenuItem saveFrameItem = fileMenu.add(saveFrameAction); saveFrameItem.setMnemonic('S'); JMenuItem exitItem = fileMenu.add(exitAction); exitItem.setMnemonic('X'); JMenu colorMenu = new JMenu("Color"); Action jetAction = new AbstractAction("Jet") { public void actionPerformed(ActionEvent event) { _color = ColorList.JET; setColorMap(); } }; Action prismAction = new AbstractAction("Prism") { public void actionPerformed(ActionEvent event) { _color = ColorList.PRISM; setColorMap(); } }; Action grayAction = new AbstractAction("Gray") { public void actionPerformed(ActionEvent event) { _color = ColorList.GRAY; setColorMap(); } }; Action rwbAction = new AbstractAction("Red-White-Blue") { public void actionPerformed(ActionEvent event) { _color = ColorList.RWB; setColorMap(); } }; colorMenu.add(jetAction); colorMenu.add(prismAction); colorMenu.add(grayAction); colorMenu.add(rwbAction); JMenu clipMenu = new JMenu("% Clip"); Action clipUp = new AbstractAction("Set max pclip") { public void actionPerformed(ActionEvent event) { String value = JOptionPane.showInputDialog(new JFrame(), "Percentile Clip Max (0-100.0):", _pmax); try { _pmax = Float.parseFloat(value); if (_pmax > 100.0f) _pmax = 100.0f; if (_pmax < _pmin) _pmax = _pmin + 1.0f; System.out.printf("pclip: (%f,%f) \n", _pmin, _pmax); _ipg.setPercentiles(_pmin, _pmax); } catch (Exception e) { System.out.println(e); } } }; Action clipDown = new AbstractAction("Set min pclip") { public void actionPerformed(ActionEvent event) { String value = JOptionPane.showInputDialog( new JFrame(), String.format("Percentile Clip Min (0-%f):", _pmax), _pmin); try { _pmin = Float.parseFloat(value); if (_pmin < 0.0f) _pmin = 0.0f; if (_pmin > _pmax) _pmin = _pmax - 1.0f; System.out.printf("pclip: (%f,%f) \n", _pmin, _pmax); _ipg.setPercentiles(_pmin, _pmax); } catch (Exception e) { System.out.println(e); } } }; clipMenu.add(clipUp); clipMenu.add(clipDown); JMenu modeMenu = new JMenu("Mode"); modeMenu.setMnemonic('M'); JMenuItem ovmItem = new JMenuItem(ovm); modeMenu.add(ovmItem); JMenuItem sdmItem = new JMenuItem(sdm); modeMenu.add(sdmItem); JMenu tensorMenu = new JMenu("Tensor"); Action tenLoadAction = new AbstractAction("Load Tensors") { public void actionPerformed(ActionEvent event) { String filename = chooseFile("."); if (filename != null) loadTensors(filename); } }; Action tenCoordLoadAction = new AbstractAction("Load Tensor Coordinates") { public void actionPerformed(ActionEvent event) { String filename = chooseFile("."); if (filename != null) loadTensorCoords(filename); } }; Action showTenAction = new AbstractAction("Show Tensors at Coordinates") { public void actionPerformed(ActionEvent event) { String filename; int sel; if (_ipg == null) { sel = JOptionPane.showConfirmDialog( null, "An RSF Cube (Image) needs to be loaded. " + " Would you like to load one?", "Notice", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if (sel == JOptionPane.OK_OPTION) { filename = chooseFile("."); if (filename != null) addRSFCube(filename); } else { JOptionPane.showConfirmDialog( null, "Cannot load tensors because an RFC cube was not loaded.", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); return; } } if (_coord == null) { sel = JOptionPane.showConfirmDialog( null, "The tensors coordinates need to be loaded. " + " Would you like to load them?", "Notice", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if (sel == JOptionPane.OK_OPTION) { filename = chooseFile("."); if (filename != null) loadCoord(filename); } else { JOptionPane.showConfirmDialog( null, "Error loading coordinates.", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); return; } } if (_etc == null) { sel = JOptionPane.showConfirmDialog( null, "The tensors at coordinates need to be loaded. " + " Would you like to load them?", "Notice", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if (sel == JOptionPane.OK_OPTION) { filename = chooseFile("."); if (filename != null) loadTensorCoords(filename); return; } else { JOptionPane.showConfirmDialog( null, "Cannot show tensors because the tensors at coordinates" + " were not loaded.", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); return; } } /* if(_d == null) { sel = JOptionPane.showConfirmDialog(null, "The tensors need to be loaded. " + " Would you like to load them?", "Notice", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if(sel == JOptionPane.OK_OPTION) { filename = chooseFile("."); if (filename != null) loadTensors(filename); } else { JOptionPane.showConfirmDialog(null, "Cannot show tensors because the tensors were not loaded.", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); return; } } if(_etg == null) { sel = JOptionPane.showConfirmDialog(null, "The tensor coordinates need to be loaded. " + " Would you like to load them?", "Notice", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if(sel == JOptionPane.OK_OPTION) { filename = chooseFile("."); if (filename != null) loadTensorCoords(filename); return; } else { JOptionPane.showConfirmDialog(null, "Cannot show tensors because the tensor coordinates" + " were not loaded.", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); return; } } */ _world.addChild(_etg); } }; Action hideTenAction = new AbstractAction("Hide Tensors at Coordinates") { public void actionPerformed(ActionEvent event) { if (_etg != null) _world.removeChild(_etg); } }; Action showTenPanAction = new AbstractAction("Show Tensor Panels") { public void actionPerformed(ActionEvent event) { String filename; int sel; if (_ipg == null) { sel = JOptionPane.showConfirmDialog( null, "An RSF Cube (Image) needs to be loaded. " + " Would you like to load one?", "Notice", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if (sel == JOptionPane.OK_OPTION) { filename = chooseFile("."); if (filename != null) addRSFCube(filename); } else { JOptionPane.showConfirmDialog( null, "Cannot load tensors because an RFC cube was not loaded.", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); return; } } if (_d == null) { sel = JOptionPane.showConfirmDialog( null, "The tensors need to be loaded. " + " Would you like to load them?", "Notice", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if (sel == JOptionPane.OK_OPTION) { filename = chooseFile("."); if (filename != null) loadTensors(filename); addRSFTensorEllipsoids(); } else { JOptionPane.showConfirmDialog( null, "Cannot show tensors because the tensors were not loaded.", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); return; } } else if (_tpx == null || _tpy == null) { addRSFTensorEllipsoids(); } else { ImagePanel ipx = _ipg.getImagePanel(Axis.X); ImagePanel ipy = _ipg.getImagePanel(Axis.Y); ipx.getFrame().addChild(_tpx); ipy.getFrame().addChild(_tpy); } } }; Action hideTenPanAction = new AbstractAction("Hide Tensor Panels") { public void actionPerformed(ActionEvent event) { if (_tpx != null) { ImagePanel ipx = _ipg.getImagePanel(Axis.X); ipx.getFrame().removeChild(_tpx); } if (_tpy != null) { ImagePanel ipy = _ipg.getImagePanel(Axis.Y); ipy.getFrame().removeChild(_tpy); } } }; JMenuItem tensorItem0 = tensorMenu.add(tenLoadAction); tensorItem0.setMnemonic('L'); JMenuItem tensorItem2 = tensorMenu.add(tenCoordLoadAction); tensorItem2.setMnemonic('C'); JMenuItem tensorItem1 = tensorMenu.add(showTenPanAction); tensorItem1.setMnemonic('T'); JMenuItem tensorItem5 = tensorMenu.add(hideTenPanAction); tensorItem5.setMnemonic('R'); JMenuItem tensorItem4 = tensorMenu.add(showTenAction); tensorItem4.setMnemonic('S'); JMenuItem tensorItem3 = tensorMenu.add(hideTenAction); tensorItem3.setMnemonic('H'); JMenuBar menuBar = new JMenuBar(); menuBar.add(fileMenu); menuBar.add(modeMenu); menuBar.add(tensorMenu); menuBar.add(colorMenu); menuBar.add(clipMenu); JToolBar toolBar = new JToolBar(SwingConstants.VERTICAL); toolBar.setRollover(true); JToggleButton ovmButton = new ModeToggleButton(ovm); toolBar.add(ovmButton); JToggleButton sdmButton = new ModeToggleButton(sdm); toolBar.add(sdmButton); _cb = new ColorBar(); _cb.setWidthMinimum(45); _cb.setFont(_cb.getFont().deriveFont(18.f)); // _ipg.addColorMapListener(_cb); ovm.setActive(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(new Dimension(SIZE, SIZE)); this.add(_canvas, BorderLayout.CENTER); this.add(toolBar, BorderLayout.WEST); this.add(_cb, BorderLayout.EAST); this.setJMenuBar(menuBar); this.setVisible(true); }
/** *********** Initialization of every component and variable being used ************* */ IPToolKit() { cp = new Container(); cp.setLayout(new BorderLayout()); Main_pnl = new JPanel(); jmb = new JMenuBar(); m_File = new JMenu("File"); m_Arith = new JMenu("Arithmetic"); m_Filter = new JMenu("Filters"); m_Open = new JMenuItem("Open"); m_Save = new JMenuItem("Save"); m_Exit = new JMenuItem("Exit"); m_Add = new JMenu("Add"); m_Sub = new JMenu("Subtract"); m_Mul = new JMenu("Multiply"); m_Div = new JMenu("Divide"); edge = new JMenu("Edge Detection"); mpre = new JMenu("Prewits"); msob = new JMenu("Sobel"); lbl_img = new JLabel(""); lbl_gray = new JLabel(""); lbl_res = new JLabel(""); jfc = new JFileChooser(); panel_image = new JPanel(); panel_gray = new JPanel(); panel_result = new JPanel(); scrollPane = new JScrollPane(); mAddConst = new JMenu("Constant"); mSubConst = new JMenu("Constant"); mMulConst = new JMenu("Constant"); mDivConst = new JMenu("Constant"); mAddSat = new JMenuItem("Saturation"); mAddWrap = new JMenuItem("Wrap Around"); mSubSat = new JMenuItem("Saturation"); mSubWrap = new JMenuItem("Wrap Around"); mMulSat = new JMenuItem("Saturation"); mMulWrap = new JMenuItem("Wrap Around"); mDivSat = new JMenuItem("Saturation"); mDivWrap = new JMenuItem("Wrap Around"); mAddImg = new JMenuItem("Image"); mSubImg = new JMenuItem("Image"); mMulImg = new JMenuItem("Image"); mDivImg = new JMenuItem("Image"); mlowPass = new JMenuItem("Low Pass"); mhighPass = new JMenuItem("High Pass"); mhighBoost = new JMenuItem("High Boost"); median = new JMenuItem("Median"); mBright = new JMenuItem("Brightness"); mContrast = new JMenuItem("Contrast Stretch"); mThreshold = new JMenuItem(" Threshold "); m_Enhance = new JMenu("Enhancement"); mInvert = new JMenuItem("Negative"); mpre_hor = new JMenuItem("Prewits Horizontal"); mpre_ver = new JMenuItem("Prewits Vertical"); mpre_both = new JMenuItem("Prewits Both"); msob_hor = new JMenuItem("Sobel Horizontal"); msob_ver = new JMenuItem("Sobel Vertical"); msob_both = new JMenuItem("Sobel Both"); ; mrob = new JMenuItem("Roberts"); mean = new JMenuItem("Mean"); mlap = new JMenuItem("Laplacian"); other = new JMenu("Other"); hist = new JMenuItem("Histogram Equalization"); conect = new JMenuItem("Connected Component"); mBlend = new JMenuItem("Blending"); vsb = new JScrollBar(JScrollBar.VERTICAL); hsb = new JScrollBar(JScrollBar.HORIZONTAL); // jsp = new JScrollPane(); setJMenuBar(jmb); add(cp); // setBackground(new Color(255,255,255)); panel_image.setBackground(new Color(255, 0, 0)); panel_gray.setBackground(new Color(0, 255, 0)); panel_result.setBackground(new Color(0, 0, 255)); jmb.add(m_File); jmb.add(m_Arith); jmb.add(m_Enhance); jmb.add(m_Filter); jmb.add(edge); jmb.add(other); cp.add(Main_pnl); Main_pnl.setLayout(new GridLayout(1, 3, 10, 10)); Main_pnl.add(panel_image); Main_pnl.add(panel_gray); Main_pnl.add(panel_result); m_File.add(m_Open); m_File.add(m_Save); m_File.add(m_Exit); m_Arith.add(m_Add); m_Add.add(mAddImg); m_Add.add(mAddConst); mAddConst.add(mAddSat); mAddConst.add(mAddWrap); m_Arith.add(m_Sub); m_Sub.add(mSubImg); m_Sub.add(mSubConst); mSubConst.add(mSubSat); mSubConst.add(mSubWrap); m_Arith.add(m_Mul); m_Mul.add(mMulImg); m_Mul.add(mMulConst); mMulConst.add(mMulSat); mMulConst.add(mMulWrap); m_Arith.add(m_Div); m_Div.add(mDivImg); m_Div.add(mDivConst); mDivConst.add(mDivSat); mDivConst.add(mDivWrap); m_Filter.add(mlowPass); m_Filter.add(mhighPass); m_Filter.add(mhighBoost); m_Filter.add(median); m_Filter.add(mean); edge.add(mpre); edge.add(msob); mpre.add(mpre_hor); mpre.add(mpre_ver); mpre.add(mpre_both); msob.add(msob_hor); msob.add(msob_ver); msob.add(msob_both); edge.add(mrob); edge.add(mlap); m_Enhance.add(mBright); m_Enhance.add(mContrast); m_Enhance.add(mThreshold); m_Enhance.add(mInvert); m_Enhance.add(mBlend); other.add(hist); other.add(conect); m_Open.addActionListener(this); m_Save.addActionListener(this); m_Exit.addActionListener(this); mAddImg.addActionListener(this); mSubImg.addActionListener(this); mMulImg.addActionListener(this); mDivImg.addActionListener(this); mAddSat.addActionListener(this); mAddWrap.addActionListener(this); mSubSat.addActionListener(this); mSubWrap.addActionListener(this); mMulSat.addActionListener(this); mMulWrap.addActionListener(this); mDivSat.addActionListener(this); mDivWrap.addActionListener(this); mlowPass.addActionListener(this); mhighPass.addActionListener(this); mhighBoost.addActionListener(this); median.addActionListener(this); mBright.addActionListener(this); mContrast.addActionListener(this); mThreshold.addActionListener(this); mInvert.addActionListener(this); mBlend.addActionListener(this); mpre_hor.addActionListener(this); mpre_ver.addActionListener(this); mpre_both.addActionListener(this); msob_hor.addActionListener(this); msob_ver.addActionListener(this); msob_both.addActionListener(this); mrob.addActionListener(this); mean.addActionListener(this); mlap.addActionListener(this); hist.addActionListener(this); conect.addActionListener(this); lbl_gray.addMouseListener(this); panel_image.add(lbl_img); panel_gray.add(lbl_gray); panel_result.add(lbl_res); cp.add(vsb, BorderLayout.EAST); cp.add(hsb, BorderLayout.SOUTH); // cp1 = getContentPane(); cp = getContentPane(); setSize(600, 400); setVisible(true); }
/** Constructor to set everything up */ public TicTac() { // Set defaults before settings are changed via menus // (defaults are vs AI, play as X, with graphics on) multiPlayers = false; temp = false; currentPlayer = Symbol.X; ai = Symbol.O; difficulty = Difficulty.IMPOSSIBLE; frame = new JFrame("Tic Tac Toe"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); // Set preferred size so game fills window nicely this.setPreferredSize(new Dimension(cellSize * ROWS, cellSize * COLS)); frame.add(this); // Load images try { sword = ImageIO.read(new File("swordsBlue.png")); saber = ImageIO.read(new File("saber2.png")); skull = ImageIO.read(new File("skull.png")); ring = ImageIO.read(new File("ring.png")); smiley = ImageIO.read(new File("smiley.png")); hydra = ImageIO.read(new File("hydra.png")); finalfantasy = ImageIO.read(new File("finalfantasy.jpg")); dragon = ImageIO.read(new File("dargon.jpg")); night = ImageIO.read(new File("night.jpg")); } catch (IOException e) { System.out.println("Could not open image files, turning images off!"); useImages = false; } imageX = saber; imageO = ring; background = dragon; // Create menu bars. Each one item will need a listener menuBar = new JMenuBar(); JMenu menu = new JMenu("Mode"); JMenu menu2 = new JMenu("Side"); JMenu menu3 = new JMenu("Graphics"); JMenu menu4 = new JMenu("Other"); JMenu menu5 = new JMenu("Difficulty"); JMenu xImageMenu = new JMenu("Image for X"); JMenu oImageMenu = new JMenu("Image for O"); JMenu backgroundMenu = new JMenu("Set Background"); menuBar.add(menu); menuBar.add(menu2); menuBar.add(menu5); menuBar.add(menu3); menuBar.add(menu4); // Create checkbox menu items for choices final JCheckBoxMenuItem item = new JCheckBoxMenuItem("2 Player"); final JCheckBoxMenuItem item2 = new JCheckBoxMenuItem("VS Computer"); final JCheckBoxMenuItem item3 = new JCheckBoxMenuItem("Play as X"); final JCheckBoxMenuItem item4 = new JCheckBoxMenuItem("Play as O"); final JCheckBoxMenuItem item5 = new JCheckBoxMenuItem("On"); final JCheckBoxMenuItem item6 = new JCheckBoxMenuItem("Off"); JMenuItem item7 = new JMenuItem("Restart"); JMenuItem item8 = new JMenuItem("Exit"); final JCheckBoxMenuItem item9 = new JCheckBoxMenuItem("Easy"); final JCheckBoxMenuItem item10 = new JCheckBoxMenuItem("Hard"); final JCheckBoxMenuItem item11 = new JCheckBoxMenuItem("Impossible"); final JCheckBoxMenuItem itemSword = new JCheckBoxMenuItem("Sword"); final JCheckBoxMenuItem itemSaber = new JCheckBoxMenuItem("Sabers"); final JCheckBoxMenuItem itemSkull = new JCheckBoxMenuItem("Skull"); final JCheckBoxMenuItem itemRing = new JCheckBoxMenuItem("One Ring"); final JCheckBoxMenuItem itemSmiley = new JCheckBoxMenuItem("Red Smiley"); final JCheckBoxMenuItem itemHydra = new JCheckBoxMenuItem("Hydra"); final JCheckBoxMenuItem itemDragon = new JCheckBoxMenuItem("Dragon"); final JCheckBoxMenuItem itemFinalFantasy = new JCheckBoxMenuItem("Final Fantasy 7"); final JCheckBoxMenuItem itemNight = new JCheckBoxMenuItem("Nighttime"); // Set the initial checkboxes to true (for play vs ai, as X, with graphics on) item2.setSelected(true); item3.setSelected(true); item5.setSelected(true); item11.setSelected(true); itemSaber.setSelected(true); itemRing.setSelected(true); itemDragon.setSelected(true); // Play against friend item.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { item.setSelected(true); item2.setSelected(false); multiPlayers = true; repaint(); } }); menu.add(item); // Checkbox for Selecting to play against AI item2.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { item2.setSelected(true); item.setSelected(false); if (currentPlayer == Symbol.X) ai = Symbol.O; else ai = Symbol.X; item3.setSelected(true); item4.setSelected(false); multiPlayers = false; } }); menu.add(item2); // Play as X item3.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // use temp variable to move after assigning symbols if (item4.getState() && !multiPlayers) temp = true; item3.setSelected(true); item4.setSelected(false); currentPlayer = Symbol.X; ai = Symbol.O; if (temp) aiMove(); repaint(); temp = false; } }); menu2.add(item3); // Play as O item4.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (item3.getState() && !multiPlayers) temp = true; item4.setSelected(true); item3.setSelected(false); currentPlayer = Symbol.O; ai = Symbol.X; if (temp) aiMove(); repaint(); temp = false; } }); menu2.add(item4); // Turn Graphics on item5.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { item5.setSelected(true); item6.setSelected(false); useImages = true; repaint(); } }); menu3.add(item5); // Turn Graphics off item6.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { item5.setSelected(false); item6.setSelected(true); useImages = false; repaint(); } }); menu3.add(item6); menu3.addSeparator(); menu3.add(xImageMenu); menu3.add(oImageMenu); menu3.add(backgroundMenu); // Choose Saber for X itemSaber.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { itemSword.setSelected(false); itemSaber.setSelected(true); itemSkull.setSelected(false); imageX = saber; repaint(); } }); xImageMenu.add(itemSaber); // Choose Sword for X itemSword.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { itemSword.setSelected(true); itemSaber.setSelected(false); itemSkull.setSelected(false); imageX = sword; repaint(); } }); xImageMenu.add(itemSword); // Choose Skull for X itemSkull.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { itemSword.setSelected(false); itemSaber.setSelected(false); itemSkull.setSelected(true); imageX = skull; repaint(); } }); xImageMenu.add(itemSkull); // Choose ring for O itemRing.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { itemRing.setSelected(true); itemSmiley.setSelected(false); itemHydra.setSelected(false); imageO = ring; repaint(); } }); oImageMenu.add(itemRing); // Choose red smiley for O itemSmiley.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { itemRing.setSelected(false); itemSmiley.setSelected(true); itemHydra.setSelected(false); imageO = smiley; repaint(); } }); oImageMenu.add(itemSmiley); // Choose hydra for O itemHydra.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { itemRing.setSelected(false); itemSmiley.setSelected(false); itemHydra.setSelected(true); imageO = hydra; repaint(); } }); oImageMenu.add(itemHydra); // Set dragon as background itemDragon.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { itemDragon.setSelected(true); itemFinalFantasy.setSelected(false); itemNight.setSelected(false); background = dragon; repaint(); } }); backgroundMenu.add(itemDragon); // Set final fantasy 7 as background itemFinalFantasy.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { itemDragon.setSelected(false); itemFinalFantasy.setSelected(true); itemNight.setSelected(false); background = finalfantasy; repaint(); } }); backgroundMenu.add(itemFinalFantasy); // Set night as background itemNight.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { itemDragon.setSelected(false); itemFinalFantasy.setSelected(false); itemNight.setSelected(true); background = night; repaint(); } }); backgroundMenu.add(itemNight); // Reset game item7.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { initialize(); } }); menu4.add(item7); // Exit game item8.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); menu4.add(item8); // Difficulty easy item9.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { difficulty = Difficulty.EASY; item9.setSelected(true); item10.setSelected(false); item11.setSelected(false); } }); menu5.add(item9); // Difficulty hard item10.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { difficulty = Difficulty.HARD; item10.setSelected(true); item9.setSelected(false); item11.setSelected(false); } }); menu5.add(item10); // Difficulty impossible item11.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { difficulty = Difficulty.IMPOSSIBLE; item11.setSelected(true); item9.setSelected(false); item10.setSelected(false); } }); menu5.add(item11); // Add Whole menu bar to frame frame.setJMenuBar(menuBar); // Create statusBar (updates will take place in paintComponent) statusBar = new JLabel(" "); statusBar.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 15)); statusBar.setBorder(BorderFactory.createEmptyBorder(2, 5, 4, 5)); // Use container variable to set layout and pack everything nicely Container cp = frame.getContentPane(); cp.setLayout(new BorderLayout()); cp.add(this, BorderLayout.CENTER); cp.add(statusBar, BorderLayout.SOUTH); frame.pack(); board = new Symbol[ROWS][COLS]; initialize(); // Add the mouse listener. Use MouseAdapter rather than implement it this.addMouseListener( new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { int currentCol = e.getX() / cellSize; int currentRow = e.getY() / cellSize; if (gameStatus == GameStatus.CONTINUE) { // If valid click, and spot is empty, make the move. Then check // to see if game is won. If not, and facing AI, ai moves. // Otherwise switch X and O if (currentRow >= 0 && currentRow < ROWS && currentCol >= 0 && currentCol < COLS && board[currentRow][currentCol] == Symbol.EMPTY) { board[currentRow][currentCol] = currentPlayer; checkState(currentPlayer); if (multiPlayers) currentPlayer = (currentPlayer == Symbol.X) ? Symbol.O : Symbol.X; if (gameStatus == GameStatus.CONTINUE && !multiPlayers) aiMove(); } } else { // Game is over, so re-initialize everything initialize(); } repaint(); } }); }