Java Based application to demonstrate how to use OOP principles, for example interface, inheritance.
For example, class InsideContainer is represent the Fish Container for the Aquarium. At its constructor we initialize the baseBackground for the water image, and initialize fishCollection variable to hold the fishes.
InsideContainer, the paint method. In the paint method we override:
- Create a buffer image for Double Buffering (Drawing Image in memory first)
- Draw the background SeaScape
- Loop all fishCollection and draw the fish current image ( each fish will have two type of image, left or right).
For example, Fish Abstract Class. Fish is an abstract class, this class cannot be instantiate. We will need to inherits from Fish and make a new class, for example RedFish.
The RedFish class, use static initializer to load images, for example, fish-red.png and fish-red-right.png You will see that RedFish will be forced to override two abstract methods from Fish class:
- getLeftImage()
- getRightImage()
But many methods its already inherited from Fish class:
- moveUp()
- moveDown()
- moveLeft()
- moveRight()
- getCurrentImage()
- run() - this is for Runnable thread
Some of them is either protected or private.
private method in Fish class means the method is only visible inside the Fish class, and cannot be invoke from outside Fish class, for examples:
- randomStartPosition
- randomMovementArea
- randomHorizontalDirection
- randomVerticalDirection
- randomMinLeft
- randomMaxRight
- randomMinTop
- randomMaxBottom
protected method in Fish class means the method is only visible inside the Fish class and it subclasses, so RedFish as a subclass still can invoke it directly:
- setCurrentImage(Image currentImage)
By providing private modifiers, we hide the complexity of the Fish class, so when we create a new class, extending from Fish, we can only see the public/protected and can override the required method.
Using FishFactory Class FishFactory is a class that can be used to instantiate various kinds of Fish, we are recommending to create a fish object using FishFactory. The FishFactory will read the index file and load it to program.
Easily adding new Fish
- Create a new class for example YellowFish.java in package.
- Implements the required method, make sure you use image, for example fish-yellow.png and fish-yellow-right.png
- Put those images in src/main/resources.
- Add some line the index file.
Get help: TBD-support • Review the GitHub status page
© 2023 TBD-copyright-holder • Code of Conduct • MIT License