Question
develop simulation of fish in a fish tank. The goal is to practice Object Oriented Concepts and Comparable, MouseListener and MouseMotionListener Interfaces. The fish tank
develop simulation of fish in a fish tank. The goal is to practice Object Oriented Concepts and Comparable, MouseListener and MouseMotionListener Interfaces.
The fish tank has 2 modes of status, fish manipulation and simulation.
In the fish manipulation status, the user can:
add new fish to the tank,
drag a fish to a different slot,
To implement these manipulation functionalities, you need to implement three functions: MouseClicked, MouseDragged and MouseReleased.
MouseClicked:
Verify that the application is in manipulation mode.
Construct a new fish object at the clicked point with a random colour
If the selected slot is not occupied by another fish, add the new fish to the tank
Paint
MouseDragged:
Verify that the application is in manipulation mode.
Set mouseDragged to true
Find out which fish is selected from the tank
MouseReleased:
Verify that mouseDragged is true
Verify that the selected slot is empty
Update the position of the selected fish by the selected slot
Paint
Do not forget to reset the selected fish index and mouseDragged flag
In the simulation mode, each fish independent of the other ones in the fish tank swim in random order.
They can move maximum one slot in each dimension at each fishTick trigger or they can stay in their current slot.
If there is another fish in the slot a fish decided to move, then the fish stays in its current slot.
To implement these simulation functionalities, you need to develop body of the following functions:
CompareTo function of the Fish class:
Compare with respect to mX values
If mX values are equal, compare with respect to mY values
You need to use CompareTo function in the Move, MouseDragged, MouseReleased and MouseClicked
Move function of the Fish class:
Randomly compute a move in x direction as -1, 0, 1
Randomly compute a move in y direction as -1, 0, 1
Compare the new slot of the fish with slots of the other fish in the tank:
If the new slot is occupied, stay in the old slot, and return
If the new slot is out of the tank, stay in the old slot, and return
Paint function of the Fish class:
Draw a filled circle in the center of the selected slot with the fish color
If your fish is drawn as a nice-looking fish as follows (in minimum), then you will get 1 point bonus. Use your creativity.
Please pay attention that I used the Singleton Design Pattern to allow usage of global variables. It is an often used and a very useful design pattern.
Please also pay attention to how we define the Fish class and how Fish class instances independent of each other behave in the Fish Tank.
FishTank.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.JFrame;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.border.EtchedBorder;
class GlobalVariables {
public ArrayList
public FishTank mFishTank;
private GlobalVariables() {
mFish = new ArrayList
mFishTank = new FishTank();
}
private static GlobalVariables instance;
public static GlobalVariables getInstance() {
if (instance == null){
instance = new GlobalVariables();
}
return instance;
}
}
class Fish implements Comparable
int mX;
int mY;
int mId;
Color mColor;
public Fish(int id, int x, int y, Color color){
mId = id;
mX = x;
mY = y;
mColor = color;
}
public void paint(Graphics g){
// Implement this function
}
public void move(){
// Implement this function
}
@Override
public int compareTo(Fish o) {
// Implement this function
}
}
class FishTick extends TimerTask{
@Override
public void run() {
if (FishTank.mSimulateStatus){
for (int x=0;x Fish f = GlobalVariables.getInstance().mFish.get(x); f.move(); GlobalVariables.getInstance().mFish.set(x, f); } GlobalVariables.getInstance().mFishTank.mDrawPanel.paint(); } } } public class FishTank extends javax.swing.JFrame implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener{ private final int mNumRows = 20; private final int mNumCols = 20; private final int mGridSz = 30; private int mSelectedFishIndex = -1; private boolean mDragged = false; private int mTopHeight; JToolBar mToolbar; JToggleButton mSimulationButton; DrawPanel mDrawPanel; private int mFishIndex = 0; static public boolean mSimulateStatus = false; public static void main(String[] args) { GlobalVariables global = GlobalVariables.getInstance(); if (global == null){ System.out.println("Cannot initialize, exiting ...."); return; } } private JToggleButton addButton(String title){ JToggleButton button = new JToggleButton(title); button.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ev) { mSimulateStatus = !mSimulateStatus; } }); this.mToolbar.add(button); return (button); } public FishTank() { JFrame guiFrame = new JFrame(); guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); guiFrame.setTitle("MY FISH TANK"); // Create a toolbar and give it an etched border. this.mToolbar = new JToolBar(); this.mToolbar.setBorder(new EtchedBorder()); mSimulationButton = addButton("Simulate"); this.mToolbar.add(mSimulationButton); //This will center the JFrame in the middle of the screen guiFrame.setLocationRelativeTo(null); this.mDrawPanel = new DrawPanel(mNumRows, mNumCols, mGridSz); this.mDrawPanel.setBackground(Color.cyan); this.mDrawPanel.paint(); guiFrame.add(mDrawPanel); guiFrame.add(this.mToolbar, BorderLayout.NORTH); // Add the Exit Action JButton button = new JButton("Quit"); button.setToolTipText("Quit the program"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); mToolbar.add(button); guiFrame.addMouseListener(this); guiFrame.addMouseMotionListener(this); //make sure the JFrame is visible guiFrame.setVisible(true); mTopHeight = guiFrame.getInsets().top + mToolbar.getHeight(); guiFrame.setSize(mNumRows * mGridSz, mNumCols * mGridSz + mTopHeight); Timer timer = new Timer("tick", true); timer.scheduleAtFixedRate(new FishTick(), Calendar.getInstance().get(Calendar.MILLISECOND), 500); } @Override public void mouseClicked(MouseEvent e) { // Implement this function } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { // Implement this function } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void mouseDragged(MouseEvent e) { // Implement this function } @Override public void mouseMoved(MouseEvent e) { } } class DrawPanel extends JPanel{ int mRows; int mCols; int mGridSz; int maxGridSz; ArrayList public DrawPanel(int numberOfRows, int numberOfCols, int gridSz){ mGridSz = gridSz; mRows = numberOfRows; mCols = numberOfCols; maxGridSz = mGridSz * mRows; } private void paintBackground(Graphics g){ for (int i = 1; i g.drawLine(i * mGridSz, 0, i * mGridSz, maxGridSz); } for (int mAnimateStatus = 1; mAnimateStatus g.drawLine(0, mAnimateStatus * mGridSz, maxGridSz, mAnimateStatus * mGridSz); } } @Override public void paintComponent(Graphics g){ super.paintComponent(g); paintBackground(g); for (Fish f:GlobalVariables.getInstance().mFish){ f.paint(g); } } public void paint(){ repaint(); } } Rubric
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