15puzzles GWT way
15puzzles GWT implementation source code
Code does not contains auto generated by plugin and framework parts .
Files: index.html,
gwt15puzzles.css,
gwt15puzzles.java,
FifteenPuzzles.java,
Puzzle.java,
FreeSpace.java
Html code below is not completely displayed and also not formatted well. Please download whole project.
index.html
gwt15puzzles.css
.page{
color: #000000;
margin: 0px;
padding: 0px;
font-family: fantasy;
font-size: 24px;
font-weight: bold;
background-color: Silver;
}
.title{
position: absolute;
color: #000000;
font-family: fantasy;
font-size: 18px;
}
.box{
width: 200px;
height: 200px;
position: absolute;
top: 92px;
left: 92px;
/*z-index:-2;*/
visibility: visible;
display: block;
border-top: 8px double Black;
border-right: 8px double black;
border-bottom: 8px double Black;
border-left: 8px double Black;
background-color: Gray;
}
.puzzle{
position: absolute;
width: 42px;
height: 42px;
visibility: visible;
z-index: 2;
color: Black;
border: 4px outset orange;
font-family: Verdana,Arial,Helvetica,sans-serif;
font-size: 24px;
font-style: normal;
font-variant: normal;
font-weight: bold;
line-height: 43px;
text-align: center;
vertical-align: middle;
background-color: Yellow;
}
.freeSpace{
width: 48px;
height: 48px;
visibility: visible;
border: 1px dotted yellow;
background-color: Gray;
}
.buttons{
position: absolute;
top: 100px;
left: 350px;
width: 100px;
height: 300px;
}
.button{
width: 100px;
height: 30px;
color: Black;
margin: 30px;
border: 5px outset #ffcc66;
font-family: Verdana,Arial,Helvetica,sans-serif;
font-size: 14px;
font-weight: bold;
line-height: 30px;
text-align: center;
text-indent: 1px;
vertical-align: bottom;
background-color: #ffff66;
}
.button-clicked{
width: 100px;
height: 30px;
color: dimgray;
margin: 30px;
border: 5px inset #8c4e9b;
font-family: Verdana,Arial,Helvetica,sans-serif;
font-size: 14px;
font-weight: bold;
line-height: 30px;
text-align: center;
text-indent: 1px;
vertical-align: bottom;
background-color: #ccff33;
}
.gwt-iframe{
top: 0px;
left: 0px;
width: 600px;
height: 500px;
visibility: visible;
border-right-style: none;
border-left-style: none;
border-bottom-style: none;
border-top-style: none;
border-right-color: silver;
border-left-color: silver;
border-bottom-color: silver;
border-top-color: silver;
}
.header-menu{
position: absolute;
top: 0px;
left: 500px;
width: 500px;
height: 50px;
visibility: visible;
}
.header-link{
font-style: normal;
font-family: fantasy;
font-size: 18px;
color: #000000;
margin-left: 8px;
margin-right: 8px;
padding-left: 8px;
padding-right: 8px;
}
.ads{
border-right-style: none;
border-left-style: none;
border-bottom-style: none;
border-top-style: none;
border-right-color: #e08989;
border-left-color: #e08989;
border-bottom-color: #e08989;
border-top-color: #e08989;
border-right-width: 1px;
border-left-width: 1px;
border-bottom-width: 1px;
border-top-width: 1px;
position: absolute;
top: 92px;
left: 550px;
width: 300px;
height: 350px;
visibility: visible;
}
.others-ways{
position: absolute;
top: 92px;
left: 860px;
color: #000000;
}
.other-way-link{
font-size: 14px;
font-style: normal;
font-weight: bold;
color: #000000;
line-height: 40px;
}
gwt15puzzles.java
package com.javaghost.gwt15puzzles.client;
import com.google.gwt.core.client.EntryPoint;
/**
* Entry point classes define onModuleLoad().
*/
public class gwt15puzzles implements EntryPoint {
FifteenPuzzles fifteenPuzzles;
public void onModuleLoad() {
RootPanel rootPanel = RootPanel.get();
rootPanel.setStyleName("page");
{
Box box = new Box();
box.setStyleName("box");
rootPanel.add(box);
box.setSize("200", "200");
fifteenPuzzles = FifteenPuzzles.createInstance(box);
fifteenPuzzles.init();
fifteenPuzzles.makeDraggable();
AbsolutePanel buttonsPanel = new AbsolutePanel();
buttonsPanel.setStyleName("buttons");
rootPanel.add(buttonsPanel, 357, 97);
buttonsPanel.setSize("134px", "300px");
Button scrableButton = new Button("Scramble");
buttonsPanel.add(scrableButton, -12, 5);
scrableButton.setSize("100px", "30px");
scrableButton.setStyleName("button");
Button sortButton = new Button("Sort");
buttonsPanel.add(sortButton, -12, 83);
sortButton.setSize("100px", "30px");
sortButton.setStyleName("button");
Label label = new Label("15puzzles GWT way");
label.setStyleName("title");
rootPanel.add(label, 92, 26);
scrableButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
fifteenPuzzles.scramble15puzzles();
}
});
sortButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
fifteenPuzzles.sort15puzzles();
}
});
}
}
}
FifteenPuzzles.java
package com.javaghost.gwt15puzzles.client;
import com.google.gwt.user.client.ui.Label;
import com.javaghost.gwt15puzzles.client.Puzzle.DragDirection;
public class FifteenPuzzles {
public FreeSpace freeSpace;
public Puzzle lastDraggedPuzzle;
public Puzzle[] puzzles;
public String dragDirection;
public Box box;
private static FifteenPuzzles fifteenPuzzles;
public static FifteenPuzzles createInstance(Box box) {
if (fifteenPuzzles == null) {
fifteenPuzzles = new FifteenPuzzles(box);
}
return fifteenPuzzles;
}
public static FifteenPuzzles getInstance() {
return fifteenPuzzles;
}
public FifteenPuzzles(Box box) {
this.box = box;
}
public void init() {
puzzles = new Puzzle[16];
for (int i = 1; i <= 16; i++) {
int x = 0;
if (i < 5) {
x = (i - 1) * 50;
} else if (5 <= i && i < 9) {
x = (i - 5) * 50;
} else if (9 <= i && i < 13) {
x = (i - 9) * 50;
} else if (13 <= i && i <= 16) {
x = (i - 13) * 50;
}
int y = 0;
if (i < 5) {
y = 0;
} else if (5 <= i && i < 9) {
y = 50;
} else if (9 <= i && i < 13) {
y = 100;
} else if (13 <= i && i <= 16) {
y = 150;
}
Puzzle puzzle = null;
if (i == 16) {
// freeSpace = new FreeSpace(x , y , i);
} else {
puzzle = new Puzzle(i, x, y, i);
}
if (i == 16) {
appendFreeSpace(x, y, i);
} else {
appendPuzzle(puzzle);
}
}
}
public void makeDraggable() {
for (int i = 1; i < 16; i++) {
if (freeSpace.x == puzzles[i].x || freeSpace.y == puzzles[i].y) {
if (puzzles[i].position == freeSpace.position - 1) {
puzzles[i].makeDraggable(DragDirection.RIGHT);
} else if (puzzles[i].position == freeSpace.position + 1) {
puzzles[i].makeDraggable(DragDirection.LEFT);
} else if (puzzles[i].position == freeSpace.position + 4) {
puzzles[i].makeDraggable(DragDirection.UP);
} else if (puzzles[i].position == freeSpace.position - 4) {
puzzles[i].makeDraggable(DragDirection.DOWN);
}
}
}
}
public void appendPuzzle(Puzzle puzzle) {
puzzles[puzzle.number] = puzzle;
puzzle.setStyleName("puzzle");
Label label = new Label(puzzle.number + "");
puzzle.add(label);
box.add(puzzle, puzzle.x, puzzle.y);
}
public void appendFreeSpace(int x, int y, int position) {
freeSpace = new FreeSpace(x, y, position);
freeSpace.setStyleName("freeSpace");
box.add(freeSpace, freeSpace.x, freeSpace.y);
}
public void scramble15puzzles() {
while (box.getChildren().size() > 0) {
box.remove(0);
}
int xTemp = freeSpace.x;
int yTemp = freeSpace.y;
int positionTemp = freeSpace.position;
int n = (int) Math.ceil(Math.random() * 15);
Puzzle randomPuzzle = puzzles[n];
freeSpace.x = randomPuzzle.x;
freeSpace.y = randomPuzzle.y;
freeSpace.position = randomPuzzle.position;
randomPuzzle.x = xTemp;
randomPuzzle.y = yTemp;
randomPuzzle.position = positionTemp;
appendFreeSpace(freeSpace.x, freeSpace.y, freeSpace.position);
for (int i = 0; i < 20; i++) {
Puzzle randomPuzzle1 = puzzles[(int) Math.ceil(Math.random() * 15)];
Puzzle randomPuzzle2 = puzzles[(int) Math.ceil(Math.random() * 15)];
xTemp = randomPuzzle1.x;
yTemp = randomPuzzle1.y;
positionTemp = randomPuzzle1.position;
randomPuzzle1.x = randomPuzzle2.x;
randomPuzzle1.y = randomPuzzle2.y;
randomPuzzle1.position = randomPuzzle2.position;
randomPuzzle2.x = xTemp;
randomPuzzle2.y = yTemp;
randomPuzzle2.position = positionTemp;
}
for (int i = 1; i < 16; i++) {
appendPuzzle(puzzles[i]);
}
this.makeDraggable();
}
public void sort15puzzles() {
while (box.getChildren().size() > 0) {
box.remove(0);
}
init();
makeDraggable();
}
}
Puzzle.java
package com.javaghost.gwt15puzzles.client;
import com.google.gwt.user.client.DOM;
@SuppressWarnings("deprecation")
public class Puzzle extends AbsolutePanel implements SourcesMouseEvents, EventPreview {
private FifteenPuzzles fifteenPuzzles;
public int number;
public int position;
public int x; // left
public int y; // top
private DragDirection dragDirection;
private int normalPositionX;
private int normalPositionY;
public boolean alreadyDragged;
public int lastDraggedPuzzleNormalX;
public int lastDraggedPuzzleNormalY;
private MouseListenerCollection mouseListeners = new MouseListenerCollection();
public Puzzle(int number, int x, int y, int position) {
fifteenPuzzles = FifteenPuzzles.getInstance();
this.x = x;
this.y = y;
this.position = position;
this.number = number;
DOM.sinkEvents(getElement(), DOM.getEventsSunk(getElement())
| Event.MOUSEEVENTS);
DOM.addEventPreview(this);
addMouseListener(new DraggableMouseListener());
}
public void makeDraggable(DragDirection dragDirection) {
this.dragDirection = dragDirection;
normalPositionX = x;
normalPositionY = y;
}
public void addMouseListener(MouseListener l) {
mouseListeners.add(l);
}
public void removeMouseListener(MouseListener l) {
mouseListeners.remove(l);
}
public void onBrowserEvent(Event event) {
switch (DOM.eventGetType(event)) {
case Event.ONMOUSEDOWN:
case Event.ONMOUSEUP:
case Event.ONMOUSEMOVE:
//case Event.ONMOUSEOVER:
case Event.ONMOUSEOUT:
mouseListeners.fireMouseEvent(this, event);
break;
}
}
public boolean onEventPreview(Event event) {
if (DOM.eventGetType(event) == Event.ONMOUSEDOWN &&
DOM.isOrHasChild(getElement(), DOM.eventGetCurrentTarget(event))) {
DOM.eventPreventDefault(event);
}
// Always returning true as we don't want to cancel
// the event, just to prevent the default behaviour.
return true;
}
private class DraggableMouseListener extends MouseListenerAdapter {
private boolean dragging = false;
private int dragStartX;
private int dragStartY;
public void onMouseDown(Widget sender, int x, int y) {
if (dragDirection == null){
return;
}
dragging = true;
// capturing the mouse to the dragged widget.
DOM.setCapture(getElement());
dragStartX = x;
dragStartY = y;
markLastDraggedPuzzle();
}
private void markLastDraggedPuzzle () {
if (!alreadyDragged) {
lastDraggedPuzzleNormalX = Puzzle.this.x;
lastDraggedPuzzleNormalY = Puzzle.this.y;
alreadyDragged = true;
}
fifteenPuzzles.lastDraggedPuzzle = new Puzzle(Puzzle.this.number , lastDraggedPuzzleNormalX , lastDraggedPuzzleNormalY , Puzzle.this.position);
}
public void onMouseUp(Widget sender, int x, int y) {
dragging = false;
DOM.releaseCapture(getElement());
if (dragDirection == DragDirection.RIGHT && x > fifteenPuzzles.freeSpace.x - 20) {
drop();
} else if (dragDirection == DragDirection.LEFT && x < fifteenPuzzles.freeSpace.x + 20) {
drop();
} else if (dragDirection == DragDirection.UP && y < fifteenPuzzles.freeSpace.y + 20) {
drop();
} else if (dragDirection == DragDirection.DOWN && y > fifteenPuzzles.freeSpace.y - 20) {
drop();
}
}
public void onMouseMove(Widget sender, int x, int y) {
int newX = normalPositionX;
int newY = normalPositionY;
if (dragging) {
// we don't want the widget to go off-screen, so the top/left
// values should always remain be positive.
if (dragDirection == DragDirection.RIGHT) {
if (x >= normalPositionX && x <= normalPositionX + 50 ){
newX = x;
} else if (x > normalPositionX + 50) {
newX = normalPositionX + 50;
}
//newX = Math.max(0, x + getAbsoluteLeft() - dragStartX);
} else if (dragDirection == DragDirection.LEFT) {
if (x >= normalPositionX - 50 && x <= normalPositionX ){
newX = x;
} else if (x < normalPositionX - 50) {
newX = normalPositionX - 50;
}
//newX = Math.max(0, x + getAbsoluteLeft() - dragStartX);
} else if (dragDirection == DragDirection.UP) {
if (y >= normalPositionY - 50 && y <= normalPositionY ){
newY = y;
} else if (y < normalPositionY - 50) {
newY = normalPositionY;
}
//newY = Math.max(0, y + getAbsoluteTop() - dragStartY);
} else if (dragDirection == DragDirection.DOWN) {
if (y >= normalPositionY && y <= normalPositionY + 50){
newY = y;
} else if (y > normalPositionY + 50) {
newY = normalPositionY + 50;
}
//newY = Math.max(0, y + getAbsoluteTop() - dragStartY);
}
DOM.setStyleAttribute(getElement(), "left", newX + "");
DOM.setStyleAttribute(getElement(), "top", newY + "");
}
}
}
public void drop(){
alreadyDragged = false;
fifteenPuzzles.box.remove(fifteenPuzzles.freeSpace);
int xTemp = fifteenPuzzles.freeSpace.x;
int yTemp = fifteenPuzzles.freeSpace.y;
int positionTemp = fifteenPuzzles.freeSpace.position;
fifteenPuzzles.freeSpace.x = fifteenPuzzles.lastDraggedPuzzle.x;
fifteenPuzzles.freeSpace.y = fifteenPuzzles.lastDraggedPuzzle.y;
fifteenPuzzles.freeSpace.position = fifteenPuzzles.lastDraggedPuzzle.position;
fifteenPuzzles.lastDraggedPuzzle.x = xTemp;
fifteenPuzzles.lastDraggedPuzzle.y = yTemp;
fifteenPuzzles.lastDraggedPuzzle.position = positionTemp;
fifteenPuzzles.puzzles[fifteenPuzzles.lastDraggedPuzzle.number] = fifteenPuzzles.lastDraggedPuzzle;
//destroy all draggable and dropable
fifteenPuzzles.appendFreeSpace(fifteenPuzzles.freeSpace.x , fifteenPuzzles.freeSpace.y , fifteenPuzzles.freeSpace.position);
fifteenPuzzles.box.remove(this);
Puzzle newPuzzle = new Puzzle(fifteenPuzzles.lastDraggedPuzzle.number , xTemp , yTemp , positionTemp);
fifteenPuzzles.appendPuzzle(newPuzzle);
fifteenPuzzles.makeDraggable();
}
public enum DragDirection {
LEFT, RIGHT, UP, DOWN;
}
}
FreeSpace.java
package com.javaghost.gwt15puzzles.client;
import com.google.gwt.user.client.ui.AbsolutePanel;
public class FreeSpace extends AbsolutePanel {
public int position;
public int x; // left
public int y; // top
public FreeSpace(int x, int y, int position) {
this.x = x;
this.y = y;
this.position = position;
}
}
Related posts:
- 15puzzles Flex way
- 15puzzles ExtJs way
- 15puzzles jQuery way
- 15puzzles YUI way
- TOP COUNTRIES WITH THE HIGHEST INTERNET PENETRATION RATE
- 5 ways to 15puzzles
- Top internet languages ISO codes























