private void _displayRespStrInFrame() { final JFrame frame = new JFrame("Google Static Map - Error"); GUIUtils.setAppIcon(frame, "69.png"); // frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); JTextArea response = new JTextArea(_respStr, 25, 80); response.addMouseListener( new MouseListener() { public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) { /*frame.dispose();*/ } public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }); frame.setContentPane(new JScrollPane(response)); frame.pack(); GUIUtils.centerOnScreen(frame); frame.setVisible(true); }
private void initGUI() { JPanel pCommand = new JPanel(); pResult = new JPanel(); nsSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, pCommand, pResult); pCommand.setLayout(new BorderLayout()); pResult.setLayout(new BorderLayout()); Font fFont = new Font("Dialog", Font.PLAIN, 12); txtCommand = new JTextArea(5, 40); txtCommand.setMargin(new Insets(5, 5, 5, 5)); txtCommand.addKeyListener(this); txtCommandScroll = new JScrollPane(txtCommand); txtResult = new JTextArea(20, 40); txtResult.setMargin(new Insets(5, 5, 5, 5)); txtResultScroll = new JScrollPane(txtResult); txtCommand.setFont(fFont); txtResult.setFont(new Font("Courier", Font.PLAIN, 12)); /* // button replaced by toolbar butExecute = new JButton("Execute"); butExecute.addActionListener(this); pCommand.add(butExecute, BorderLayout.EAST); */ pCommand.add(txtCommandScroll, BorderLayout.CENTER); gResult = new GridSwing(); gResultTable = new JTable(gResult); gScrollPane = new JScrollPane(gResultTable); // getContentPane().setLayout(new BorderLayout()); pResult.add(gScrollPane, BorderLayout.CENTER); // Set up the tree rootNode = new DefaultMutableTreeNode("Connection"); treeModel = new DefaultTreeModel(rootNode); tTree = new JTree(treeModel); tScrollPane = new JScrollPane(tTree); tScrollPane.setPreferredSize(new Dimension(120, 400)); tScrollPane.setMinimumSize(new Dimension(70, 100)); txtCommandScroll.setPreferredSize(new Dimension(360, 100)); txtCommandScroll.setMinimumSize(new Dimension(180, 100)); gScrollPane.setPreferredSize(new Dimension(460, 300)); ewSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tScrollPane, nsSplitPane); fMain.getContentPane().add(ewSplitPane, BorderLayout.CENTER); doLayout(); fMain.pack(); }
public static void main(String[] args) { MojamComponent mc = new MojamComponent(); JFrame frame = new JFrame(); JPanel panel = new JPanel(new BorderLayout()); panel.add(mc); frame.setContentPane(panel); frame.pack(); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); mc.start(); }
public static void main(String s[]) { // Getting save directory String saveDir; if (s.length > 0) { saveDir = s[0]; } else { saveDir = JOptionPane.showInputDialog( null, "Please enter directory where " + "the images is/will be saved\n\n" + "Also possible to specifiy as argument 1 when " + "running this program.", "l:\\webcamtest"); } String layout = ""; if (s.length > 1) { layout = s[1]; } // Move mouse to the point 5000,5000 px (out of the screen) Robot rob; try { rob = new Robot(); rob.setAutoDelay(500); // 0,5 s rob.mouseMove(5000, 5000); } catch (AWTException e) { e.printStackTrace(); } // Make the main window JFrame frame = new JFrame(); frame.setAlwaysOnTop(true); frame.setTitle( "Webcam capture and imagefading - " + "Vitenfabrikken Jærmuseet - " + "made by Hallvard Nygård - " + "Vitenfabrikken.no / Jaermuseet.no"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setUndecorated(true); WebcamCaptureAndFadePanel panel = new WebcamCaptureAndFadePanel(saveDir, layout); frame.getContentPane().add(panel); frame.addKeyListener(panel); frame.pack(); frame.setVisible(true); }
public static void createAndShowGUI() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } JFrame frame = new JFrame("@title@"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().add(new MainPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); }
/** Creates the JFrame and sets everything up */ private void createWindow() { // create the picture frame and initialize it createAndInitPictureFrame(); // set up the menu bar setUpMenuBar(); // create the information panel createInfoPanel(); // creates the scrollpane for the picture createAndInitScrollingImage(); // show the picture in the frame at the size it needs to be pictureFrame.pack(); pictureFrame.setVisible(true); }
public static void main(String[] args) { Global.debugging = true; // make frame final JFrame frame = new JFrame("Test Client Game"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // make game ClientGame game = null; String[] names = { "Alpha", "Beta", "Charlie", "Delta", "Epsilon", "Foxtrot", "Gamma", "Hurricane" }; int ptr = 0; while (true) { try { game = new ClientGame(names[ptr++], (args.length > 1 ? args[1] : "localhost")); break; } catch (ClientNameException ex) { System.err.println(ex); } catch (IOException ex) { System.err.println("Could not connect to server"); } } // add controls to frame game.getPlayerShip().getControls().setContainer(frame); // add full screen toggle FullScreenToggle.addToggleToGame(frame, game, KeyEvent.VK_F11); // add game to frame frame.add(game); // show frame frame.pack(); frame.setVisible(true); game.startTheGame(); }
public static void main(String s[]) { if (s.length > 0) j2kfilename = s[0]; else j2kfilename = "girl"; System.out.println(j2kfilename); isApplet = false; JFrame f = new JFrame("ImageViewer"); f.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); JApplet applet = new ImageViewer(); f.getContentPane().add("Center", applet); applet.init(); f.pack(); f.setSize(new Dimension(550, 550)); f.show(); }
// init private static void init() { if (frame != null) frame.setVisible(false); frame = new JFrame(); offscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); onscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); offscreen = offscreenImage.createGraphics(); onscreen = onscreenImage.createGraphics(); setXscale(); setYscale(); offscreen.setColor(DEFAULT_CLEAR_COLOR); offscreen.fillRect(0, 0, width, height); setPenColor(); setPenRadius(); setFont(); clear(); // add anti-aliasing RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); offscreen.addRenderingHints(hints); // frame stuff ImageIcon icon = new ImageIcon(onscreenImage); JLabel draw = new JLabel(icon); draw.addMouseListener(std); draw.addMouseMotionListener(std); frame.setContentPane(draw); frame.addKeyListener(std); // JLabel cannot get keyboard focus frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // closes all windows // frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // closes only current window frame.setTitle("Standard Draw"); frame.setJMenuBar(createMenuBar()); frame.pack(); frame.requestFocusInWindow(); frame.setVisible(true); }
public static void createAndShowGUI() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } JFrame frame = new JFrame("@title@"); frame.setIconImages( Arrays.asList( makeBufferedImage(new StarIcon(), 16, 16), makeBufferedImage(new StarIcon(16, 8, 5), 40, 40))); // frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.getContentPane().add(new MainPanel(frame)); frame.setResizable(false); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); }
/** @param args */ public static void main(String[] args) { JPanel boxPane = new JPanel(new BorderLayout()); // boxPane.setLayout(new BoxLayout(boxPane, BoxLayout.LINE_AXIS)); // boxPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); // boxPane.add(Box.createVerticalGlue()); Example1 ex = new Example1(); // boxPane.add(ex); boxPane.add(Box.createRigidArea(new Dimension(10, 0))); Button btnStartStop = new Button("Start / Stop"); btnStartStop.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent event) { log.info("Suspending / Resuming the simulation thread, current state = " + helper.stop); if (helper.stop == true) helper.unpause(); else helper.pause(); } }); // btnStartStop.setMaximumSize(new Dimension(100, 100)); boxPane.add(btnStartStop, BorderLayout.PAGE_START); JScrollPane worldScrollPane = new JScrollPane(ex); final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, worldScrollPane, boxPane); splitPane.setResizeWeight(0.5); splitPane.setOneTouchExpandable(true); splitPane.setContinuousLayout(true); worldScrollPane.addAncestorListener( new AncestorListener() { @Override public void ancestorRemoved(AncestorEvent arg0) { splitPane.repaint(); } @Override public void ancestorMoved(AncestorEvent arg0) { splitPane.repaint(); } @Override public void ancestorAdded(AncestorEvent arg0) { splitPane.repaint(); } }); JFrame f = new JFrame(); f.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.add(splitPane, BorderLayout.CENTER); // f.add(boxPane); f.pack(); f.setVisible(true); helper = new SimulationHelper(ex.getGraphics()); helper.start(); // layeredPane.setBorder(BorderFactory.createTitledBorder("Move the Mouse to Move Duke")); }
public static void main(String[] args) { // read filename and N 2 parameters String fileName = args[0]; N = Integer.parseInt(args[1]); // output two images, one original image at left, the other result image at right BufferedImage imgOriginal = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB); BufferedImage img = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB); try { File file = new File(fileName); InputStream is = new FileInputStream(file); long len = file.length(); byte[] bytes = new byte[(int) len]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } int ind = 0; for (int y = 0; y < IMAGE_HEIGHT; y++) { for (int x = 0; x < IMAGE_WIDTH; x++) { // for reading .raw image to show as a rgb image byte r = bytes[ind]; byte g = bytes[ind]; byte b = bytes[ind]; // set pixel for display original image int pixOriginal = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); imgOriginal.setRGB(x, y, pixOriginal); ind++; } } is.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } int[] vectorSpace = calculateVectorSpace(imgOriginal); // quantization for (int y = 0; y < IMAGE_HEIGHT; y++) { for (int x = 0; x < IMAGE_WIDTH; x++) { int clusterId = vectorSpace[IMAGE_WIDTH * y + x]; img.setRGB(x, y, clusters[clusterId].getPixel()); } } // Use a panel and label to display the image JPanel panel = new JPanel(); panel.add(new JLabel(new ImageIcon(imgOriginal))); panel.add(new JLabel(new ImageIcon(img))); JFrame frame = new JFrame("Display images"); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
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); }
private void _displayImgInFrame() { final JFrame frame = new JFrame("Google Static Map"); GUIUtils.setAppIcon(frame, "71.png"); // frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); JLabel imgLbl = new JLabel(new ImageIcon(_img)); imgLbl.setToolTipText( MessageFormat.format( "<html>Image downloaded from URI<br>size: w={0}, h={1}</html>", _img.getWidth(), _img.getHeight())); GUIUtils.centerOnScreen(frame); frame.setVisible(true); frame.setContentPane(imgLbl); frame.pack(); frame.setResizable(false); imgLbl.addMouseListener( new MouseListener() { public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) { System.out.println("Mouse Listener: Mouse Clicked!"); mapIsUp = 1; sentX = 0.00; clickX = e.getX(); // Latitude clickY = e.getY(); // Longitude if ((clickX < (_img.getWidth() / 2)) && (clickY < (_img.getHeight() / 2))) { // 1st quadrant positive values sentX = Double.parseDouble(ttfLati.getText()) + (((-1 * (_img.getWidth() / 2)) + clickX) * pixelX); // Add to latitude sentY = Double.parseDouble(ttfLongi.getText()) + (((_img.getHeight() / 2) - clickY) * pixelY); // Add to Longitude System.out.println("Top left"); } else if ((clickX > (_img.getWidth() / 2)) && (clickY > (_img.getHeight() / 2))) { // 2nd quadrant negative values sentX = Double.parseDouble(ttfLati.getText()) + (((-1 * (_img.getHeight() / 2)) + clickX) * pixelX); sentY = Double.parseDouble(ttfLongi.getText()) + (((_img.getHeight() / 2) - clickY) * pixelY); System.out.println("Bottom Right"); } else if ((clickX < (_img.getWidth() / 2)) && (clickY > (_img.getHeight() / 2))) { // 3rd quadrant 1 positive 1 negative sentX = Double.parseDouble(ttfLati.getText()) + (((-1 * (_img.getWidth() / 2)) + clickX) * pixelX); sentY = Double.parseDouble(ttfLongi.getText()) + (((_img.getHeight() / 2) - clickY) * pixelY); System.out.println("Bottom Left"); } else { // 3rd quadrant 1 positive 1 negative sentX = Double.parseDouble(ttfLati.getText()) + (((-1 * (_img.getHeight() / 2)) + clickX) * pixelX); sentY = Double.parseDouble(ttfLongi.getText()) + (((_img.getHeight() / 2) - clickY) * pixelY); System.out.println("Top Right"); } BigDecimal toCoordsX = new BigDecimal(sentX); BigDecimal toCoordsY = new BigDecimal(sentY); sentX = (toCoordsX.setScale(6, BigDecimal.ROUND_HALF_UP)) .doubleValue(); // allows values of up to 6 decimal places sentY = (toCoordsY.setScale(6, BigDecimal.ROUND_HALF_UP)).doubleValue(); getCoords = sentX + " " + sentY; ttfLati.setText(Double.toString(sentX)); ttfLongi.setText(Double.toString(sentY)); System.out.println("... saving Coordinates"); saveLocation( getCoords); // pass getCoords through saveLocation. this string is appended to the // savedLocations file. System.out.println("... savedCoordinates"); // Update the Locations ComboBox with new additions ttfSave.removeAllItems(); // re-populate the ComboBox System.out.println("removed items"); getSavedLocations(); // run through file to get all locations for (int i = 0; i < loc.size(); i++) ttfSave.addItem(loc.get(i)); System.out.println("update combobox"); mapIsUp = 0; frame.dispose(); // closes window startTaskAction(); // pops up a new window } public void saveLocation(String xy) { BufferedWriter f = null; // created a bufferedWriter object try { f = new BufferedWriter( new FileWriter( "savedLocations.txt", true)); // evaluated true if file has not been created yet f.write(xy); // append passed coordinates and append to file if exists f.newLine(); f.flush(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { // close the file if (f != null) { try { f.close(); } catch (IOException e) { // any error, catch exception System.err.println("Error: " + e.getMessage()); } } } } public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }); }
/** 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(); } }); }