Question
When i run this code i get a error class interface or enum expected? how can i solve this import java.awt.*; import java.awt.event.*; import javax.swing.*;
When i run this code i get a error class interface or enum expected? how can i solve this
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class Dice extends JApplet{
public Dice() { this.setContentPane(new RollDicePanel()); } public static void main(String[] args) { JFrame window = new JFrame(); window.setTitle("Dice Demo"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setContentPane(new RollDicePanel()); window.pack(); window.show(); }//end main } class RollDicePanel extends JPanel { private Die _left; // component for one die private Die _right; RollDicePanel() { JButton rollButton = new JButton("Roll"); rollButton.setFont(new Font("Sansserif", Font.PLAIN, 24)); rollButton.addActionListener(new RollListener()); JPanel dicePanel = new JPanel(); dicePanel.setLayout(new GridLayout(1, 2, 4, 0)); dicePanel.add(_left); dicePanel.add(_right); this.setLayout(new BorderLayout()); this.add(rollButton, BorderLayout.NORTH); this.add(dicePanel , BorderLayout.CENTER); } private class RollListener implements ActionListener { public void actionPerformed(ActionEvent e) { _left.roll(); _right.roll(); } } } private int _value; // value that shows on face of die private int _diam = 9; // Diameter of spots private static Random random = new Random(); // random generator
public Die() { setBackground(Color.white); //-- Preferred size is set, but layout may change it. setPreferredSize(new Dimension(60,60)); roll(); // Set to random initial value } public int roll() { int val = random.nextInt(6) + 1; // Range 1-6 setValue(val); return val; } public int getValue() { return _value; } public void setValue(int spots) { _value = spots; repaint(); // Value has changed, must repaint } public void paintComponent(Graphics g) { super.paintComponent(g); // required int w = getWidth(); int h = getHeight(); // should use to resize spots too. switch (_value) { case 1: drawSpot(g, w/2, h/2); break; case 3: drawSpot(g, w/2, h/2); // Fall thru to next case case 2: drawSpot(g, w/4, h/4); drawSpot(g, 3*w/4, 3*h/4); break; case 5: drawSpot(g, w/2, h/2); // Fall thru to next case case 4: drawSpot(g, w/4, h/4); drawSpot(g, 3*w/4, 3*h/4); drawSpot(g, 3*w/4, h/4); drawSpot(g, w/4, 3*h/4); break; case 6: drawSpot(g, w/4, h/4); drawSpot(g, 3*w/4, 3*h/4); drawSpot(g, 3*w/4, h/4); drawSpot(g, w/4, 3*h/4); drawSpot(g, w/4, h/2); drawSpot(g, 3*w/4, h/2); break; } private void drawSpot(Graphics g, int x, int y) { g.fillOval(x-_diam/2, y-_diam/2, _diam, _diam); }//end drawSpot }//
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started