import java.applet.Applet; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class mrAppl extends Applet { JButton b2; Button b3; Label b1, l1, l2, l3, l4; Panel p1, p2, p3, p4; Label msgLabel; Memory mem = new Memory (); ShiftBuffer sb = new ShiftBuffer (4); char guess, prediction; int right = 0, wrong = 0; public void init () { p1 = new Panel(); p2 = new Panel(); p3 = new Panel(); p4 = new Panel(); b1 = new Label (" ? "); // b2 = new Button ("Head"); b2 = new JButton (new ImageIcon("s.gif")); b3 = new Button ("Tail"); b2.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent ae) { System.out.println ("Head"); foobar ('h'); }} ); b3.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent ae) { System.out.println ("Tail"); foobar ('t'); }} ); l1 = new Label ("You:"); l2 = new Label ("Computer:"); l3 = new Label ("0"); l4 = new Label ("0"); p1.add (l1); p1.add (l3); p1.add (l2); p1.add (l4); // p1.setBackground(Color.red); add(p1); p2.add(b1); //p2.setBackground(Color.green); add(p2); msgLabel = new Label("I've made my prediction", Label.CENTER); // msgLabel.setText("=======Hello!========"); // msgLabel.setText("Hello!"); p4.add(msgLabel); add(p4); // msgLabel.setBackground(Color.yellow); // p4.setBackground(Color.blue); p3.add (b2); p3.add (b3); // p3.setBackground(Color.blue); add(p3); ((FlowLayout)p3.getLayout()).setHgap(60); setLayout (new GridLayout (4,1)); setSize(200,100); setBackground(Color.pink); // setFont (new Font ("serif", Font.BOLD, 20); // String ans = JOptionPane.showInputDialog ("Hello!!"); // Frame aFrame = new Frame ("Hi"); // aFrame.add(this, BorderLayout.CENTER); // aFrame.setSize(50,50); // aFrame.setVisible(true); play(); } public void play () { prediction = mem.retrieve(sb); System.out.println ("My prediction: " + prediction); b1.setText(" ? "); msgLabel.setText("I've made my prediction"); } void foobar (char guess) { b1.setText(prediction == 'h' ? "Head" : "Tail"); if (guess == prediction) { System.out.println ("Yes!. I too predicted " + prediction); right++; msgLabel.setText("I'm right!"); l4.setText (""+right); Dialog dl = new Dialog((Frame) getParent()); // dl.show(); } else { System.out.println ("No. I predicted " + prediction); wrong++; msgLabel.setText("Missed."); l3.setText (""+wrong); } mem.store (sb, guess); sb.shift (guess); System.out.println ("Score = " + right + " | " + wrong); try { Thread.sleep (1000); } catch (InterruptedException e) {} play(); } }