Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Object Oriented Programming using GUI components. With comments and explanaition please. Also, please provide answer only if your sure, read and do all of the
Object Oriented Programming using GUI components.
With comments and explanaition please.
Also, please provide answer only if your sure, read and do all of the requirments of the exercise.
i want the solution based on the Exercise needs and not different methods etc.
also the png files i have them and use them my self.
Thank you in advance!
Exercise 2 hangmangui. HangmanApplet DOX Into E.11 Scenario: You have been assigned to write a Hangman game. Specifically, you will write the graphical user interface for the game and all the operations that are required. The game's underlying model has already been written and will be provided to you. obdurate You won with 3 guesses left! abcdefghijklmnopqrstu Game Behavior: Hangman is a simple game where a player attempts to guess a secret word by picking letters from the alphabet. Initially, the secret word appears as a series of ? (question marks), one for each letter in the secret word. If the player guesses a letter that occurs in the secret word, all occurrences of that letter will appear, changing from question marks into the correct letter. If the player guesses a letter that does not occur in the secret word, the player uses up one guess, and a portion of the hangman's body appears. If the player correctly guesses all the letters in the secret word before running out of guesses, he/she wins the game. If all guesses are used up, the hangman's entire body appears and the player loses the game. Each time the game begins, a random word is chosen. For simplicity, the secret word is one of the following ten words: accretion, boisterous, dullard, feigned, haughty, insipid, noisome, obdurate, parsimonious, sycophant Typing letter keys guesses letters. The state of the hangman, word being guessed, number of guesses left, and letters ouessed are all visible on screen Below are some screen shots of the window in various states: 0X Harga X ??????? 6 guesses left abcdefghijklmnopqrstuvwxY ????? 3 guesses left abcdefghijklmnopetuvwxY Nem New Hangman OX ????ha?? You lost! (sycophant) defghijklmnopaustuvwxyz Merwe Figure 1: Screen shots of the window in various states GUI and Drawing Panel: The main GUI should have the following properties: The frame's size should be set to 450 x 350 pixels. The frame should contain a drawing panel in its center that occupies the majority of the window. The south of the window should contain a panel with a "New button. When the New button is clicked, the game should be restarted and a new game should begin. The frame should grab keyboard focus when it is started, so that key presses will correctly go to the frame instead of other GUI components. Do this by calling the frame's requestFocus() method when it is started. When the user types a letter key (lowercase 'a' through 'z'), the game should guess that letter. The guess may change the state of the model, and if so, the drawing panel should be repainted. If the user presses a key other than a lowercase letter no action should be taken . The game's drawing panel should have the following properties: Its background color should be white. An image should appear on the panel at all times to indicate the current hang man's status. The image to use should reflect how many guesses the player has used up. That is, if the player has made O guesses, the file hangman0.gif is the image to use; if the player has used 4 guesses, hangman4.gif is the image to use; and so on. You are guaranteed that for any valid number of guesses n in the model, an image file hangmann.gif exists. Page 7/11 The image should be located 10 pixels from the top of the panel, centered horizontally in the panel regardless of the panel's size. You may assume that the images are all the same dimensions and that they will fit into the window, but you should not hard-code the image dimensions into your code. The image should always be up-to-date with the game's current state; your image should not lag behind the game, nor should it be necessary to resize or obscure the window to refresh the current image. Three strings of text should also appear at all times on the panel. All text on the panel should be drawn in Monospaced font, bold, size 18. Unless otherwise specified, text appears in black. The text should also centered horizontally, described below. be as The first is the current word being guessed at, showing the letters that the player has tried and ? (question marks) for all letters that have not been correctly guessed. This string should be 60 pixels from the bottom of the panel, centered horizontally. (Hint: Use the stringWidth(String) method from a FontMetrics object to discover how wide a string will be when drawn on screen. To retrieve the font metrics for a panel, call the getFontMetrics0 method on its Graphics context.) The second string of text is a display of the current game status. This string should be 40 pixels from The second string of text is a display of the current game status. This string should be 40 pixels from the bottom of the panel, centered horizontally. If the game is in progress, the string will show the number of guesses the player has remaining. For example, if the player has 2 guesses left, the string will be 2 guesses left. If the game has been won, the string should display this information and the number of guesses left. For example, if the player wins the game with 3 guesses remaining, the string will be "You won with 3 guesses left!. If the game has been lost, the string should display this information as well as what the word really was. For example, if the player runs out of guesses while trying to guess the word banana, the string will be "You lost! (banana). The third string of text (more accurately, many individually drawn characters of text) is a list of all the letters of the alphabet, in lower case, each colored to indicate whether the player has guessed it or not. If a letter has been guessed, it should be colored in blue; if not, it should be colored in light gray. This "string" should be 20 pixels from the bottom of the panel, centered horizontally. Since each letter is potentially a different color, each letter must be drawn individually, rather than drawing all of the letters as a combined string. This makes it harder to find the overall width of the "string" and therefore harder to center it. Assume that each letter occupies a 14-pixel wide region; draw the letters 14 pixels apart, and assume that the width of the alphabet string is (26 letters * 14 pixels) = 364 pixels total. Use this overall width to center the third line of text. Page 8/11 Resources and Model: Pre-written files are: hangman*.gif (image files for use with the drawing panel) Note: the image files are located on the server/moodle courses' material https://courses unic.ac.cy/ As well as the images required. Note: If there are difficulties in downloading the files please refer to the lecturer. In order to have an organized code you should create the following classes: HangmanGUI.java which will contain the GUI of the Hangman class (the class that will contain the Action Listeners and Actions performed) Hangman.Model.java which will contain the model of the Hangman game playing Hangman Main.java which will contain the call of the instant classes as follows: // This program provides a graphical hangman game. public class Hangman Main { public static void main(String[args) { Hangman GUI gui = new HangmanGUI(); } } The following is a partial list of Java packages and classes may help you in writing this project: package java.awt: Color, Container, FlowLayout, Font, FontMetrics, Graphics, Graphics2D, Image, MediaTracker, Toolkit package java.awt.event: ActionEvent, ActionListener, KeyAdapter, KeyEvent, KeyListener package java.util: Observable, Observer package javax.swing: JFrame, JButton, JPanel Style and Design: You should exercise good object-oriented design in your program. Encapsulate state and behavior intelligently into classes, and represent related reused code intelligently in methods. Separate event listeners and observers from GUI components by putting them in separate classes (they can be inner classes if desired). You should follow good style guidelines as described in your previous projects, such as encapsulation and access, comments, indentation, spacing, and identifier names, and argument checking. Comment using the Javadoc commenting style, with a comment block on each public class and method; description summaries, as well as use of tags @author, @param, and @return, are required where appropriate. Make constant strings, numbers and names into static final constants
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started