Skip to content
Snippets Groups Projects
Commit 4b3ec7c9 authored by LABOUREL Arnaud's avatar LABOUREL Arnaud
Browse files

Première version du template

parent 1efa5a33
Branches
No related tags found
No related merge requests found
Pipeline #2340 passed
Showing
with 330 additions and 274 deletions
......@@ -15,7 +15,7 @@ repositories {
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter-api:5.8.1',
'org.assertj:assertj-core:3.21.0')
testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine:5.8.1')
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
......@@ -23,5 +23,5 @@ test {
}
application {
mainClassName = "Main"
mainClassName = "main.MainApp"
}
\ No newline at end of file
rootProject.name = 'image'
rootProject.name = 'wang-tile'
import javafx.scene.paint.Color;
/**
* Created by Arnaud Labourel on 02/10/2018.
*/
public class ByteGrayColor implements GrayColor {
private static final int MINIMUM_GRAY_LEVEL = 0;
private static final int MAXIMUM_GRAY_LEVEL = 255;
private static final int OPACITY = 1;
private final int grayLevel;
public ByteGrayColor(){
this.grayLevel = MINIMUM_GRAY_LEVEL;
}
public ByteGrayColor(int grayLevel) {
// TODO : Corriger l'initialisation de la propriété grayLevel de l'instance.
this.grayLevel = 0;
}
public ByteGrayColor(double luminosity) {
// TODO : Corriger l'initialisation de la propriété grayLevel de l'instance.
this.grayLevel = 0;
}
@Override
public double getLuminosity() {
// TODO : Retourner la luminosité de la couleur (entre 0 noir et 1 blanc)
return 0;
}
@Override
public Color getColor(){
double component = grayLevel / (double) MAXIMUM_GRAY_LEVEL;
return new Color(component, component, component, OPACITY);
}
@Override
public int compareTo(GrayColor o) {
// TODO : Retourner la différence de niveau de gris.
return 0;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
ByteGrayColor color = (ByteGrayColor) o;
return this.compareTo(color) == 0;
}
}
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.PixelWriter;
import java.net.URL;
import java.util.ResourceBundle;
/**
* Created by Arnaud Labourel on 04/10/2018.
*/
public class Display implements Initializable {
@FXML
private Canvas canvas;
MatrixGrayImage image;
@Override
public void initialize(URL location, ResourceBundle resources) {
this.image = MatrixGrayImage.createImageFromPGMFile("images/luminy.pgm");
// TODO : Ajouter les transformations d'image.
render();
}
public void render() {
int pixelWidth = image.getWidth();
int pixelHeight = image.getHeight();
canvas.setWidth(pixelWidth);
canvas.setHeight(pixelHeight);
GraphicsContext graphicsContext = canvas.getGraphicsContext2D();
PixelWriter pixelWriter = graphicsContext.getPixelWriter();
for (int i = 0; i < pixelWidth; i++) {
for (int j = 0; j < pixelHeight; j++) {
renderPixel(i,j, pixelWriter);
}
}
}
private void renderPixel(int x, int y, PixelWriter pixelWriter) {
pixelWriter.setColor(x, y, image.getPixelColor(x, y));
}
}
import javafx.scene.paint.Color;
/**
* Created by Arnaud Labourel on 04/10/2018.
* Interface correspondant à une couleur de gris.
*/
public interface GrayColor extends Comparable<GrayColor> {
double getLuminosity();
Color getColor();
}
/**
* Created by Arnaud Labourel on 04/10/2018.
*/
public interface GrayImage extends Image {
void setPixel(GrayColor gray, int x, int y);
GrayColor getPixelGrayColor(int x, int y);
}
import javafx.scene.paint.Color;
/**
* Created by Arnaud Labourel on 02/10/2018.
*/
public interface Image {
Color getPixelColor(int x, int y);
int getWidth();
int getHeight();
}
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application
{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws IOException {
Parent root =FXMLLoader.load(getClass().getResource("fxml/Display.fxml"));
primaryStage.setTitle("Image display");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
import javafx.scene.paint.Color;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Created by Arnaud Labourel on 02/10/2018.
*/
public class MatrixGrayImage implements GrayImage {
private final GrayColor[][] pixels;
private final int width;
private final int height;
@Override
public void setPixel(GrayColor gray, int x, int y) {
// TODO : Compléter la méthode pour modifier le pixel.
}
@Override
public GrayColor getPixelGrayColor(int x, int y) {
// TODO : Changer les instructions pour retourner le bon pixel.
return new ByteGrayColor();
}
@Override
public Color getPixelColor(int x, int y) {
// TODO : Changer les instructions pour retourner la couleur du pixel.
return Color.WHITE;
}
@Override
public int getWidth() {
// TODO : Changer les instructions pour retourner la largeur de l'image.
return 600;
}
@Override
public int getHeight() {
// TODO : Changer les instructions pour retourner la hauteur de l'image.
return 400;
}
public MatrixGrayImage(int width, int height){
/* TODO : Modifier les instructions pour initialiser correctement
les propriétés de l'instance.
*/
this.width = 0;
this.height = 0;
this.pixels = null;
}
public static MatrixGrayImage createImageFromPGMFile(String fileName) {
// NE PAS MODIFIER !
InputStream file = ClassLoader.getSystemResourceAsStream(fileName);
Scanner scan = new Scanner(file);
scan.nextLine();
scan.nextLine();
int width = scan.nextInt();
int height = scan.nextInt();
MatrixGrayImage result = new MatrixGrayImage(width, height);
scan.nextInt();
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++) {
GrayColor color = new ByteGrayColor(scan.nextInt());
result.setPixel(color, x, y);
}
}
return result;
}
public void writeIntoPGMFormat(String fileName){
// NE PAS MODIFIER !
try {
FileWriter fileWriter = new FileWriter(fileName);
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.println("P2");
printWriter.println("# CREATOR: TP3 Version 1.0");
printWriter.printf("%d %d\n",this.width, this.height);
printWriter.println(pgmCodeOfGrayColor(pixels[0][0]));
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++) {
printWriter.println(pgmCodeOfGrayColor(getPixelGrayColor(x,y)));
}
}
printWriter.close();
}
catch (Exception e){
e.printStackTrace();
}
}
private static final int PGM_MAXIMUM_CODE = 255;
private int pgmCodeOfGrayColor(GrayColor pixelGrayColor) {
return (int) (pixelGrayColor.getLuminosity() * (double) PGM_MAXIMUM_CODE);
}
}
package controller;
import javafx.fxml.FXML;
import javafx.scene.paint.Color;
import model.*;
import view.GridTileCanvas;
import java.util.List;
import java.util.Random;
public class GridController {
Random random = new Random();
@FXML
public GridTileCanvas gridTileCanvas;
public void initialize() {
clearGrid();
}
public void fillGrid(TileGenerator tileGenerator) {
gridTileCanvas.fillGrid(tileGenerator);
}
public void drawGrid() {
gridTileCanvas.update();
}
public void clearGrid(){
fillGrid(new EmptyTileGenerator());
}
public void updateGrid(TileGenerator tileGenerator){
clearGrid();
fillGrid(tileGenerator);
drawGrid();
}
@FXML
public void updateEmptyUniformTile(){
updateGrid(new EmptyTileGenerator());
}
@FXML
public void updateRedUniformTile(){
// TODO : uncomment the code to test UniformTileGenerator
// updateGrid(new UniformTileGenerator(new InternalSide(Color.RED)));
}
@FXML
public void updateRandomColorUniformTile(){
// TODO : uncomment the code to test RandomUniformTileGenerator
// updateGrid(new RandomUniformTileGenerator(List.of(Color.RED, Color.BLUE, Color.BLACK), random));
}
@FXML
public void updateRandomTruchetTile(){
// TODO : uncomment the code to test RandomRotatedTruchetTileGenerator
// updateGrid(new RandomRotatedTruchetTileGenerator(Color.RED, Color.BLUE, random));
}
@FXML
public void updateRandomWangTile() {
// TODO : uncomment the code to test RandomWangTileGenerator
// updateGrid(new RandomWangTileGenerator(List.of(Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW), random));
}
@FXML
public void updateRandomConstrainedWangTile() {
// TODO : uncomment the code to test RandomConstrainedWangTileGenerator
// updateGrid(new RandomConstrainedWangTileGenerator(List.of(Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW), random));
}
@FXML
public void updateConstrainedTruchetTile() {
// TODO : uncomment the code to test ConstrainedRotatedTruchetTileGenerator
// updateGrid(new ConstrainedRotatedTruchetTileGenerator(Color.RED, Color.BLUE, random));
}
@FXML
public void updateRandomWangTileSet() {
// TODO : uncomment the code to test RandomTileSetGenerator
/*
updateGrid(new RandomTileSetGenerator(List.of(
new WangTile(new Side[]{new InternalSide(Color.RED), new InternalSide(Color.BLUE), new InternalSide(Color.RED), new InternalSide(Color.RED)}),
new WangTile(new Side[]{new InternalSide(Color.RED), new InternalSide(Color.RED), new InternalSide(Color.BLUE), new InternalSide(Color.BLUE)}),
new WangTile(new Side[]{new InternalSide(Color.RED), new InternalSide(Color.GREEN), new InternalSide(Color.GREEN), new InternalSide(Color.GREEN)}),
new WangTile(new Side[]{new InternalSide(Color.BLUE), new InternalSide(Color.GREEN), new InternalSide(Color.RED), new InternalSide(Color.BLUE)}),
new WangTile(new Side[]{new InternalSide(Color.BLUE), new InternalSide(Color.BLUE), new InternalSide(Color.BLUE), new InternalSide(Color.RED)}),
new WangTile(new Side[]{new InternalSide(Color.BLUE), new InternalSide(Color.RED), new InternalSide(Color.GREEN), new InternalSide(Color.GREEN)}),
new WangTile(new Side[]{new InternalSide(Color.GREEN), new InternalSide(Color.GREEN), new InternalSide(Color.BLUE), new InternalSide(Color.GREEN)}),
new WangTile(new Side[]{new InternalSide(Color.GREEN), new InternalSide(Color.RED), new InternalSide(Color.RED), new InternalSide(Color.RED)}),
new WangTile(new Side[]{new InternalSide(Color.GREEN), new InternalSide(Color.BLUE), new InternalSide(Color.GREEN), new InternalSide(Color.BLUE)})), random));
*/
}
}
package main;
import controller.GridController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.io.IOException;
public class MainApp extends Application {
private Stage primaryStage;
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Wang tiles");
initRootLayout();
}
public void initRootLayout() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/GridCanvas.fxml"));
AnchorPane rootLayout = loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
GridController controller = loader.getController();
controller.initialize();
controller.drawGrid();
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
package model;
public enum CardinalDirection {
NORTH(-1, 0),
EAST(0, 1),
SOUTH(1, 0),
WEST(0, -1);
public static final int NUMBER_OF_DIRECTIONS = values().length;
public final int deltaRow;
public final int deltaColumn;
CardinalDirection(int deltaRow, int deltaColumn) {
this.deltaRow = deltaRow;
this.deltaColumn = deltaColumn;
}
public CardinalDirection oppositeDirection(){
return switch (this){
case EAST -> WEST;
case WEST -> EAST;
case NORTH -> SOUTH;
case SOUTH -> NORTH;
};
}
}
package model;
import java.util.Iterator;
public class EmptyGrid implements Grid{
private final int numberOfRows;
private final int numberOfColumns;
public EmptyGrid(int numberOfRows, int numberOfColumns) {
this.numberOfRows = numberOfRows;
this.numberOfColumns = numberOfColumns;
}
@Override
public Square getSquare(int rowIndex, int columnIndex) {
return EmptySquare.EMPTY_SQUARE;
}
@Override
public int getNumberOfRows() {
return numberOfRows;
}
@Override
public int getNumberOfColumns() {
return numberOfColumns;
}
@Override
public void fill(TileGenerator tileGenerator) {
}
@Override
public Iterator<Square> iterator() {
return new Iterator<>() {
@Override
public boolean hasNext() {
return false;
}
@Override
public Square next() {
return EmptySquare.EMPTY_SQUARE;
}
};
}
}
package model;
import javafx.scene.paint.Color;
import java.util.List;
public class EmptySide implements Side {
public static final Side EMPTY_SIDE = new EmptySide();
private EmptySide(){}
@Override
public boolean accept(Side side) {
return true;
}
@Override
public List<Side> compatibleSides(List<Side> sides) {
return sides;
}
@Override
public Color color() {
return Color.WHITE;
}
}
package model;
import java.util.List;
public class EmptySquare implements Square{
public static final Square EMPTY_SQUARE= new EmptySquare();
private EmptySquare(){}
@Override
public boolean accept(Tile tile) {
return true;
}
@Override
public void put(Tile tile) {}
@Override
public Tile getTile() {
return EmptyTile.EMPTY_TILE;
}
@Override
public List<Tile> compatibleTiles(List<Tile> tiles) {
return tiles;
}
@Override
public List<Side> compatibleSides(List<Side> sides, CardinalDirection direction) {
return sides;
}
@Override
public Square getNeighbor(CardinalDirection direction) {
return EMPTY_SQUARE;
}
@Override
public void setNeighbor(Square neighbor, CardinalDirection direction) { }
}
package model;
public class EmptyTile implements Tile{
public static Tile EMPTY_TILE = new EmptyTile();
private EmptyTile(){}
@Override
public Side side(CardinalDirection direction) {
return EmptySide.EMPTY_SIDE;
}
}
package model;
public class EmptyTileGenerator implements TileGenerator{
@Override
public Tile nextTile(Square square) {
return EmptyTile.EMPTY_TILE;
}
}
package model;
public interface Grid extends Iterable<Square>{
Square getSquare(int rowIndex, int columnIndex);
int getNumberOfRows();
int getNumberOfColumns();
void fill(TileGenerator tileGenerator);
}
package model;
import javafx.scene.paint.Color;
import java.util.List;
public interface Side {
List<Side> compatibleSides(List<Side> sides);
Color color();
boolean accept(Side side);
}
package model;
import java.util.List;
public interface Square {
boolean accept(Tile tile);
void put(Tile tile);
Tile getTile();
List<Tile> compatibleTiles(List<Tile> tiles);
List<Side> compatibleSides(List<Side> sides, CardinalDirection direction);
Square getNeighbor(CardinalDirection direction);
void setNeighbor(Square neighbor, CardinalDirection direction);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment