A5 solved guess the number

Write a JFrame application that plays “guess the number” as follows: Your application chooses the number to be guessed by selecting an integer at random in the range 1–1000.

The application uses JLabel to display the following two labels: I have a number between 1 and 1000. Can you guess my number? Please enter your first guess.

A JTextField should be used to input the guess in the application.

A JLabel should display either "Too High" or "Too Low" to help the user zero in on the correct answer. When the user gets the correct answer, "Correct!" should be displayed, and the JTextField used for input should be changed to be uneditable.

A JButton should be provided to allow the user to play the game again. When the JButton is clicked, a new random number should be generated and the input JTextField field changed to be editable.

To distinguish in between different stages of the guessing game we use different background colors and different JLabel messages.

1. Just start the game the background color of JFram window is magenta. The first line of the window contains JLabel messages "I have a number between 1 and 1000.”. The 2nd line contain “Can you guess my number? Enter your first Guess:." JLabel message. A JText filed should be at the beginning of third line and follow by a “Guess result appears here." JLabel, and a “New Game” Jbutton.

2. As each guess is input, the background color should change to either red or orange. Red indicates that the user is getting closer (“warmer”) to the answer and orange indicates that the user is getting farther (“colder”) to the answer. At this stage the number guessed should be displayed at the top of the window. The second line should still display “Can you guess my number? Please enter your first guess.” JLabel message. A JText filed should be at the beginning of third line and follow by a “Too high..” or “To low ..” JLabel message, and a “New Game” Jbutton.

3. When the number is guessed the background will be in green color. The first line of the window displays "After # trials you get the number # correct." JLabel message. The 2nd line displays "You are great! Try again? Click "New Game" button.”. :." A JText filed should be at the beginning of third line and follow by a “Correct." JLabel, and a “New Game” Jbutton.

4. When the “New Game” Jbutton clicked the window will be in yellow color. The first line of the window contains JLabel messages "I have a number between 1 and 1000.”. The 2nd line contain “Can you guess my number? Enter your first Guess:." JLabel message. A JText filed should be at the beginning of third line and follow by a “Guess result appears here." JLabel, and a “New Game” Jbutton.

Create a driver to display the capability of the program designed.

The following are some of the functions and parameters should have in the program. You can create the necessary functions, classes, and parameters for this assignment.

GuessGameFrame.java

GuessGameFrame class extends JFrame. So the GuessGameFrame class has all variables and methods defined in class JFrame.

There are several private data members. Some of their names and data types in my program are:

int lastDistance; // distance between last guess and number
JTextField guessInputJTextField; // for guessing
JLabel prompt1JLabel; // first prompt to user
JLabel prompt2JLabel; // second prompt to user
JLabel messageJLabel; // displays message of game status
JButton newGameJButton; // creates new game
Color background; // background color of application


There are several functions. Some of the important functions in my program are:

public GuessGameFrame function sets up GUI, initializes values and adds action listener .
public theGame function provides a new random number.
public paint function changes JFrame background color.
public react function reacts to a new guess number entered.


GuessHandler class implements ActionListener. It declares the ActionListener interfaces that are to be implemented by the class. It is an inner class to act on user inputs.



GuessGame.java

This class contains the main function which creates a JFrame object, sets up the JFrame’s size, visibility and exit.

You can fine the needed techniques in chapter 1~ 10, 14, 23 and 24. You can also use more advance skills to do this assignment.

This assignment comes with a CISP401V9A5.zip file. It includes four files (GuessGameFrame.class GuessGame.class, GuessGameFrame$1.class and GuessGameFrame$GuessHandler.class.) All of them are executable files. You can place them in a sub folder (directory). Bring up a command prompt and go to the sub directory, type “java GuessGame“ in the sub directory and hit “Enter” key. You should see the program run and with proper enter you should get to the result as the following picture. That should also be expecting result of this assignment.







Please document the files properly and zip your GuessGame.java and GuessGameFrame.java files into a proper named zip file for an assignment (refer to the assignment section of the class syllabus) and submit it to the A5 dropbox of the D2L Website.



Worth 150 points







AD5



Create a drawing programing that allows you create up to 20 drawing windows to draw different size and colors of lines, rectangles, ovals with fill or without.



The drawing program (window) has two menus, file and window. It also has a name (drawing program) and a small java picture.

The file has two options, new and exit. The new is to create a new drawing window called picture. The first one is named picture1 and second one is named picture2 and so on. The exit is to exit the drawing program which will close all the created picture windows. The file, new and exit have keys combination to open and execute the function. To open the file menu holds alt key and presses f key at the same time. To create a new picture window holds alt key and presses f key at the same time and then press alt and n keys at the same time. To exit the drawing program holds alt key and presses f key at the same time and then press alt and e keys at the same time.

The window menu stores the drawing windows created and exist.

The drawing window has minus, rectangle and x boxes on the up right corner of the window to minimize, maximize and close the window. It allows us to use a mouse to adjust to any proper size window and also move and adjust the size of the picture windows.

The created picture window has a small picture and name of the file at the up left edge corner of the window. It also has minimize, maximize and close boxes at the up right edge corner of the window.

It has a drop down menu at the up center portion of the drawing area. It has three options, Rectangle, Line and Oval. A filled check box is on the right side of the drop down box. We can use this option to decide shape of a draw and fill or no fill of the draw.

There are Red, Green and Blue sliders at the bottom of the picture window. There is a box for each slide. It display numbers from 0 ~ 255. You can use slide or type number into the box to adjust the color. When we adjust the slide the number in the box adjusts accordingly. Reverse also hold true.

We use mouse to choose a color, shape and filled options to drag a picture in the picture window.



The needed techniques for this assignment should be in Ch1 ~10, Ch14, Ch23 and Ch24. The drawing components of this assignment are discussed in chapter 11 and Chapter 22. The GUI components from Chapter 14 and Chapter 24 can help you develop this program.

Several suggestions:

Each shape should be stored in an array of MyShape objects, where MyShape is the superclass in your hierarchy of shape classes.
Use a JDesktopPane and JInternalFrames to allow the user to create multiple separate drawings in separate child windows.
Create the user interface as a separate child window containing all the GUI components that allow the user to determine the characteristics of the shape to be drawn. The user can then click in any JInternalFrame to draw the shape.
The following are some of the parameters, classes and functions for your reference. You can add classes, functions and parameters as you feel a need.

DrawFrame.java

DrawFrame class creates the desktop, manages menu items and creates frames. The class extends JFrame. So the DrawFrame class has all variables and methods defined in class JFrame.



There are several private data members:

1. MAX_WINDOWS sets the maximum number of windows.

2. [] frames is a JInternalFrame array to internal frames.

3. [] frameItems is a JMenuItem array to store menu items

4. count counts the number of internal frames created

5. desktop is a JDesktopPane variable to holds internalframes.

6. newItem is a JMenuItem variable to store menu item to create new internal frame

7. windowMenu is a JMenu variable to create window menu to select internal frame

There are couple possible public functions for this class.

DrawFrame function sets up the graphic user interface, menu items, mnemonic keys, action listener, control of the maximum number of the Picture windows, and action perform for this assignment.
frameClosed function controls the remove a particular Picture window and makes sure that the item is removed from the Window menu list.
DrawInternalFrame.java

DrawInternalFrame class creates a frame that allows the user to draw shapes. DrawInternalFrame class extends JInternalFrame. So the DrawInternalFrame class has all variables and methods defined in class JInternalFrame.

There are several private data members:

1. drawPanel is a DrawPanel3 object. It draws panel to create shapes.

2. chooseJComboBox is a JComboBox object. It allows user to select which shape to draw.

3. parent is a DrawFrame object. It is a parent window to controls application.

4. name is a String object. It store the name of a window.



There is a public functions for this class.

DrawInternalFrame function sets up GUI for the Picture windows. It adds action listeners and all the Picture frame components.
DrawPanel3.java

DrawPanel3 class draws the shapes. DrawPanel3 class extends JPanel. So the DrawPanel3 class has all variables and methods defined in class JPanel.

There are several private data members:

1. MAX_SHAPES is an integer number. It sets the maximum number of shapes.

2. [] shape is an array of MyShape objects. It takes MyShapes objects.

3. count is an integer variable. It counts the number of shapes.

4. done is a boolean variable. It tells whether reached maximum number of shapes.

5. fill is a boolean variable. It tells whether to create filled or unfilled shapes.

6. red is an integer variable. It stores amount of red in color.

7. green is an integer variable. It stores amount of green in color.

8. blue is an integer variable. It stores amount of blue in color.

9. type is a String object. It stores type of shape to draw.



There are several public functions for this class

DrawPanel3 function is the constructor. It initializes the drawing panel.
paintComponent function draws colored shapes.
createShape function create a shape at the specified coordinates.
finishShape function determines whether the program reaches the maximum number of shapes.
isDone function determines whether done drawing.
resizeShape function resizes the current shape.
setFill function sets the fill property of the shape.
setShape function changes the shape that is drawn.
setRed function sets red data.
setGreen function sets green value.
setBlue function sets blue value.


DrawProgram.java

DrawProgram class creates the desktop, manages menu items and creates frames.

It contains only the main function. In the main function it creates DrawFrame object, sets the size of the frame, sets the frame visible, and sets the exit component.



MyBounded.java

Abstract MyBounded class sets up the fill for all the shape objects. Abstract MyBounded class extends MyShape. So the MyBounded class has all variables and methods defined in class MyShape.

It has a private boolean filled data member. It controls whether shape is filled or not.

It has several public functions.

MyBounded, no-argument constructor, sets filled to false.
MyBounded, five-argument constructor, sets filled to an object and filled private data member.
MyBounded, six-argument constructor, sets filled to an object and filled private data member.
setAll function sets all the parameters
setFilled function sets filled private data member.
getFilled function returns filled private data member.
getUpperLeftX function returns upper left x.
getUpperLeftY function returns upper left y.
getWidth function returns width.
getHeight function returns height.


MyColorChooser4.java

MyColorChooser4 class is a JPanel subclass. It provides color sliders’ GUI. MyColorChooser4 class extends JPanel. So the MyColorChooser4 class has all variables and methods defined in class JPanel.

It has several private data members:

1. redJSlider is a JSlider object. It allows user to use slider to choose red value.

2. blueJSlider is a JSlider object. It allows user to use slider to choose blue value.

3. greenJSlider is a JSlider object. It allows user to use slider to choose green value.

4. redJTextField is a JTextField object. It displays red value or allows enter red value.

5. blueJTextField is a JTextField object. It displays blue value or allows enter blue value.

6. greenJTextField is a JTextField object. It displays green value or allows enter green value.

7. redJLabel is a JLabel object. It displays the red color label.

8. blueJLabel is a JLabel object. It displays the blue color label.

9. greenJLabel is a JLabel object. It display the green color label.

10. color is a Color object. It stores color.



It has several public member functions:

MyColorChooser4 is the constructor to set up the GUI for sliders, adds change listener to the sliders, and action listener to textfileds.
setColor function sets slider and text field values.
getColor function returns color.
getRedSlider function returns red slider information.
getGreenSlider function returns green slider information.
getBlueSlider function returns blue slider information.
getRedDisplay function returns red textfield information.
getGreenDisplay function returns green textfield information.
getBlueDisplay function returns blue textfield information.


ChangeHandler class

ChangeHandler class is a private inner class to handle slider events. The ChangeHandler class implements ChangeListener. It declares the ChangeListener interfaces that are to be implemented by the class.

The only member functions for this class is stated here.

stateChanged function handles change in slider value.


ActionHandler class

ActionHandler class is a private inner class to handle action events. The ActionHandler class implements ActionListener. It declares the ActionListener interfaces that are to be implemented by the class.

The only member function for this class is actionPerformed. We have a definition here.

actionPerformed function handles text field input and adjusts the slider position.


MyLine.java

MyLine class provides function to handle line drawing. The MyLine class extends MyShape. So the MyLine class has all variables and methods defined in class MyShape.

There are several public member functions:

MyLine no-argument constructor passes the information to its super class.
MyLine four-argument constructor passes the information to its super class.
MyLine five-argument constructor passes the information to its super class.
Draw function draws lines.
toString function returns “Line”.
MyOval.java

MyOval class provides function to handle oval drawing. The MyOval class extends MyBounded. So the MyOval class has all variables and methods defined in class MyBounded.

There are several public member functions:

MyOval no-argument constructor passes the information to its super class.
MyOval five-argument constructor passes the information to its super class.
MyOval six-argument constructor passes the information to its super class.
Draw function draws ovals.
toString function returns “Oval”.


MyRectangle.java

MyRectangle class provides function to handle oval drawing. The MyRectangle class extends MyBounded. So the MyRectangle class has all variables and methods defined in class MyBounded.

There are several public member functions:

MyRectangle no-argument constructor passes the information to its super class.
MyRectangle five-argument constructor passes the information to its super class.
MyRectangle six-argument constructor passes the information to its super class.
Draw function draws rectangles.
toString function returns “Rectangle”.


MyShape.java

MyShape class.is an abstract. It implements Serializable. So the MyShape class has all variables and methods defined in class Serializable.

There are 5 private variables.

x1 is an integer variable. It is the first x-coordinate.
x2 is an integer variable. It is the second x-coordinate.
y1 is an integer variable. It is the first y-coordinate.
y2 is an integer variable. It is the second x-coordinate.
Color is a Color object. It stores a Color object information.
There are several public member functions:

MyShape no-argument constructor initializes 0s to the class private data members.
MyShape four-argument constructor initializes to the class private data members.
MyShape five-argument constructor initializes to the class private data members.
setAll function changes all the properties at once after the shape has been created.
setX1 function sets X1 coordinate.
getX1 function returns X1 coordinate.
setY1 function sets Y1 coordinate.
getY1 function returns Y1 coordinate.
setX2 function sets X2 coordinate.
getX2 function returns X2 coordinate.
setY2 function sets Y2 coordinate.
getY2 function returns Y2 coordinate.
setColor function sets data to the Color object private data member.
getColor function return data of the Color object private data member.
An abstract draw do nothing function.


ShapePanel.java

ShapePanel class allows the user to choose a shape and fill setting. It extends JPanel. So the ShapePanel class has all variables and methods defined in class JPanel.

It has three private data members.

[] shapeList is a string array to store the shapes’ names (Rectangle, Line and Oval).
chooseJComboBox is a JComboBox object variable.
filledJCheckBox is a JCheckBox object variable.
There are several public member functions.

ShapePanel no-argument constructor initializes the private data members.
getFill function returns filledJCheckBox information.
getChooser function returns chooseJComboBox information.


Please review the textbook material and understand then. You can also use more advance skills to do this assignment.

This assignment comes with a CISP401V9AD5.zip file. It includes 28 .class files. All of these files are executable files. You can place them in a sub folder (directory). Bring up a command prompt and go to the sub directory, type “java DrawProgram “ in the sub directory and hit “Enter” key. You should see the program run and be able to get to the result as the following picture.




Powered by