130 likes | 142 Views
Create and solve crossword puzzles using Java's JApplet, JPanels, and JComboBoxes. Load words and clues from a text file and challenge yourself with this interactive word game!
E N D
CrossWord Puzzle By Keith Lynn Field Trip #31
JApplet • A JApplet is a top-level container in Java • We will use the JApplet to contain two Jpanels • The first JPanel will contain letters • The second JPanel will contain words • In order to load a JApplet, we create an HTML document • The HTML document must contain an applet tag • The applet tag must contain the applet's name, width, and height
Appletviewer • An alternative to using a browser is to use the appletviewer program that is included in the JDK • In order to use the appletviewer program, we create an HTML document where we include an applet tag • The applet tag must contain the name of the class, the width and height
LayoutManager • A LayoutManager defines how components will be placed onto a screen • There are built-in layout managers in Java • FlowLayout will put components in a row • GridLayout will put components in a grid • In this app, we will create a Jpanel to hold letters in buttons and put the buttons in a grid
String • A String is a collection of individual characters • We can obtain the character at a certain position in a String with the method charAt(int) • The first character in a String is at position 0 • We can determine the length of the String with the length() method • We can concatenate characters to String with the += operator • We can compare the content of two Strings with the equals or equalsIgnoreCase methods
Characters • A character is a single character • There is a special relationship between chars and ints in Java • If you treat a char like an int, it is converted to its ASCII equivalent
JComboBox • A JComboBox is a selection list • It will contain a list of items • In this app, we will create many JComboBoxes that contain the letters of the alphabet
JButton • A JButton is a component that can be placed on a container • The JButton can contain text or an image • In this app, we will provide the user with button to give up
Events • We can detect events and act on them by creating an event listener • An event listener we can use with a button is the ActionListener • ActionListener is an interface which contains the method actionPerformed • If an ActionListener is registered with a button, then when the button is clicked, actionPerformed is called
Events, cont'd • The event listener we will use with the JComboBoxes is the ItemListener • The ItemListener interface contains the method itemStateChanged • We can determine whether the state change was ItemEvent.SELECTED or ItemEvent.DESELECTED by using the method getStateChange • Note that selecting an item in a JComboBox causes both a deselection and a selection event
Arrays • Arrays are a collection of elements of the same type • In our app, we will use a 2 dimensional array • Java doesn't directly support 2 dimensional arrays • But we can create an array of arrays • We write a 2 dimensional array of 20 x 20 containing JcomboBoxes • However, we will black out some boxes
Filling in Words • We choose beforehand to black out certain boxes leaving only 26 positions to be filled with words • Some of those words overlap • We will load from a text file a list of words and definitions and choose 26 words randomly • We make sure that we choose words that have the correct letters if the words overlap
The Game • After the list of words and clues are loaded and we have chosen the words, we will display the clue for the word in a JComboBox • The JComboBoxes corresponding to the word that goes with the clue will be highlighted in red • If the user correctly chooses the letters, then the JComboBoxes are highlighted in green • If the user gives up, then all of the words are displayed and all JComboBoxes that contains letters of words are highlighted in green