import cs101.canvas.*; import java.awt.*; import java.awt.image.*; import java.net.URL; public class HandViz extends Frame { private CardList hand; private CS101Canvas canvas; public HandViz() { hand = null; setLayout(new FlowLayout(FlowLayout.RIGHT, 100, 0)); setSize(new Dimension(700,150)); } public void update(CardList cl) { removeAll(); int x = 10; for (cl=cl; cl != null; cl=cl.getRest()) { Card c = cl.getFirst(); // System.out.println("Adding " + c); int id = c.getId(); if (cl.isShown()) add(new CardImage(x, 10, id)); else add(new CardImage(x, 10, 52)); x+=50; repaint(); setVisible(true); } try {Thread.sleep(100);} catch(Throwable t) {} } public void close() { setVisible(false); } public static void selfTest() { CardList cl = null; for (int i=1; i<=8; ++i) cl = new CardList(new Card((int)(Math.random()*52)), cl); System.out.println("hand of " + cl); HandViz hv = new HandViz(); hv.update(cl); } public static void main(String[] args) { selfTest(); } } class CardImage extends Panel { private static Image cards[]; static { cards = new Image[53]; for (int i=0; i<=51; ++i) { cards[i] = loadImage("Cards/"+i+".gif"); } cards[52] = loadImage("Cards/back.gif"); } private static Image loadImage(String s) { if (Lab6.isApplet) return (Lab6.loadImage(s)); else { Image ans = null; try { ans = Toolkit.getDefaultToolkit().getImage( new URL("http://www.cs.wustl.edu/~cytron/cs101/" + s) ); } catch (Exception e) { System.err.println(e); System.exit(-1); } return(ans); } } private int num; public CardImage(int x, int y, int num) { super(); this.num = num; } public void paint(Graphics g) { setSize(new Dimension(80,100)); g.drawImage(cards[num],0,0,80,100,(ImageObserver)this); } }