// // Eighth Ball -- Programmed by Trevor Stone, tstone@colorado.edu -- 11/2/1999 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class EighthBall extends JApplet { String [] questions = { "Will you be rich?", "What is the meaning of life?", "What does the future hold?", "Is it because of uncertainty that you give answers?", "What is the answer to this question?", "What's the answer to life, the universe, and everything?", "What are you doing this weekend?", "Just who do you think you are?", "Don't you have something better to do?", "Where did I leave my keys?", "Does she like me?", "When will I die?" }; String [] answers = { "You can hope, anyway.", "When pigs fly.", "Sure, why not?", "Not a chance in hell.", "It's as likely as a pro wrestler being elected governor.", "Forty-two.", "Yes, but not in this lifetime.", "It could be worse.", "That's the most absurd thing I've ever heard.", "I gah-ron-tee.", "I pitty the fool who says otherwise.", "Again with the questions?", "You think I know? I'm just an applet!", "Answer uncertain. Assume no." }; int numquestions, numanswers; String result; JLabel questnum, ansnum, title; public void init () { JButton questBut = new JButton("Question"); JButton ansBut = new JButton("Answer"); JTextField query = new JTextField("Enter a question or answer."); questnum = new JLabel("Questions asked: 0"); questnum.setVerticalTextPosition(JLabel.BOTTOM); questnum.setHorizontalTextPosition(JLabel.LEFT); ansnum = new JLabel("Answers given: 0"); ansnum.setVerticalTextPosition(JLabel.BOTTOM); ansnum.setHorizontalTextPosition(JLabel.RIGHT); result = new String("(1/8)"); questBut.addActionListener(new questionListener()); ansBut.addActionListener(new answerListener()); JPanel inputPanel = new JPanel(); inputPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); inputPanel.add(questBut); inputPanel.add(query); inputPanel.add(ansBut); JPanel questPanel = new JPanel(); questPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); JPanel ansPanel = new JPanel(); ansPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); title = new JLabel("EighthBall"); title.setForeground(Color.magenta); JPanel statusPanel = new JPanel(); statusPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 5)); statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS)); statusPanel.add(questnum); statusPanel.add(Box.createHorizontalGlue()); statusPanel.add(title); statusPanel.add(Box.createHorizontalGlue()); statusPanel.add(ansnum); Dimension d = getSize(); JPanel resultPanel = (new JPanel() { public void paintComponent (Graphics g) { int circsize = (getWidth() > getHeight()) ? getHeight() : getWidth(); int left = getWidth()/2 - circsize/2; int top = getHeight()/2 - circsize/2; g.setColor(Color.black); g.fillOval(left, top, circsize, circsize); g.setColor(Color.cyan); FontMetrics fn = g.getFontMetrics(); int resleft = left+circsize/2-fn.stringWidth(result)/2; int restop = top+circsize/2+ (fn.getHeight())/2; g.drawString(result, resleft, restop); } }); resultPanel.setPreferredSize(new Dimension(d.width, d.width)); resultPanel.setBackground(Color.white); Container c = getContentPane(); c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS)); c.add(inputPanel); c.add(resultPanel); c.add(statusPanel); } public class questionListener implements ActionListener { public void actionPerformed(ActionEvent e) { String newstring = questions[(int) java.lang.Math.floor(java.lang.Math.random()*questions.length)]; result = newstring; numquestions++; questnum.setText("Questions asked: " + numquestions); if ((numquestions + numanswers)%2 == 1) title.setText("by Trevor Stone"); else title.setText("EighthBall"); repaint(); } } public class answerListener implements ActionListener { public void actionPerformed(ActionEvent e) { String newstring = answers[(int) java.lang.Math.floor(java.lang.Math.random()*answers.length)]; result = newstring; numanswers++; ansnum.setText("Answers given: " + numanswers); if ((numquestions + numanswers)%2 == 1) title.setText("by Trevor Stone"); else title.setText("EighthBall"); repaint(); } } }