This repository has been archived by the owner on Nov 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5fd5e6
commit f7edf3c
Showing
12 changed files
with
1,246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Box And Balls | ||
 | ||
|
||
Box And Balls or bab, is a software which will help you organize and store your balls. | ||
## First | ||
Enter the number of boxes and the number of balls you wish to process. Remember, if you want the software to accept your values, the number of balls must be between your number of boxes and half your number of boxes. So, if you set 10 boxes, you can have between 5 to 10 balls. | ||
|
||
## Second | ||
Select your algorithm. You have four possible choices : | ||
- Chaining: take a random box and put a ball inside of it. Repeat until there are no more balls. | ||
- Double choice: take two random boxes (may be the same box), put a ball in the least filled one. If they contain the same number of balls, the first box gets the ball. | ||
- Open linear addressing: box size is limited to one ball, take a random box, if it is empty put a ball in it, else go to the next one on the list. Repeat until there are no more balls. | ||
- Open quadratic addressing: box size is limited to one ball, take a random box, if it is empty put a ball in it, else go to the box + 1, if this one is also full go to the box + 4 then box + 9 then box + 16, ... Repeat until there are no more balls. | ||
|
||
## Finally | ||
Display your boxes on your screen. You can choose between two display modes, "number" or "shape", number puts numbers in your boxes, shape puts shapes in your boxes. If you chose to use the open linear addressing or open quadratic addressing, you may find some green colored box. This is it was the first empty box encountered to put a ball in it. Also the max value on top of the screen displays the maximum number of boxes visited to place a ball. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package bab; | ||
|
||
import javax.swing.UIManager; | ||
|
||
import bab.ui.Window; | ||
|
||
/** | ||
* Class implmenting the main method :<pre> | ||
* public static void main (String[] args) {...}</pre> | ||
*/ | ||
public class BoxAndBalls { | ||
/** | ||
* Instanciates the {@code Window} class. Tries to set the Look and Feel. | ||
* (Style of java with the current OS) | ||
*/ | ||
public static void main (String[] args) { | ||
try { | ||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | ||
} catch (Exception e) { } | ||
|
||
new Window(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
package bab.ui; | ||
|
||
import java.awt.CardLayout; | ||
import java.awt.Dimension; | ||
import java.util.ArrayList; | ||
import javax.swing.JFrame; | ||
import javax.swing.JPanel; | ||
|
||
import bab.ui.panel.DisplayPanel; | ||
import bab.ui.panel.HomePanel; | ||
import bab.ui.panel.InputPanel; | ||
import bab.ui.panel.SelectPanel; | ||
import bab.util.Box; | ||
|
||
/** | ||
* Main frame of this application, the {@code Window} class consists of a | ||
* {@code JPanel} with the {@code CardLayout} where each card is a different | ||
* section. Each section is a JPanel with its own graphic interface. | ||
* | ||
* <p>The class also stores different values used by the software. Such as the | ||
* list of the different sections the list of possible algorithms, the desired | ||
* number of balls and boxes, the list of previously filled boxes and the | ||
* selected algorithm. | ||
*/ | ||
public class Window extends JFrame { | ||
private static final long serialVersionUID = -2665608161311636325L; | ||
|
||
/** | ||
* The list of the different sections or panels with a {@code String} to be set as | ||
* a key when objects can't be stored. ({@code CardLayout} require a String) | ||
*/ | ||
public static enum Panel { | ||
HOMEPANEL ("Home Panel"), | ||
INPUTPANEL ("Input Panel"), | ||
SELECTPANEL ("Select Panel"), | ||
DISPLAYPANEL ("Display Panel"); | ||
|
||
public final String txt; | ||
|
||
Panel(String txt) { | ||
this.txt = txt; | ||
} | ||
} | ||
|
||
/** | ||
* The list of possible algorithms. | ||
*/ | ||
public static enum Algo { | ||
CHAIN, | ||
DOUBLE, | ||
LINEAR, | ||
QUADRA | ||
} | ||
|
||
/** | ||
* Main panel of this frame, cards will use the CardLayout and display each | ||
* section. | ||
*/ | ||
private final JPanel cards; | ||
|
||
/** | ||
* The {@code DisplayPanel} is the only panel stored directly in the class | ||
* because when the user sets different values the panel needs to be | ||
* refreshed. | ||
*/ | ||
private final DisplayPanel d; | ||
|
||
/** | ||
* The number of boxes selected by the user. | ||
*/ | ||
private Integer nbBoxes = 0; | ||
|
||
/** | ||
* The number of balls selected by the user. | ||
*/ | ||
private Integer nbBalls = 0; | ||
|
||
/** | ||
* The list of processed boxes filled with balls. | ||
*/ | ||
public ArrayList<Box> boxes; | ||
|
||
/** | ||
* The selected algorithm. | ||
*/ | ||
private Algo algo; | ||
|
||
/** | ||
* Initializes a newly created {@code Window} object. Sets default values | ||
* and adds graphic components. Finally, displays a window to the user. | ||
*/ | ||
public Window() { | ||
super("BoxAndBalls"); | ||
|
||
this.boxes = new ArrayList<Box>(); | ||
|
||
this.cards = new JPanel(new CardLayout()); | ||
this.d = new DisplayPanel(this); | ||
this.cards.add(new HomePanel(this), Panel.HOMEPANEL.txt); | ||
this.cards.add(new InputPanel(this), Panel.INPUTPANEL.txt); | ||
this.cards.add(new SelectPanel(this), Panel.SELECTPANEL.txt); | ||
this.cards.add(this.d, Panel.DISPLAYPANEL.txt); | ||
|
||
/* | ||
* this.setSize() takes into account the size of the title bar which is | ||
* different for windows and linux. this.pack() is better in our case. | ||
*/ | ||
this.cards.setPreferredSize(new Dimension(250, 260)); | ||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
this.setLocationRelativeTo(null); | ||
this.setResizable(false); | ||
|
||
this.disp(Panel.HOMEPANEL); | ||
this.getContentPane().add(this.cards); | ||
|
||
this.pack(); | ||
this.setVisible(true); | ||
} | ||
|
||
/** | ||
* Returns the number of boxes. | ||
*/ | ||
public Integer getNbBoxes() { | ||
return nbBoxes; | ||
} | ||
|
||
/** | ||
* Returns the number of balls. | ||
*/ | ||
public Integer getNbBalls() { | ||
return nbBalls; | ||
} | ||
|
||
/** | ||
* Returns the selected algorithm. | ||
*/ | ||
public Algo getAlgo() { | ||
return algo; | ||
} | ||
|
||
/** | ||
* Sets the number of boxes. | ||
* | ||
* @param nbBoxes the number of boxes | ||
*/ | ||
public void setNbBoxes(Integer nbBoxes) { | ||
this.nbBoxes = nbBoxes; | ||
} | ||
|
||
/** | ||
* Sets the number of balls. | ||
* | ||
* @param nbBalls the number of balls | ||
*/ | ||
public void setNbBalls(Integer nbBalls) { | ||
this.nbBalls = nbBalls; | ||
} | ||
|
||
/** | ||
* Sets the selected algorithm. | ||
* | ||
* @param algo the selected algorithm | ||
*/ | ||
public void setAlgo(Algo algo) { | ||
this.algo = algo; | ||
} | ||
|
||
/** | ||
* Displays the selected panel on the window. Mostly used by panels to | ||
* switch between displays. | ||
* | ||
* <p>If the selected panel is the {@code DisplayPanel}, its refresh method | ||
* is called to update the graph. | ||
* | ||
* @param panel the selected panel | ||
* @see bab.ui.panel.DisplayPanel | ||
*/ | ||
public void disp(Panel panel) { | ||
if (panel == Panel.DISPLAYPANEL) | ||
this.d.refresh(); | ||
|
||
((CardLayout) this.cards.getLayout()).show(this.cards, panel.txt); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package bab.ui.component; | ||
|
||
import java.awt.Color; | ||
import java.awt.Graphics; | ||
import java.awt.Graphics2D; | ||
import javax.swing.JComponent; | ||
|
||
import bab.util.Box; | ||
|
||
/** | ||
* Implements the element of the graphical interface to display boxes. | ||
*/ | ||
public class BoxRender extends JComponent { | ||
private static final long serialVersionUID = 2561035491873542509L; | ||
|
||
/** | ||
* The box represented by this element. | ||
*/ | ||
private final Box box; | ||
|
||
/** | ||
* The current display mode, number or shape. | ||
*/ | ||
private final Boolean shaped; | ||
|
||
/** | ||
* Initializes a newly created {@code BoxRender} object. Sets the box | ||
* represented by this object and the method of drawing it. | ||
* | ||
* @param box the box represented by this element | ||
* @param shaped the method of drawing this element | ||
*/ | ||
public BoxRender(Box box, Boolean shaped) { | ||
this.box = box; | ||
this.shaped = shaped; | ||
} | ||
|
||
/** | ||
* Method inherited by the {@code JComponent} class. Draws a rectangle and | ||
* a number or multiple shapes inside of it. | ||
*/ | ||
public void paint(Graphics g) { | ||
Graphics2D g2d = (Graphics2D) g; | ||
|
||
if (this.box.getIsFirst()) | ||
g2d.setColor(Color.GREEN); | ||
else | ||
g2d.setColor(Color.BLACK); | ||
|
||
g2d.drawRect(2, 2, 54, 32); | ||
|
||
g2d.setColor(Color.BLACK); | ||
|
||
if (shaped) | ||
for (int i = 0; i < box.getNbBalls(); i++) { | ||
g2d.fillOval(5 + (10 * i), 5, 10, 10); | ||
} | ||
else | ||
g2d.drawString(String.valueOf(this.box.getNbBalls()), 25, 23); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package bab.ui.component; | ||
|
||
import javax.swing.event.DocumentEvent; | ||
import javax.swing.event.DocumentListener; | ||
|
||
/** | ||
* Implements the {@code DocumentListener} interface and calls its | ||
* {@code Runnable} each time there is an event. | ||
*/ | ||
public class CustomDocListener implements DocumentListener { | ||
/** | ||
* The runnable that is called. | ||
*/ | ||
private final Runnable runnable; | ||
|
||
/** | ||
* Initializes a newly created {@code CustomDocListener} object. Sets the | ||
* runnable that is called at each event. | ||
* | ||
* @param runnable the runnable that is called | ||
*/ | ||
public CustomDocListener(Runnable runnable) { | ||
this.runnable = runnable; | ||
} | ||
|
||
/** | ||
* Method inherited from the {@code DocumentListener} interface. Calls the | ||
* update method. | ||
*/ | ||
@Override | ||
public void insertUpdate(DocumentEvent e) { | ||
this.update(); | ||
} | ||
|
||
/** | ||
* Method inherited from the {@code DocumentListener} interface. Calls the | ||
* update method. | ||
*/ | ||
@Override | ||
public void removeUpdate(DocumentEvent e) { | ||
this.update(); | ||
} | ||
|
||
/** | ||
* Method inherited from the {@code DocumentListener} interface. Calls the | ||
* update method. | ||
*/ | ||
@Override | ||
public void changedUpdate(DocumentEvent e) { | ||
this.update(); | ||
} | ||
|
||
/** | ||
* Calls the runnable. | ||
*/ | ||
private void update() { | ||
this.runnable.run(); | ||
} | ||
} |
Oops, something went wrong.