Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I am trying to find a way to update the sliders by getting the updated information from the model class. Class with sliders: public

Hello, I am trying to find a way to update the sliders by getting the updated information from the model class.
Class with sliders: public class GoneFishingView extends JFrame
{
private JLabel labelTry;
private JLabel labelFish;
private JPanel fishingPanel;
private JPanel fishingSliderPanel;
private JSlider fishingSlideTry;
private JSlider fishingSlideFish;
private JPanel instructionsPanel;
private JLabel instructionLabel;
private GoneFishingModel model;
public GoneFishingView(GoneFishingModel model)
{
this.model = model;
updateUI();
setTitle("Fishing Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
instructionsPanel = new JPanel();
instructionLabel = new JLabel("Click on the buttons to fish");
instructionsPanel.add(instructionLabel);
add(instructionsPanel, BorderLayout.WEST);
fishingPanel = new JPanel();
fishingPanel.setLayout(new GridLayout(model.DIMENSION, model.DIMENSION));
for (int row =0; row < model.DIMENSION; row++){
for (int col =0; col < model.DIMENSION; col++){
FishingButton button = new FishingButton(row, col);
button.addActionListener(new FishingButtonListener(model, this , button));
button.setSize(100,100);
fishingPanel.add(button);
add(fishingPanel, BorderLayout.CENTER);
fishingPanel.setPreferredSize(new Dimension(400,100));
}
}
fishingSliderPanel = new JPanel();
fishingSliderPanel.setLayout(new GridLayout(1,2));
JPanel fishingSliderTryPanel = new JPanel();
fishingSlideTry = new JSlider(JSlider.VERTICAL,0, model.getTriesRemaining(), model.getTriesRemaining()); fishingSlideTry.setMajorTickSpacing(10);
fishingSlideTry.setPaintLabels(true);
fishingSlideTry.setPaintTicks(true);
labelTry = new JLabel("Tries");
fishingSliderTryPanel.add(fishingSlideTry);
fishingSliderTryPanel.add(labelTry);
fishingSliderPanel.add(fishingSliderTryPanel);
JPanel fishingSliderFishPanel = new JPanel();
fishingSlideFish = new JSlider(JSlider.VERTICAL,0, model.getFishRemaining(), model.getFishRemaining());
fishingSlideFish.setMajorTickSpacing(5);
fishingSlideFish.setPaintLabels(true);
fishingSlideFish.setPaintTicks(true);
labelFish = new JLabel("Fish");
fishingSliderFishPanel.add(fishingSlideFish);
fishingSliderFishPanel.add(labelFish);
fishingSliderPanel.add(fishingSliderFishPanel);
add(fishingSliderPanel, BorderLayout.EAST);
JPanel Bottom = new JPanel();
JLabel bottomLabel = new JLabel("Programmed by: Marko Yovanovich");
Bottom.add(bottomLabel);
add(Bottom, BorderLayout.SOUTH);
pack();
setVisible(true);
}
public static void main(String[] args)
{
new GoneFishingView(new GoneFishingModel());
}
public void updateUI()
{
}
}
Model Class:
public class GoneFishingModel
{
public static final int DIMENSION =6;
private boolean[][] grid = new boolean[DIMENSION][DIMENSION];
private int triesRemaining =30;
private int fishRemaining =10;
/**
* TODO
*/
public GoneFishingModel()
{
Random randomNumberGenerator = new Random();
for (int fishCounter =0; fishCounter < fishRemaining; fishCounter++)
{
int row, column;
do
{
row = randomNumberGenerator.nextInt(DIMENSION);
column = randomNumberGenerator.nextInt(DIMENSION);
} while (grid[row][column]);
grid[row][column]= true;
}
}
/**
* TODO
* @param row
* @param column
* @return
*/
public boolean fishAt(int row, int column)
{
triesRemaining--;
boolean foundFish = grid[row][column];
if (foundFish)
{
fishRemaining--;
}
return foundFish;
}
/**
* TODO
* @return
*/
public int getTriesRemaining()
{
return triesRemaining;
}
/**
* TODO
* @return
*/
public int getFishRemaining()
{
return fishRemaining;
}
/**
* TODO
* @return
*/
public boolean fishWin()
{
return triesRemaining ==0 && fishRemaining >0;
}
/**
* TODO
* @return
*/
public boolean playerWins()
{
return fishRemaining ==0;
}
}
Listener Class:
public class FishingButtonListener implements ActionListener
{
private GoneFishingModel goneFishingModel;
private GoneFishingView goneFishingView;
private FishingButton fishingButton;
public FishingButtonListener(GoneFishingModel goneFishingModel, GoneFishingView goneFishingView,
FishingButton fishingButton)
{
this.goneFishingModel = goneFishingModel;
this.goneFishingView = goneFishingView;
this.fishingButton = fishingButton;
}
@Override
public void actionPerformed(ActionEvent e){

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions

Question

Discuss the role of motivation in financial literacy.

Answered: 1 week ago

Question

Explain the role of research design in HRD evaluation

Answered: 1 week ago