Write a GUI tic-tac-toe game that can run as either a windowed application or as an applet.
Text: Java Tutorial: Swing
Concepts: GUIs.
Construct a GUI for tic-tac-toe. It should contain at least a 3x3 tic-tac-toe grid of buttons. These buttons can start blank or with some other symbol that shows they are "empty" squares. When the player clicks on one, it should change to an X (or O). You should then disable the clicked button or otherwise prevent the player from changing a button that already shows a move.
After the player enters a move, you should have the computer change one of the empty squares to a O. You can just choose an empty square randomly, or, for extra credit, you can use a more intelligent strategy.
The game should end when either someone wins (3 of the same characters in a row) or there are no more empty squares. You should report the game outcome to the user. Once the user has had a chance to read that message, you can start a new game or provide some means (such as a Replay or Reset button) for the user to do so.
These are only the basic requirements. You are encouraged to go beyond this. A few ideas include:
You can earn up to +15 points for particularly nice GUI improvements and options, and up to (another) 15 additional points for a smart opponent AI.
You may write additional classes as needed, but your main executable/JApplet class should be UsernameA08
.
Your main class should also be a JApplet that runs embedded in a webpage. (It is possible for applets to popup a JFrame window, but don't do that. Instead, keep the applet content in the webpage itself.) Create the necessary HTML page. This page should have additional info too, such as a description of the game and a link to the associated documentation.
Make sure all of your methods are documented. In the overview documentation for your main class, describe/list any extra credit enhancements you made or any existing bugs or problems. Also, you must include the full URL in the documentation to a working version of your applet on www2.hawaii.edu.
Run the javadoc tool to extract your documentation into HTML pages. Upload all of these pages to www2.hawaii.edu, preferably in a single subdirectory. As mentioned, your applet page should contain a link to the index.html page of this extracted documentation.
The following shows an example of only the basic requirements. I had to resize the window to make it look like this. I used the setEnabled method on the buttons to disable buttons that should not be clickable. I disable all the buttons as soon as someone wins so the user can't continue the game. Also at the end of the game, I enable the Play Again button. Clicking that returns the game to the initial state shown on the left.
You will upload your source code to Tamarin, but it will only check that everything compiles. Anthony will then grade your submission manually.
Create a .jar including all your .java files and your applet's .html file. You should not include the extracted documentation pages. Upload your UsernameA08.jar
file to Tamarin.
Remember to follow the course coding standards on all assignments.
Caused by: java.security.AccessControlException: access denied ("java.io.FilePermission" "myimage.png" "read")
This suggests that this is a unix file permissions problem that can be fixed with the chmod
command. This is not actually the case.
What's actually happening here is that the .class file has been downloaded from the server and is running in your browser on your local (client) machine. Your applet is then trying to access some file--myimage.png, in this case. But your applet is looking for that file in the same directory as itself... but, since it's now running on the user's machine, it's trying to find some file in its local directory there. But, due to the Java security sandbox, an applet does not have permission to read files on the user's machine! Hence, the exception.
This is not a problem when you run the applet directly from your local machine because it is able to access files in the same or child directories of where it came from, and both the applet and the image are then on the same computer.
There are two solutions I can suggest to fix this problem.
The first is the quick-and-dirty fix: use a URL for you images. For example, you may have had something like this in your code:
ImageIcon icon = new ImageIcon("myimage.png");
Instead, you'll need:
//location of your applet's html and .class file. Note the final / String site = "http://www2.hawaii.edu/~username/ics211/A08/"; try { ImageIcon icon = new ImageIcon(new java.net.URL(site + "myimage.png")); //use icon... }catch (java.net.MalformedURLException e) { System.out.println("URL malformed"); }
Note that you have to catch a checked excpetion whenever you create a URL object. You can expand this try block to include all of the URLs you create.
This is a quick-and-dirty approach because you have to specify the URL of the final location of your applet. You'd have to recompile it every time you move it. And now the applet won't run locally. Of course, there are ways to clean this up a bit by using some of JApplet/Applet's methods--though this assumes the class that loads the images extends JApplet, which may not be true if you separated your GUI into other classes. See getCodeBase() or getAppletContext() so that the URLs are relative to wherever the applet came from. (More here).
The other (better) approach is to bundle the images with your .class file in a jar file. This can be an executable jar or not. You can then use the getResource() method, which is in the class Class
Example:
//assuming myimage.png is in the root of the jar file with all your .class files ImageIcon icon = new ImageIcon(this.getClass().getResource("myimage.png"));
Note that your code will only worked now when its packaged in the jar file. So everytime you recompile your code, you'll have to recreate your jar too. Also, you'll need to specify in your HTML file that your applet class is now in a jar, like so:
<applet code="UsernameA08.class" archive="UsernameA08.jar" width="200" height="180">
Good luck!