Begin javafx

This commit is contained in:
loic 2016-09-22 20:00:51 +02:00
parent f281a7189c
commit 5446859dee
17 changed files with 681 additions and 103 deletions

2
.idea/compiler.xml generated
View file

@ -26,7 +26,7 @@
</profile> </profile>
</annotationProcessing> </annotationProcessing>
<bytecodeTargetLevel> <bytecodeTargetLevel>
<module name="2048" target="1.5" /> <module name="2048" target="1.6" />
</bytecodeTargetLevel> </bytecodeTargetLevel>
</component> </component>
</project> </project>

95
.idea/misc.xml generated
View file

@ -10,85 +10,6 @@
</list> </list>
</option> </option>
</component> </component>
<component name="ProjectInspectionProfilesVisibleTreeState">
<entry key="Project Default">
<profile-state>
<expanded-state>
<State>
<id />
</State>
<State>
<id>Android &gt; Lint &gt; Correctness</id>
</State>
<State>
<id>Android &gt; Lint &gt; Security</id>
</State>
<State>
<id>Android Lint for Kotlin</id>
</State>
<State>
<id>CFML</id>
</State>
<State>
<id>GPath inspectionsGroovy</id>
</State>
<State>
<id>GeneralJavaScript</id>
</State>
<State>
<id>Google Web Toolkit issues</id>
</State>
<State>
<id>Groovy</id>
</State>
<State>
<id>JPA issues</id>
</State>
<State>
<id>JSP Inspections</id>
</State>
<State>
<id>Java</id>
</State>
<State>
<id>Java EE issues</id>
</State>
<State>
<id>JavaScript</id>
</State>
<State>
<id>Probable bugsJava</id>
</State>
<State>
<id>RESTful Web Service</id>
</State>
<State>
<id>Security issuesJava</id>
</State>
<State>
<id>Spring</id>
</State>
<State>
<id>Spring MVCSpring</id>
</State>
<State>
<id>WebSocket issues</id>
</State>
<State>
<id>XML</id>
</State>
<State>
<id>XPath</id>
</State>
</expanded-state>
<selected-state>
<State>
<id>JpaQlInspection</id>
</State>
</selected-state>
</profile-state>
</entry>
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false"> <component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" /> <OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" /> <OptionsSetting value="true" id="Remove" />
@ -102,20 +23,4 @@
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
<component name="masterDetails">
<states>
<state key="ProjectJDKs.UI">
<settings>
<last-edited>1.8</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project> </project>

124
.idea/uiDesigner.xml generated Normal file
View file

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false"> <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" /> <output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" /> <output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">

12
pom.xml
View file

@ -7,6 +7,18 @@
<groupId>manzerbredes</groupId> <groupId>manzerbredes</groupId>
<artifactId>2048</artifactId> <artifactId>2048</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

View file

@ -0,0 +1,12 @@
package adapter;
/**
* Created by loic on 21/09/16.
*/
public interface IModelAdapter {
void goUp();
void goDown();
void goLeft();
void goRight();
boolean isLoosed();
}

View file

@ -0,0 +1,80 @@
package adapter;
import model.Board;
import observer.IObservable;
import observer.IObserver;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* Created by loic on 21/09/16.
*/
public class ModelAdapter implements IModelAdapter, IObservable{
private Board model;
private Collection<IObserver> observers;
public ModelAdapter(Board model){
this.model=model;
observers = new ArrayList<IObserver>();
}
@Override
public void goUp() {
model.goUp();
model.addRandomNumber();
this.notifyObservers();
}
@Override
public void goDown() {
model.goDown();
model.addRandomNumber();
this.notifyObservers();
}
@Override
public void goLeft() {
model.goLeft();
model.addRandomNumber();
this.notifyObservers();
}
@Override
public void goRight() {
model.goRight();
model.addRandomNumber();
this.notifyObservers();
}
@Override
public boolean isLoosed() {
return this.model.isLoosed();
}
public void addRandomNumber() {
this.model.addRandomNumber();
this.notifyObservers();
}
@Override
public void addObserver(IObserver observer) {
this.observers.add(observer);
}
@Override
public void removeObserver(IObserver observer) {
this.observers.remove(observer);
}
@Override
public void notifyObservers() {
Iterator<IObserver> i=this.observers.iterator();
while(i.hasNext()){
i.next().update();
}
}
}

View file

@ -1,14 +1,44 @@
package app; package app;
/** import adapter.ModelAdapter;
* Created by loic on 21/09/16. import controller.MainWindowController;
*/ import javafx.event.EventHandler;
public class Application { import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
import model.Board;
import model.IModel;
import observer.IObserver;
import java.awt.*;
public class Application extends javafx.application.Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(Application.class.getClassLoader().getResource("JavafxView/MainWindow.fxml"));
Scene scene=new Scene((Parent)fxmlLoader.load());
primaryStage.setTitle("2048");
primaryStage.setScene(scene);
// COnfigure controller
IModel model=new Board(4,4);
ModelAdapter adapter=new ModelAdapter((Board) model);
adapter.addRandomNumber();
MainWindowController controller=fxmlLoader.getController();
adapter.addObserver((IObserver)controller);
controller.loadComponent(adapter,model,scene);
public static void main(String[] args){ primaryStage.show();
System.out.println("Test");
} }
} }

View file

@ -0,0 +1,50 @@
package controller;
import adapter.IModelAdapter;
import adapter.ModelAdapter;
import model.Board;
/**
* Created by loic on 21/09/16.
*/
public class ConsoleController implements IModelAdapter {
private ModelAdapter adapter;
public ConsoleController(ModelAdapter model){
this.adapter=model;
}
public void startGame(){
this.adapter.addRandomNumber();
}
@Override
public void goUp() {
adapter.goUp();
}
@Override
public void goDown() {
adapter.goDown();
}
@Override
public void goLeft() {
adapter.goLeft();
}
@Override
public void goRight() {
adapter.goRight();
}
@Override
public boolean isLoosed() {
return this.adapter.isLoosed();
}
}

View file

@ -0,0 +1,147 @@
package model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
/**
* Created by loic on 21/09/16.
*/
public class Board implements IModel{
private int[][] board;
private Random rand = new Random();
private LineAlgorithm lineAlgorithm;
public Board(int sizeX, int sizeY){
board=new int[sizeY][sizeX];
this.cleanBoard();
}
public void goUp() {
for(int i=0;i<this.board[0].length;i++){
int[] column=this.lineAlgorithm.reverseLine(this.getColumn(i));
this.setColumn(this.lineAlgorithm.reverseLine(this.lineAlgorithm.mergeRight(column)), i);
}
}
public void goDown() {
for(int i=0;i<this.board[0].length;i++){
int[] column=this.getColumn(i);
this.setColumn(this.lineAlgorithm.mergeRight(column), i);
}
}
public void goLeft() {
for(int i=0;i<this.board.length;i++){
int[] tmp=this.lineAlgorithm.reverseLine(board[i]);
tmp=this.lineAlgorithm.mergeRight(tmp);
tmp=this.lineAlgorithm.reverseLine(tmp);
this.board[i]=tmp;
}
}
public void goRight() {
for(int i=0;i<this.board.length;i++){
this.board[i]=this.lineAlgorithm.mergeRight(board[i]);
}
}
private void cleanBoard(){
for(int i=0;i<this.board.length;i++) {
this.board[i]=this.lineAlgorithm.clearLine(this.board[i]);
}
}
private int[] getColumn(int index){
int[] column=new int[this.board.length];
for(int i=0;i<this.board.length;i++){
column[i]=this.board[i][index];
}
return column;
}
private void setColumn(int[] column, int index){
for(int i=0;i<this.board[index].length;i++){
this.board[i][index]=column[i];
}
}
public void addRandomNumber(){
Collection<Integer[]> choices=new ArrayList<Integer[]>();
for(int i=0;i<this.board.length;i++) {
for (int j = 0; j < this.board[i].length; j++) {
if(this.board[i][j]==-1){
choices.add(new Integer[]{i,j});
}
}
}
if(choices.size()>0){
int index=0;
if(choices.size()>1){
index=rand.nextInt(choices.size()-1) + 0;
}
Integer[] xy=(Integer[])choices.toArray()[index];
this.board[xy[0]][xy[1]]=2;
}
}
private boolean boardsIsEquals(int[][] board1, int[][] board2){
if(board1.length!=board1.length){
return false;
}
else {
for (int i=0;i<board1.length;i++){
if(!this.lineAlgorithm.linesIsEquals(board1[i],board2[i])){
return false;
}
}
}
return true;
}
@Override
public int[][] getBoard() {
return this.getCloneOfBoard();
}
public boolean isLoosed() {
int[][] copyBoard=this.getCloneOfBoard();
this.goDown();
if(boardsIsEquals(copyBoard, this.board)) {
this.goUp();
if (boardsIsEquals(copyBoard, this.board)) {
this.goRight();
if (boardsIsEquals(copyBoard, this.board)) {
this.goLeft();
if (boardsIsEquals(copyBoard, this.board)) {
return true;
}
}
}
}
this.board=copyBoard;
return false;
}
private int[][] getCloneOfBoard(){
int[][] copyBoard=new int[this.board.length][this.board[0].length];
for(int i=0;i<this.board.length;i++){
copyBoard[i]=this.board[i].clone();
}
return copyBoard;
}
}

View file

@ -0,0 +1,8 @@
package model;
/**
* Created by loic on 21/09/16.
*/
public interface IModel {
int[][] getBoard();
}

View file

@ -0,0 +1,91 @@
package model;
/**
* Created by loic on 21/09/16.
*/
public class LineAlgorithm {
public static int[] mergeRight(int[] line){
line=gravityRight(line);
for(int i=(line.length-1);i>=0;i--){
if(i>0){
int a=line[i];
int b=line[i-1];
if(a==b && a!=-1){
line[i]=a+b;
line[i-1]=-1;
i--;
}
}
}
line=gravityRight(line);
return line;
}
private static int[] gravityRight(int[] line){
for(int i=0;i<line.length;i++) {
for (int j = (line.length - 1); j >= 0; j--) {
int a = line[j];
if (j > 0) {
int b = line[j - 1];
if (a == -1) {
line[j] = b;
line[j - 1] = -1;
}
}
}
}
return line;
}
public static int[] clearLine(int[] line){
for(int i=0;i<line.length;i++){
line[i]=-1;
}
return line;
}
public static int[] reverseLine(int[] line){
int[] reversedLine=new int[line.length];
int j=0;
for(int i=(line.length-1);i>=0;i--){
reversedLine[j]=line[i];
j++;
}
return reversedLine;
}
public static boolean linesIsEquals(int[] line1, int[] line2){
if(line1.length!=line2.length){
return false;
}
else {
for (int i=0;i<line1.length;i++){
if(line1[i]!=line2[i]){
return false;
}
}
}
return true;
}
public static void printLine(int[] line){
System.out.println("----------");
for(int i=0;i<line.length;i++){
System.out.print(line[i]);
}
System.out.println("\n----------");
}
}

View file

@ -0,0 +1,10 @@
package observer;
/**
* Created by loic on 21/09/16.
*/
public interface IObservable {
void addObserver(IObserver object);
void removeObserver(IObserver object);
void notifyObservers();
}

View file

@ -0,0 +1,8 @@
package observer;
/**
* Created by loic on 21/09/16.
*/
public interface IObserver {
void update();
}

View file

@ -0,0 +1,68 @@
package view;
import controller.ConsoleController;
import adapter.IModelAdapter;
import model.IModel;
import observer.IObserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by loic on 21/09/16.
*/
public class ConsoleView implements IView, IObserver{
private ConsoleController controller;
private IModel model;
@Override
public void loadComponent(IModelAdapter controller, IModel model) {
this.controller= (ConsoleController) controller;
this.model=model;
}
@Override
public void show() {
this.controller.startGame();
}
@Override
public void update() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[][] board;
board = this.model.getBoard();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.print("Enter U or D or L or R :");
String input = null;
try {
input = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if(input.equals("U")){
controller.goUp();
}
else if(input.equals("D")){
controller.goDown();
}
else if(input.equals("L")){
controller.goLeft();
}
else if(input.equals("R")){
controller.goRight();
}
}
}

View file

@ -0,0 +1,13 @@
package view;
import adapter.IModelAdapter;
import model.IModel;
/**
* Created by loic on 21/09/16.
*/
public interface IView {
void loadComponent(IModelAdapter controller, IModel model);
void show();
}

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.canvas.Canvas?>
<BorderPane fx:controller="controller.MainWindowController"
xmlns:fx="http://javafx.com/fxml" prefWidth="800" prefHeight="800">
<top>
</top>
<center>
<Canvas fx:id="boardCanvas" height="500" width="500" />
</center>
</BorderPane>