Hi, I'm new-ish to java so please excuse any mistakes I make. I've been making a dice game That when you press space, the dice "rolls" and a random number is generated. I've been trying to add the class to an html as an applet. I created a Application, and then attempted to turn it into a applet. Why doesn't the applet show up in my browser?
Application Code:
Applet Code:import java.awt.FlowLayout; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import javax.swing.*; import java.awt.event.*; public class DiceGame extends JFrame implements KeyListener, ActionListener { private JLabel label1; private JLabel label2; private JLabel label3; JTextArea displayArea; JTextField typingArea; static final String newline = System.getProperty("line.separator"); public static void main( String args[] ) { try { UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); } catch (UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (InstantiationException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } UIManager.put("swing.boldMetal", Boolean.FALSE); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } public static void createAndShowGUI() { DiceGame frame = new DiceGame("Dice Game"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.addComponentsToPane(); frame.pack(); frame.setVisible(true); } private void addComponentsToPane() { JButton button = new JButton("Clear"); button.addActionListener(this); displayArea = new JTextArea(); displayArea.setEditable(true); displayArea.addKeyListener(this); JScrollPane scrollPane = new JScrollPane(displayArea); scrollPane.setPreferredSize(new Dimension(375, 125)); getContentPane().add(scrollPane, BorderLayout.CENTER); getContentPane().add(button, BorderLayout.PAGE_END); } public DiceGame(String name) { super( name ); setLayout( new FlowLayout() ); Icon DiceGame = new ImageIcon( getClass().getResource( "dice.gif" ) ); label2 = new JLabel(); label2.setText( "" ); label2.setIcon( DiceGame ); label2.setToolTipText( "DiceGame" ); label2.setHorizontalTextPosition( SwingConstants.LEFT ); label2.setVerticalTextPosition( SwingConstants.BOTTOM ); add( label2 ); } public void keyTyped(KeyEvent e) {} public void keyPressed(KeyEvent e) { if(e.getKeyChar() == e.VK_SPACE) { int d = (int)(Math.random() * 10) % 6 + 1; displayInfo(d); } displayArea.requestFocusInWindow(); } public void keyReleased(KeyEvent e) {} public void actionPerformed(ActionEvent e) { displayArea.setText(""); displayArea.requestFocusInWindow(); } private void displayInfo(int g){ displayArea.setText(""); String keyString = Integer.toString(g); displayArea.append(keyString + newline); displayArea.setCaretPosition(displayArea.getDocument().getLength()); } }
import java.awt.FlowLayout; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import javax.swing.*; import java.awt.event.*; import java.applet.*; public class DiceGameApplet extends JApplet implements KeyListener, ActionListener { private JLabel label2 = new JLabel(); JTextArea displayArea = new JTextArea(); int width, height; JButton button = new JButton("Clear"); JScrollPane scrollPane = new JScrollPane(displayArea); Icon DiceGame = new ImageIcon( getClass().getResource( "dice.gif" ) ); static final String newline = System.getProperty("line.separator"); public void init() { width = getSize().width; height = getSize().height; button.addActionListener(this); displayArea.setEditable(true); displayArea.addKeyListener(this); scrollPane.setPreferredSize(new Dimension(375, 125)); getContentPane().add(scrollPane, BorderLayout.CENTER); getContentPane().add(button, BorderLayout.PAGE_END); setLayout( new FlowLayout() ); label2.setText( "" ); label2.setIcon( DiceGame ); label2.setToolTipText( "DiceGame" ); label2.setHorizontalTextPosition( SwingConstants.LEFT ); label2.setVerticalTextPosition( SwingConstants.BOTTOM ); add( label2 ); } public void keyTyped(KeyEvent e) {} public void keyPressed(KeyEvent e) { if(e.getKeyChar() == e.VK_SPACE) { int d = (int)(Math.random() * 10) % 6 + 1; displayInfo(d); } displayArea.requestFocusInWindow(); } public void keyReleased(KeyEvent e) {} public void actionPerformed(ActionEvent e) { displayArea.setText(""); displayArea.requestFocusInWindow(); } private void displayInfo(int g){ displayArea.setText(""); String keyString = Integer.toString(g); displayArea.append(keyString + newline); displayArea.setCaretPosition(displayArea.getDocument().getLength()); } }