Write a DiceViewer and DiceFrame (similar to InvestmentViewer and InvestmentFram
ID: 3669234 • Letter: W
Question
Write a DiceViewer and DiceFrame (similar to InvestmentViewer and InvestmentFrame in Section 10.2.3) that simulates the rolling of a dice (die) using the Random class - see nextInt(int) method.
Similar to :
import javax.swing.JFrame;
/**
This program shows the growth of an investment.
*/
public class InvestmentViewer
{
public static void main(String[] args)
{
JFrame frame = new InvestmentFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
-------------------------------------------------------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class InvestmentFrame extends JFrame
{
private JButton button;
private JLabel resultLabel;
private double balance;
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 100;
private static final double INTEREST_RATE = 5;
private static final double INITIAL_BALANCE = 1000;
public InvestmentFrame()
{
balance = INITIAL_BALANCE;
createComponents();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
/**
Adds interest to the balance and updates the display.
*/
class AddInterestListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
double interest = balance * INTEREST_RATE / 100;
balance = balance + interest;
resultLabel.setText("Balance: " + balance);
}
}
private void createComponents()
{
button = new JButton("Add Interest");
ActionListener listener = new AddInterestListener();
button.addActionListener(listener);
resultLabel = new JLabel("Balance: " + balance);
JPanel panel = new JPanel();
panel.add(button);
panel.add(resultLabel);
add(panel);
}
}
Explanation / Answer
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Game extends JFrame implements ActionListener
{
JPanel mainPanel;
JPanel optionPanel;
JPanel dicePanel;
JButton rollDice;
JMenu options;
JMenuItem quit;
JMenuItem explanation;
JMenuBar menuBar;
JLabel diceLabel;
JLabel diceLabel2;
DiceRoll dr;
Graphics die1;
Graphics die2;
public Game(){
// Set the JFrame properties
setTitle("Rollin' Dice");
this.setPreferredSize(new Dimension(600,600));
// Create the main JPanel to hold the interface
mainPanel = new JPanel(new BorderLayout());
// Build the Menu
options = new JMenu("Options");
quit = new JMenuItem("Quit");
options.add(quit);
explanation = new JMenuItem("Explanation");
options.add(explanation);
menuBar = new JMenuBar();
menuBar.add(options);
// Add the menu to the top of the main panel
mainPanel.add(menuBar,BorderLayout.NORTH);
// Create the dice
dr = new DiceRoll();
diceLabel = new JLabel();
diceLabel2 = new JLabel();
dicePanel = new JPanel(new GridLayout(2,1));
dicePanel.add(diceLabel);
dicePanel.add(diceLabel2);
// Add the dicePanel to the center of the main panel
mainPanel.add(dicePanel,BorderLayout.CENTER);
// Add the rollDice button to the bottom of the main panel
rollDice = new JButton("Roll Dice");
mainPanel.add(rollDice,BorderLayout.SOUTH);
// Add listeners to the menu items and buttons
quit.addActionListener(this);
explanation.addActionListener(this);
rollDice.addActionListener(this);
// Add the main panel to the JFrame
this.getContentPane().add(mainPanel);
// Show the JFrame
this.pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()== quit)
System.exit(0);
if (e.getSource() == explanation)
{
JOptionPane.showMessageDialog(mainPanel,
"Win: Roll a sum that is an even number Lose: Roll a sum that is an odd number" + dicePanel, "Rules", JOptionPane.INFORMATION_MESSAGE);
}
if (e.getSource() == rollDice)
{
dr.roll(diceLabel);
dr.roll(diceLabel2);
}
diceLabel.updateUI();
dicePanel.updateUI();
}
}
public static void main (String []args)
{
Game dg = new Game();
}
Dice Program:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.imageio.*;
import java.io.File;
import java.io.IOException;
import javax.swing.JComponent;
public class DiceRoll extends JComponent {
private BufferedImage die1;
private BufferedImage die2;
private BufferedImage die3;
private BufferedImage die4;
private BufferedImage die5;
private BufferedImage die6;
public DiceRoll()
{
try {
die1 = (ImageIO.read(new File("die1.png")));
die2 = ImageIO.read(new File("die2.png"));
die3 = ImageIO.read(new File("die3.png"));
die4 = ImageIO.read(new File("die4.png"));
die5 = ImageIO.read(new File("die5.png"));
die6 = ImageIO.read(new File("die6.png"));
} catch (IOException ex){
System.err.println("That is invalid");
}
}
public void roll(JLabel dieLabel)
{
int dieResult = (int)(6 * Math.random());
dieLabel.setIcon(new ImageIcon("die" + dieResult + ".png"))
return g;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.