Commit 0a36a8ee authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Cleaning up.

parent c9b092e2
Loading
Loading
Loading
Loading
+56 −41
Original line number Diff line number Diff line
package edu.bu.ec504.spr19.sameGameTris;
package edu.bu.ec504.spr24.sameGameTris;

import java.awt.Color;
import java.awt.Component;
@@ -15,25 +15,27 @@ import java.util.concurrent.atomic.AtomicInteger;
 */
class SelfAwareCircle extends Component implements MouseListener, SelfAwareListener {
	// Constants
	private final Color hltColor = new Color(200,200,200); // the color to use for highlighting a button
	private final Color _hltColor = new Color(200,200,200); // the color to use for highlighting a button

	// Data fields
	// FIELDS
	public final int xx;
	public final int yy;
	public CircleColor clr;
	private static final long serialVersionUID = 1L;
	private final ArrayList<SelfAwareListener> _listeners = new ArrayList<>(); // who is listening to events fired by this circle
	private static int id=0;
	private final int myid; // the circle's ID
	private Visibility state = Visibility.plain;	// the current state of the circle
	private final sge sgeGui;                       // a link to the GUI that created this circle
	private static int _id =0;
	private final int _myID; // the circle's ID
	private Visibility _state = Visibility.plain;	// the current state of the circle
	private final sge sgeGUI;                       // a link to the GUI that created this circle

	// concurrency variables
	/**
	 * Used to manage concurrency.
	 */
	private static final AtomicInteger regionLength = new AtomicInteger(0);

	// SUBCLASSES
	/*
	 * Records the length of the selected region
	 * Records the visibility of an object.
	 */
	public enum Visibility{
		/*
@@ -65,9 +67,9 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
			this.clr = clr; // record the color of the button
			if (clr == CircleColor.NONE)    // circles with no color should be cleared
				setClear();
			this.sgeGui = sgeGui;   // record the GUI that created this button
			this.sgeGUI = sgeGui;   // record the GUI that created this button
			addMouseListener(this);        // register for mouse events
			myid = id++;
			_myID = _id++;
		}

		/**
@@ -86,11 +88,11 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
		}

		public int getId() {
			return myid;
			return _myID;
		}

		 Visibility getState() {
			return state;
		 Visibility get_state() {
			return _state;
		}

		public CircleColor getColor() {
@@ -104,20 +106,20 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
			clr=CircleColor.randColor();
		}

		void setState (Visibility state) {
			if (state==Visibility.clear)
		void set_state(Visibility _state) {
			if (_state ==Visibility.clear)
				setClear();   // i.e. there is extra overhead in clearing
			else
				this.state = state;
				this._state = _state;
			repaint();
		}

		boolean isCleared() {
			return this.state == Visibility.clear;
			return this._state == Visibility.clear;
		}

		void setClear() {
			state = Visibility.clear;
			_state = Visibility.clear;
			repaint();
		}

@@ -125,18 +127,18 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
		 * Copies immutable elements of this SelfAwareCircle to "recipient"
		 *    *except* position-dependent items (e.g. _listeners, xx, yy)
		 */
		void copy(SelfAwareCircle recipient) {
		void copyTo(SelfAwareCircle recipient) {
			recipient.clr=clr;
			if (recipient.state == state)
			if (recipient._state == _state)
				return; // i.e. nothing to do

			// special cases
			// ... copying a cleared circle to an uncleared circle
			if (state == Visibility.clear)
			if (_state == Visibility.clear)
				recipient.setClear();   // includes removing a mouse listener

			// copy the state
			recipient.setState(state);
			recipient.set_state(_state);
		}

		// ... Mouse methods
@@ -146,7 +148,7 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
		 * Highlights the button by changing its icon and firing off an event
		 * @param state determines whether to highlight or unhighlight the circle
		 */
		private void highlightButton(Visibility stt) {
		private void _highlightButton(Visibility stt) {
			if (isCleared())  // don't go further with already cleared circles
				return;

@@ -154,7 +156,7 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
			if (stt==Visibility.clear) // clear has its own special treatment
				setClear(); // properly clear this circle (including removal of mouse listener)
			else
				setState(stt);   // set the non-cleared state appropriately
				set_state(stt);   // set the non-cleared state appropriately

			// deal with the region length
			regionLength.getAndIncrement(); // update the region length by the newly highlighted item
@@ -171,30 +173,43 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
		 */
		public void mouseEntered(MouseEvent e) {
			regionLength.set(0); // i.e. reinitialize
			highlightButton(Visibility.highlight);
			_highlightButton(Visibility.highlight);
		}

	/**
	 * Mouse exited the object area.
	 * *  Change its button icon
	 * *  Broadcast the change to other buttons
	 */
		public void mouseExited(MouseEvent e) {
			regionLength.set(0); // i.e. reinitialize
			highlightButton(Visibility.plain);
			_highlightButton(Visibility.plain);
		}

	/**
	 * The mouse button was pressed.
	 */
	public void mousePressed(MouseEvent e) {
			regionLength.set(0); // i.e. reinitialize
		}

	/**
	 * The mouse button was released.
	 */
	public void mouseReleased(MouseEvent e) {
			highlightButton(Visibility.clear);
			_highlightButton(Visibility.clear);

			// register the score
			sgeGui.totalPoints.setText("" +
					(Integer.parseInt(sgeGui.totalPoints.getText()) +
							sgeGui.score(SelfAwareCircle.getRegionLength(), getColor())));
			sgeGUI.totalPoints.setText("" +
					(Integer.parseInt(sgeGUI.totalPoints.getText()) +
							sgeGUI.score(SelfAwareCircle.getRegionLength(), getColor())));

			// request that the board be shifted appropriately
			sgeGui.shiftCircles();
			sgeGUI.shiftCircles();

			// update tetrising
			sgeGui.updateNumClicks();
			sgeGui.updateTetrising();
			sgeGUI.updateNumClicks();
			sgeGUI.updateTetrising();

		}

@@ -225,22 +240,22 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
		 * @param e the event broadcast by the other button
		 */
		public void rollingOver(CircleRolloverEvent e) {
			if (state == e.getAction() || isCleared()) // i.e. this circle has already been processed
			if (_state == e.getVisibility() || isCleared()) // i.e. this circle has already been processed
				return;

			if (e.getColor().equals(clr)) { // check if the color is the same as mine
				highlightButton(e.getAction());
				_highlightButton(e.getVisibility());
			}
			// in all other cases, only update the text field (i.e. stop the recursion)
			sgeGui.regionPoints.setText(""+(SelfAwareCircle.getRegionLength())); // update the gui text field
			sgeGUI.regionPoints.setText(""+(SelfAwareCircle.getRegionLength())); // update the gui text field
		}

		// ... drawing methods
		public void paint(Graphics g) {
			// set the color, depending on whether this is being highlighted
			switch (state) {
			switch (_state) {
			case highlight:
				g.setColor(hltColor); // change to highlighting color
				g.setColor(_hltColor); // change to highlighting color
				break;
			case plain:
				g.setColor(clr.getColor());      // the default color
Loading