Android Open Source - BoxSorter Random Initial Conf Loader






From Project

Back to project page BoxSorter.

License

The source code is released under:

GNU General Public License

If you think the Android project BoxSorter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.silverhillapps.boxsorter.loader;
/*from   w  w  w. j  a v  a 2s  . c o m*/
import java.util.Random;
import com.silverhillapps.boxsorter.BSApplication;
import com.silverhillapps.boxsorter.conf.Conf;
import com.silverhillapps.boxsorter.entities.Element;
import com.silverhillapps.boxsorter.entities.InitialPositionConfig;
import com.silverhillapps.boxsorter.utils.Utils;

public class RandomInitialConfLoader extends InitialConfLoader {

  // Constants for the correct display of the initial random elements over the canvas
  private int maxWidth;
  private int maxHeight;
  private int correctionFactor;
  
  public RandomInitialConfLoader(){
    maxWidth = BSApplication.getmScreenWidth()-Conf.INITIAL_DIMENSION_ELEMENTS;
    correctionFactor = BSApplication.getmScreenHeight()*Conf.INITIAL_FREE_BOTTOM_SPACE_PERCENTAGE_PER_RANDOM_CREATOR/100;
    maxHeight = BSApplication.getmScreenHeight()-Conf.INITIAL_DIMENSION_ELEMENTS-correctionFactor;
  }
  
  /**
   * Method for creating the initial elements
   */
  @Override
  public InitialPositionConfig loadElements() {
    
    InitialPositionConfig positions = new InitialPositionConfig();
    Element newElement;
    
    int numElements = 0;
    do{
      newElement = getRandomElement();
      
      positions.addElement(newElement);
      numElements++;
    }while(numElements<Conf.INITIAL_ELEMENTS);
    
    return positions;
  }

  
  /**
   * Method used for creating a new random element
   * @return the random element
   */
  private Element getRandomElement(){
    Element newElelement = new Element();
    Random r = new Random();
    
    int xPos = r.nextInt(maxWidth);
    int yPos = r.nextInt(maxHeight);
    
    newElelement.setColour(Utils.randomColor());
    newElelement.setSize(Conf.INITIAL_DIMENSION_ELEMENTS*2);
    newElelement.setxPos(xPos);
    newElelement.setyPos(yPos);
    newElelement.setFigureCode(Conf.INITIAL_FIGURE_CODE);
    
    return newElelement;
    
  }

  /**
   * public method for adding new elements
   */
  @Override
  public Element addElement() {
    return getRandomElement();
  }
}




Java Source Code List

com.silverhillapps.boxsorter.BSApplication.java
com.silverhillapps.boxsorter.BoxSorterActivity.java
com.silverhillapps.boxsorter.conf.Conf.java
com.silverhillapps.boxsorter.conf.Constants.java
com.silverhillapps.boxsorter.entities.Element.java
com.silverhillapps.boxsorter.entities.InitialPositionConfig.java
com.silverhillapps.boxsorter.gesture.BSGestureDetector.java
com.silverhillapps.boxsorter.gesture.BSMoveListener.java
com.silverhillapps.boxsorter.gesture.BSScaleDetector.java
com.silverhillapps.boxsorter.loader.InitialConfLoader.java
com.silverhillapps.boxsorter.loader.JsonAssetsInitialConfLoader.java
com.silverhillapps.boxsorter.loader.LoaderFactory.java
com.silverhillapps.boxsorter.loader.RandomInitialConfLoader.java
com.silverhillapps.boxsorter.subviews.Circle.java
com.silverhillapps.boxsorter.subviews.FigureFactory.java
com.silverhillapps.boxsorter.subviews.Figure.java
com.silverhillapps.boxsorter.subviews.Square.java
com.silverhillapps.boxsorter.utils.Utils.java
com.silverhillapps.boxsorter.views.BoxCanvasView.java