Skip to content
This repository was archived by the owner on Nov 21, 2020. It is now read-only.

Commit f7edf3c

Browse files
authored
Uploaded first release
1 parent b5fd5e6 commit f7edf3c

12 files changed

Lines changed: 1246 additions & 0 deletions

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Box And Balls
2+
![Release 1.0](https://img.shields.io/badge/version-v1.0-informational)
3+
4+
Box And Balls or bab, is a software which will help you organize and store your balls.
5+
## First
6+
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.
7+
8+
## Second
9+
Select your algorithm. You have four possible choices :
10+
- Chaining: take a random box and put a ball inside of it. Repeat until there are no more balls.
11+
- 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.
12+
- 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.
13+
- 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.
14+
15+
## Finally
16+
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.

src/bab/BoxAndBalls.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package bab;
2+
3+
import javax.swing.UIManager;
4+
5+
import bab.ui.Window;
6+
7+
/**
8+
* Class implmenting the main method :<pre>
9+
* public static void main (String[] args) {...}</pre>
10+
*/
11+
public class BoxAndBalls {
12+
/**
13+
* Instanciates the {@code Window} class. Tries to set the Look and Feel.
14+
* (Style of java with the current OS)
15+
*/
16+
public static void main (String[] args) {
17+
try {
18+
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
19+
} catch (Exception e) { }
20+
21+
new Window();
22+
}
23+
}

src/bab/ui/Window.java

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package bab.ui;
2+
3+
import java.awt.CardLayout;
4+
import java.awt.Dimension;
5+
import java.util.ArrayList;
6+
import javax.swing.JFrame;
7+
import javax.swing.JPanel;
8+
9+
import bab.ui.panel.DisplayPanel;
10+
import bab.ui.panel.HomePanel;
11+
import bab.ui.panel.InputPanel;
12+
import bab.ui.panel.SelectPanel;
13+
import bab.util.Box;
14+
15+
/**
16+
* Main frame of this application, the {@code Window} class consists of a
17+
* {@code JPanel} with the {@code CardLayout} where each card is a different
18+
* section. Each section is a JPanel with its own graphic interface.
19+
*
20+
* <p>The class also stores different values used by the software. Such as the
21+
* list of the different sections the list of possible algorithms, the desired
22+
* number of balls and boxes, the list of previously filled boxes and the
23+
* selected algorithm.
24+
*/
25+
public class Window extends JFrame {
26+
private static final long serialVersionUID = -2665608161311636325L;
27+
28+
/**
29+
* The list of the different sections or panels with a {@code String} to be set as
30+
* a key when objects can't be stored. ({@code CardLayout} require a String)
31+
*/
32+
public static enum Panel {
33+
HOMEPANEL ("Home Panel"),
34+
INPUTPANEL ("Input Panel"),
35+
SELECTPANEL ("Select Panel"),
36+
DISPLAYPANEL ("Display Panel");
37+
38+
public final String txt;
39+
40+
Panel(String txt) {
41+
this.txt = txt;
42+
}
43+
}
44+
45+
/**
46+
* The list of possible algorithms.
47+
*/
48+
public static enum Algo {
49+
CHAIN,
50+
DOUBLE,
51+
LINEAR,
52+
QUADRA
53+
}
54+
55+
/**
56+
* Main panel of this frame, cards will use the CardLayout and display each
57+
* section.
58+
*/
59+
private final JPanel cards;
60+
61+
/**
62+
* The {@code DisplayPanel} is the only panel stored directly in the class
63+
* because when the user sets different values the panel needs to be
64+
* refreshed.
65+
*/
66+
private final DisplayPanel d;
67+
68+
/**
69+
* The number of boxes selected by the user.
70+
*/
71+
private Integer nbBoxes = 0;
72+
73+
/**
74+
* The number of balls selected by the user.
75+
*/
76+
private Integer nbBalls = 0;
77+
78+
/**
79+
* The list of processed boxes filled with balls.
80+
*/
81+
public ArrayList<Box> boxes;
82+
83+
/**
84+
* The selected algorithm.
85+
*/
86+
private Algo algo;
87+
88+
/**
89+
* Initializes a newly created {@code Window} object. Sets default values
90+
* and adds graphic components. Finally, displays a window to the user.
91+
*/
92+
public Window() {
93+
super("BoxAndBalls");
94+
95+
this.boxes = new ArrayList<Box>();
96+
97+
this.cards = new JPanel(new CardLayout());
98+
this.d = new DisplayPanel(this);
99+
this.cards.add(new HomePanel(this), Panel.HOMEPANEL.txt);
100+
this.cards.add(new InputPanel(this), Panel.INPUTPANEL.txt);
101+
this.cards.add(new SelectPanel(this), Panel.SELECTPANEL.txt);
102+
this.cards.add(this.d, Panel.DISPLAYPANEL.txt);
103+
104+
/*
105+
* this.setSize() takes into account the size of the title bar which is
106+
* different for windows and linux. this.pack() is better in our case.
107+
*/
108+
this.cards.setPreferredSize(new Dimension(250, 260));
109+
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
110+
this.setLocationRelativeTo(null);
111+
this.setResizable(false);
112+
113+
this.disp(Panel.HOMEPANEL);
114+
this.getContentPane().add(this.cards);
115+
116+
this.pack();
117+
this.setVisible(true);
118+
}
119+
120+
/**
121+
* Returns the number of boxes.
122+
*/
123+
public Integer getNbBoxes() {
124+
return nbBoxes;
125+
}
126+
127+
/**
128+
* Returns the number of balls.
129+
*/
130+
public Integer getNbBalls() {
131+
return nbBalls;
132+
}
133+
134+
/**
135+
* Returns the selected algorithm.
136+
*/
137+
public Algo getAlgo() {
138+
return algo;
139+
}
140+
141+
/**
142+
* Sets the number of boxes.
143+
*
144+
* @param nbBoxes the number of boxes
145+
*/
146+
public void setNbBoxes(Integer nbBoxes) {
147+
this.nbBoxes = nbBoxes;
148+
}
149+
150+
/**
151+
* Sets the number of balls.
152+
*
153+
* @param nbBalls the number of balls
154+
*/
155+
public void setNbBalls(Integer nbBalls) {
156+
this.nbBalls = nbBalls;
157+
}
158+
159+
/**
160+
* Sets the selected algorithm.
161+
*
162+
* @param algo the selected algorithm
163+
*/
164+
public void setAlgo(Algo algo) {
165+
this.algo = algo;
166+
}
167+
168+
/**
169+
* Displays the selected panel on the window. Mostly used by panels to
170+
* switch between displays.
171+
*
172+
* <p>If the selected panel is the {@code DisplayPanel}, its refresh method
173+
* is called to update the graph.
174+
*
175+
* @param panel the selected panel
176+
* @see bab.ui.panel.DisplayPanel
177+
*/
178+
public void disp(Panel panel) {
179+
if (panel == Panel.DISPLAYPANEL)
180+
this.d.refresh();
181+
182+
((CardLayout) this.cards.getLayout()).show(this.cards, panel.txt);
183+
}
184+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package bab.ui.component;
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
import java.awt.Graphics2D;
6+
import javax.swing.JComponent;
7+
8+
import bab.util.Box;
9+
10+
/**
11+
* Implements the element of the graphical interface to display boxes.
12+
*/
13+
public class BoxRender extends JComponent {
14+
private static final long serialVersionUID = 2561035491873542509L;
15+
16+
/**
17+
* The box represented by this element.
18+
*/
19+
private final Box box;
20+
21+
/**
22+
* The current display mode, number or shape.
23+
*/
24+
private final Boolean shaped;
25+
26+
/**
27+
* Initializes a newly created {@code BoxRender} object. Sets the box
28+
* represented by this object and the method of drawing it.
29+
*
30+
* @param box the box represented by this element
31+
* @param shaped the method of drawing this element
32+
*/
33+
public BoxRender(Box box, Boolean shaped) {
34+
this.box = box;
35+
this.shaped = shaped;
36+
}
37+
38+
/**
39+
* Method inherited by the {@code JComponent} class. Draws a rectangle and
40+
* a number or multiple shapes inside of it.
41+
*/
42+
public void paint(Graphics g) {
43+
Graphics2D g2d = (Graphics2D) g;
44+
45+
if (this.box.getIsFirst())
46+
g2d.setColor(Color.GREEN);
47+
else
48+
g2d.setColor(Color.BLACK);
49+
50+
g2d.drawRect(2, 2, 54, 32);
51+
52+
g2d.setColor(Color.BLACK);
53+
54+
if (shaped)
55+
for (int i = 0; i < box.getNbBalls(); i++) {
56+
g2d.fillOval(5 + (10 * i), 5, 10, 10);
57+
}
58+
else
59+
g2d.drawString(String.valueOf(this.box.getNbBalls()), 25, 23);
60+
}
61+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package bab.ui.component;
2+
3+
import javax.swing.event.DocumentEvent;
4+
import javax.swing.event.DocumentListener;
5+
6+
/**
7+
* Implements the {@code DocumentListener} interface and calls its
8+
* {@code Runnable} each time there is an event.
9+
*/
10+
public class CustomDocListener implements DocumentListener {
11+
/**
12+
* The runnable that is called.
13+
*/
14+
private final Runnable runnable;
15+
16+
/**
17+
* Initializes a newly created {@code CustomDocListener} object. Sets the
18+
* runnable that is called at each event.
19+
*
20+
* @param runnable the runnable that is called
21+
*/
22+
public CustomDocListener(Runnable runnable) {
23+
this.runnable = runnable;
24+
}
25+
26+
/**
27+
* Method inherited from the {@code DocumentListener} interface. Calls the
28+
* update method.
29+
*/
30+
@Override
31+
public void insertUpdate(DocumentEvent e) {
32+
this.update();
33+
}
34+
35+
/**
36+
* Method inherited from the {@code DocumentListener} interface. Calls the
37+
* update method.
38+
*/
39+
@Override
40+
public void removeUpdate(DocumentEvent e) {
41+
this.update();
42+
}
43+
44+
/**
45+
* Method inherited from the {@code DocumentListener} interface. Calls the
46+
* update method.
47+
*/
48+
@Override
49+
public void changedUpdate(DocumentEvent e) {
50+
this.update();
51+
}
52+
53+
/**
54+
* Calls the runnable.
55+
*/
56+
private void update() {
57+
this.runnable.run();
58+
}
59+
}

0 commit comments

Comments
 (0)