Android Open Source - slider Frame






From Project

Back to project page slider.

License

The source code is released under:

Apache License

If you think the Android project slider 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

/*
 * Created on 21.07.2006 by leck//  ww  w  .  j  av a 2 s.c o m
 *
 */
package de.devisnik.sliding.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import de.devisnik.sliding.IFrame;
import de.devisnik.sliding.IFrameListener;
import de.devisnik.sliding.IHole;
import de.devisnik.sliding.IMove;
import de.devisnik.sliding.IPiece;
import de.devisnik.sliding.MoveFactory;
import de.devisnik.sliding.Point;
import de.devisnik.sliding.ShiftingEvent;

public class Frame implements IFrame {

  public static void main(String[] args) {
    Frame shiftingGame = new Frame(5, 5);
    System.out.println(shiftingGame.toString());
    shiftingGame.moveHole(MoveFactory.UP);
    System.out.println(shiftingGame.toString());
    shiftingGame.moveHole(MoveFactory.UP);
    System.out.println(shiftingGame.toString());
    shiftingGame.moveHole(MoveFactory.LEFT);
    System.out.println(shiftingGame.toString());
    shiftingGame.moveHole(MoveFactory.LEFT);
    System.out.println(shiftingGame.toString());
    shiftingGame.moveHole(MoveFactory.UP);
    System.out.println(shiftingGame.toString());
    shiftingGame.moveHole(MoveFactory.LEFT);
    System.out.println(shiftingGame.toString());
    shiftingGame.moveHole(MoveFactory.DOWN);
    System.out.println(shiftingGame.toString());
  }

  private int itsDimX;
  private int itsDimY;
  private Hole itsHole;
  private Piece[][] itsPieces;

  private List<IFrameListener> itsShiftingGameListeners = new ArrayList<IFrameListener>();

  public Frame(int x, int y) {
    itsDimX = x;
    itsDimY = y;
    createPieces(x, y);
  }

  public void addListener(IFrameListener listener) {
    if (listener == null)
      return;
    itsShiftingGameListeners.add(listener);
  }

  private Piece createPieceAt(int x, int y) {
    return new Piece(x, y, "" + (y * itsDimX + x + 1));
  }

  private void createPieces(int dimX, int dimY) {
    itsPieces = new Piece[dimX][dimY];
    for (int y = 0; y < dimY; y++)
      for (int x = 0; x < dimX; x++)
        itsPieces[x][y] = createPieceAt(x, y);
    itsHole = new Hole(dimX - 1, dimY - 1, " ");
    itsPieces[dimX - 1][dimY - 1] = itsHole;
  }

  private void exchange(Point first, Point second) {
    Piece buffer = itsPieces[first.x][first.y];
    putPieceAt(itsPieces[second.x][second.y], first.x, first.y);
    putPieceAt(buffer, second.x, second.y);
  }

  public boolean execute(IMove move) {
    ShiftingEvent[] event = moveHole(move);
//    fireShiftingEvent(event[1]);
    fireSwap(event[0].getPiece(), event[1].getPiece());
    return true;
  }

  protected final void fireSwap(IPiece left, IPiece right) {
    for (IFrameListener listener : itsShiftingGameListeners)
      listener.handleSwap(left, right);
  }

  protected final void fireShiftingEvents(final ShiftingEvent[] events) {
    for (IFrameListener listener : itsShiftingGameListeners)
      listener.handleShifting(events);
  }

  public IHole getHole() {
    return itsHole;
  }

  public IPiece getPieceAt(int posX, int posY) {
    return itsPieces[posX][posY];
  }

  public Point getSize() {
    return new Point(itsDimX, itsDimY);
  }

  protected final ShiftingEvent[] moveHole(IMove move) {
    Point holePosition = getHole().getPosition().copy();
    Point target = Point.sum(holePosition, move.getX(), move.getY());
    IPiece piece = getPieceAt(target.x, target.y);
    exchange(holePosition, target);
    Point oldPosition = holePosition;
    return new ShiftingEvent[] { new ShiftingEvent(piece, target, oldPosition),
        new ShiftingEvent(itsHole, oldPosition, target) };
  }

  protected final void putPieceAt(Piece piece, int x, int y) {
    itsPieces[x][y] = piece;
    piece.setPosition(x, y);
  }

  public void removeListener(IFrameListener listener) {
    itsShiftingGameListeners.remove(listener);
  }

  @Override
  public String toString() {
    StringBuffer stringBuffer = new StringBuffer();
    for (int y = 0; y < itsDimY; y++) {
      for (int x = 0; x < itsDimX; x++) {
        String label = itsPieces[x][y].getLabel();
        stringBuffer.append(label);
        stringBuffer.append(" ");
      }
      stringBuffer.append("\n");
    }
    return stringBuffer.toString();
  }

  public Iterator<IPiece> iterator() {
    return new PieceIterator(itsPieces);
  }
}




Java Source Code List

de.devisnik.android.sliding.ARandom.java
de.devisnik.android.sliding.FPSCounter.java
de.devisnik.android.sliding.FrameDrawer.java
de.devisnik.android.sliding.ISpeed.java
de.devisnik.android.sliding.ImageCache.java
de.devisnik.android.sliding.ImageFactory.java
de.devisnik.android.sliding.Logger.java
de.devisnik.android.sliding.OpenActivity.java
de.devisnik.android.sliding.OpenPreviewActivity.java
de.devisnik.android.sliding.PropertiesBuilder.java
de.devisnik.android.sliding.RenderHandler.java
de.devisnik.android.sliding.Replayer.java
de.devisnik.android.sliding.Settings.java
de.devisnik.android.sliding.SlidingPreferences.java
de.devisnik.android.sliding.SlidingWallpaper.java
de.devisnik.android.sliding.SpeedFactory.java
de.devisnik.android.sliding.SummaryUpdater.java
de.devisnik.android.sliding.tile.BitmapCachingPieceDrawer.java
de.devisnik.android.sliding.tile.CachingPieceDrawer.java
de.devisnik.android.sliding.tile.IPieceDrawer.java
de.devisnik.android.sliding.tile.ImagePieceDrawer.java
de.devisnik.android.sliding.tile.NumberPieceDrawer.java
de.devisnik.android.sliding.tile.PieceDrawerFactory.java
de.devisnik.android.sliding.tile.TileShifter.java
de.devisnik.android.sliding.tile.TileStore.java
de.devisnik.android.sliding.tile.Tile.java
de.devisnik.sliding.FrameFactory.java
de.devisnik.sliding.FrameScrambler.java
de.devisnik.sliding.FromHomeShiftingEvent.java
de.devisnik.sliding.IFrameListener.java
de.devisnik.sliding.IFrame.java
de.devisnik.sliding.IHole.java
de.devisnik.sliding.IMove.java
de.devisnik.sliding.IPiece.java
de.devisnik.sliding.IRandom.java
de.devisnik.sliding.IRobotFrame.java
de.devisnik.sliding.IShifterListener.java
de.devisnik.sliding.MoveFactory.java
de.devisnik.sliding.Point.java
de.devisnik.sliding.ShiftingEvent.java
de.devisnik.sliding.ToHomeShiftingEvent.java
de.devisnik.sliding.animation.AccelerateDecelerateInterpolator.java
de.devisnik.sliding.animation.AnimationRunner.java
de.devisnik.sliding.animation.Animation.java
de.devisnik.sliding.animation.IInterpolator.java
de.devisnik.sliding.animation.IMovement.java
de.devisnik.sliding.animation.IShiftable.java
de.devisnik.sliding.animation.LinearInterpolator.java
de.devisnik.sliding.animation.MetaShiftable.java
de.devisnik.sliding.animation.ShiftAnimation.java
de.devisnik.sliding.animation.Translation.java
de.devisnik.sliding.impl.Frame.java
de.devisnik.sliding.impl.Hole.java
de.devisnik.sliding.impl.Move.java
de.devisnik.sliding.impl.PieceIterator.java
de.devisnik.sliding.impl.Piece.java
de.devisnik.sliding.impl.RobotFrame.java
de.devisnik.sliding.impl.Scrambler.java