130 likes | 143 Views
Hangman By Keith Lynn. Field Trip #23. JApplet. A JApplet is a top-level container An applet is a small Java program that is executed by another program such as a browser
E N D
Hangman By Keith Lynn Field Trip #23
JApplet • A JApplet is a top-level container • An applet is a small Java program that is executed by another program such as a browser • JApplet is updated version of applet that allow us to use lightweight components that don't depend heavily on their environment • This means that components should look the same no matter what environment they are on
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
JButton • A JButton is a graphical component • We can place text on a JButton • A JButton has a margin so some characters won't be displayed • We can change the margin by obtain an Insets object and setting left and right to 0 and then applying it to the button
Graphics • In order to display things like graphics, we will override either the paint or paintComponent method • If we are directly drawing on a JApplet, then we override its paint method • We can also create an instance of a JPanel and add this to the JApplet • In the JPanel, we override the paintComponent method
LayoutManager • We can specify how components are added to a Japplet or Jpanel by specifying a layout manager • There are several built-in layout managers • FlowLayout lays out components in a row • GridLayout lays out components in a grid
String • A String is a collection of characters • We can retrieve the character at a certain location in a String with the method charAt(int) • We can append characters to the String using the + symbol • We can compare two Strings using the method equals or equalsIgnoreCase
Char • A char is a single character • There is a special relationship between chars and ints in Java • If you treat a char as an int, it is converted to the ASCII value of the char • The ASCII value of 'A' is 65
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
Drawing Lines • In order to draw lines and shapes, we need a Graphics object • We get a reference to a Graphics object by overriding paint in a heavyweight component and paintComponent in a lightweight component • A Jpanel is a lightweight component • We the method drawLine(x0,y0,x1,y1) to draw a line between (x0,y0) and (x1,y1)
Sounds • In order to play a sound in an applet, we use the method getAudioClip • The first parameter to getAudioClip is a URL • This can be a URL on the web or getCodeBase() to refer to the location of the .class file of the applet or getDocumentBase() to refer to the location of the .html file of the applet • We play the audio clip with the method play
Hangman • The plan for creating Hangman is to create three panels • The first panel will contain buttons that represent the characters in the word • The second panel will contain buttons indicating the characters in the alphabet that haven't been chosen • The third panel will be used to draw the hangman
Hangman, cont'd • We attach a listener to the buttons representing the alphabet • Each time a letter is clicked, we search the word and display occurrences of the letter • We then make the letter invisible in the alphabet • If the letter didn't occur, we display another part of the hangman • If the word is filled in the game is over and the user wins • If the hangman is filled in and the word isn't, the user loses