3:30 PM 7/23/2009 Yoon Mo, Nathan, Alex, Sushmitha, Vikas, Neethu, Rajeswari, Leo, Daniel, Mohammed, Gang, Dean, Scot, Nick, Francis, Margaret Things to accomplish today: a) finish the interactive applet with double-buffering, post it b) work out an implementation for some problem in an object-oriented way c) introduce servlets and JSPs, including JDBC access to MySQL Code developed yesterday is available on-line. http://www.cs.indiana.edu/classes/a202-dger/sum2009/exercise0722.txt Copy the code into a file One.java then compile it and then call appletviewer on it. Things work well, though there is a bug in clicking the mouse outside all circles. Let's first fix the flicker. We will remove the flicker for those who experience it. Daniel has no flicker on his computer. Stay tuned for updates. The flicker is not the issue. The way we fix it if it happens is what we want to discuss. Let's create a new file NoFlickerApplet.java with this contents: import java.applet.*; import java.awt.*; class NoFlickerApplet extends Applet { private Image offScreenImage; private Graphics offScreenGraphics; private Dimension offScreenSize; public final void update(Graphics theGraphicsContext){ Dimension dim = this.getSize(); /* size() originally... */ if( (offScreenImage == null) || (dim.width != offScreenSize.width) || (dim.height != offScreenSize.height)) { this.offScreenImage = this.createImage(dim.width, dim.height); this.offScreenSize = dim; this.offScreenGraphics = this.offScreenImage.getGraphics(); } this.offScreenGraphics.clearRect(0, 0, this.offScreenSize.width, this.offScreenSize.height); this.paint(offScreenGraphics); theGraphicsContext.drawImage(this.offScreenImage, 0, 0, null); } } Looking at this class you see that it models an object with a) three instance variables and b) one instance method. The variables are of type Image, Graphics and Dimension. The method is update, the same update that the super class (Applet) has. Applet has paint, update, a few other methods (like init). We usually redefine paint. We know a little how paint works. We also talked about repaint (also already in Applet) and how we can call repaint directly but not paint. repaint erases then calls paint (so we can call paint but indirectly). So we create this file and compile it. We then change one line in One.java: public class One extends Applet implements MouseMotionListener { becomes public class One extends NoFlickerApplet implements MouseMotionListener { and now you can move the circles without any flicker. We noticed that some problems remain: a) when you resize there is still flicker b) Daniel has no flicker c) all of this relies on knowledge of how the process is implemented (how did we know about update?) That's the end of the exercise. The second part has to do with object-oriented modeling. Let's write the code for a game that uses a deck of cards and in which there could be up to seven players. The games starts with the players receiving cards one per player until everybody gets five cards. And that's it. Let's identify the characters (nouns): Game, Deck, Card, Player Define these classes: class Game { } class Player { } class Deck { } class Card { } Let's do all of this in DrJava. Go to http://www.drjava.org/ and download jar file under current stable release. Don't save the file load it in Java, keep blocking whatever they want to block. Now DrJava is running. Eventually we come up with this: import java.util.*; class Game { Player[] p; Game(int players) { this.p = new Player[players]; for (int i = 0; i < players; i++) this.p[i] = new Player("Player " + i); } void start() { Deck d = new Deck(); for (int i = 0; i < 5; i++) { for (Player player : this.p) { player.takeCardFrom(d); } } } } class Card { String suit, value; Card(String suit, String value) { this.suit = suit; this.value = value; } public String toString() { return this.value + this.suit; } } class Deck extends ArrayList { Deck() { String[] suits = { "c", "d", "s", "h"}; String[] values = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; for (String suit : suits) { for (String value : values) { this.add(new Card(suit, value)); } } Collections.shuffle(this); } } class Player { String name; Player(String name) { this.name = name; } void takeCardFrom(Deck d) { Card c = d.remove(0); System.out.println(this.name + " taking card... " + c); } } In the process we investigated primtive types, characters, Strings, arrays etc. We decided to use an ArrayList to model the Deck. The Deck could have contained an ArrayList. The Deck can also be an ArrayList (by extending the class) and that's what we did. Compile this with Shift+F5 then create a Game and start it: --