Commit abe92cf7 authored by erreiip's avatar erreiip
Browse files
parents 19eef694 68bc9199
Loading
Loading
Loading
Loading
+8 −0
Changes for jeuAssemblage/src/main/Controller.java: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -104,8 +104,16 @@ public class Controller {
	}

	public void addPieceOnBoard(int x, int y) {

		this.model.addSelectedPiece(x, y);
		this.view.repaint();
		this.eventManager.fireEvent();
	}

	public void removeSelectedPiece() {

		this.model.removeSelectedPiece();
		this.view.repaint();
		this.eventManager.fireEvent();
	}
}
+36 −4
Changes for jeuAssemblage/src/model/PlayBoard.java: 36 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -78,12 +78,26 @@ public class PlayBoard {
    * @see          Piece
    */
    public void addSelectedPiece(int x, int y) {

        if (this.selectedPiece == null) return;

        this.addPieceOnBoard(this.selectedPiece, x, y);

		int index = this.alPieceOnBoard.indexOf(this.getPieceById(this.selectedPiece.getInstanceId()));
        this.alPieceOnBoard.remove(this.getPieceById(this.selectedPiece.getInstanceId()));
		this.alPieceOnBoard.add(index, this.selectedPiece);
        this.selectedPiece = null;

		printMatrice(playBoard);
    }

	public void removeSelectedPiece() {

		if (this.selectedPiece == null) return;

		this.removePieceOnBoard(this.selectedPiece);

		this.alPieceOnBoard.add(this.getPieceById(this.selectedPiece.getInstanceId()));
	}

    /**
@@ -230,19 +244,37 @@ public class PlayBoard {
        int[][] bounds = p.getBounds();
        int k = 0;
        int l = 0;
        for (int i = y; i < y + p.getHeight(); i++) {
            l = 0;
            for (int j = x; j < x + p.getWidth(); j++) {
                this.playBoard[i][j] = bounds[k][l] * (p.getInstanceId() + 1);

        for (int i = y; i < y + bounds.length; i++) {

			for (int j = x; j < x + bounds[i - y].length; j++) {

				if (bounds[k][l] == 1) {

					this.playBoard[i][j] = p.getInstanceId() + 1;
				}

				l++;
			}
			k++;
			l = 0;
		}

        p.setX(x);
        p.setY(y);
    }

	public void removePieceOnBoard(Piece p) {

		for (int i = p.getY(); i < p.getY() + p.getHeight(); i++) {

			for (int j = p.getX(); j < p.getX() + p.getWidth(); j++) {

				this.playBoard[i][j] = 0;
			}
		}
	}

    private boolean canBeAddedToBoard(int x, int y, int width, int height) {
        if ( x + width  > this.playBoard.length   ) return false;
        if ( y + height > this.playBoard[0].length) return false;
+69 −8
Changes for jeuAssemblage/src/view/component/board/Grid.java: 69 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -12,7 +12,6 @@ import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

public class Grid extends JPanel {

@@ -33,6 +32,7 @@ public class Grid extends JPanel {
        this.setVisible(true);

		this.addMouseListener(new GridClickListener());
		this.addMouseMotionListener(new GridMouseMotionListener());
    }

	@Override
@@ -58,6 +58,7 @@ public class Grid extends JPanel {

        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.WHITE);

		int[][] matrices = this.controller.getPlayBoard();

        for ( int i = 0; i < matrices.length; i++) {
@@ -75,8 +76,6 @@ public class Grid extends JPanel {

					if (p.equals(controller.getSelectedPiece())) {

						System.out.println("Piece selected");

						g2d.setColor(Color.RED);
						g2d.fill(shape);
						g2d.setColor(Color.WHITE);
@@ -101,14 +100,53 @@ public class Grid extends JPanel {
                }
            }
        }

		if (controller.getSelectedPiece() != null) {

			// Get the position of the mouse in the grid
			int x = (int) ((mousePosition.getX() - xGridDeb) / ((xGridFin - xGridDeb) / matrices[0].length));
			int y = (int) ((mousePosition.getY() - yGridDeb) / ((yGridFin - yGridDeb) / matrices.length));

			int[][] piece = controller.getSelectedPiece().getBounds();
			Image img = controller.getImageById(controller.getSelectedPiece().getInstanceId());

			for (int i = 0; i < piece.length; i++) {

				for (int j = 0; j < piece[i].length; j++) {

					if (piece[i][j] != 0) {

						int x2 = (x + j - 1);
						int y2 = (y + i - 1);

						if (x2 - j < 0) x2 = j;
						if (y2 - i < 0) y2 = i;

						if (x2 + (piece[i].length - j) > matrices[0].length) x2 = matrices[0].length - piece[i].length + j;
						if (y2 + (piece.length - i) > matrices.length) y2 = matrices.length - piece.length + i;

						g2d.drawImage(
							img,
							x2 * (int) componentSize + (int) paddingWidth / 2 + 1,
							y2 * (int) componentSize + (int) paddingHeight / 2 + 1,
							(int) componentSize,
							(int) componentSize,
							null
						);
					}
				}
			}
		}
	}

	private final Point mousePosition = new Point(0, 0);

	private class GridClickListener extends MouseAdapter {
	
		@Override
		public void mouseClicked(MouseEvent e) {

			System.out.println("Grid clicked");
			mousePosition.setLocation(e.getX(), e.getY());

			int[][] grille = controller.getPlayBoard();

@@ -122,20 +160,43 @@ public class Grid extends JPanel {
			int x = (int) ((e.getX() - xGridDeb) / ((xGridFin - xGridDeb) / grille[0].length));
			int y = (int) ((e.getY() - yGridDeb) / ((yGridFin - yGridDeb) / grille.length));

			System.out.println("x: " + x + " y: " + y);

			int pieceId = grille[y][x];

			if (pieceId == 0) {
				System.out.println("No piece here");

				if (controller.getSelectedPiece() == null) return;

				if (controller.canBeAddedToBoard(x, y)) {

					controller.addPieceOnBoard(x, y);
					controller.setPieceSelected(null);
					getParent().repaint();
				}
				else {

					System.out.println("Piece can't be added to the board");
				}

				return;
			}

			System.out.println("Piece id: " + pieceId);
			Piece piece = controller.getPieceById(pieceId);

			controller.setPieceSelected(piece);
			getParent().repaint();
		}
	}

	private class GridMouseMotionListener extends MouseAdapter {

		@Override
		public void mouseMoved(MouseEvent e) {

			if (controller.getSelectedPiece() != null) {

				mousePosition.setLocation(e.getX(), e.getY());
				repaint();
			}
		}
	}
}
+29 −17
Changes for jeuAssemblage/src/view/component/board/PieceBoard.java: 29 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ import view.utils.SwingUtils;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.util.Arrays;

public class PieceBoard extends JPanel {

@@ -14,6 +15,7 @@ public class PieceBoard extends JPanel {

	private final JButton btnTurnLeft  = new JButton("<-");
	private final JButton btnTurnRight = new JButton("->");
	private final JButton btnReverse   = new JButton("<>");

	private final JButton btnCancel = new JButton("X");

@@ -25,19 +27,29 @@ public class PieceBoard extends JPanel {

		this.add(btnTurnLeft);
		this.add(btnTurnRight);
		this.add(btnReverse);

		this.add(btnCancel);

		btnTurnLeft.addActionListener(e -> {

			controller.getSelectedPiece().rotate(Piece.LEFT);
			repaint();
			System.out.println(Arrays.deepToString(controller.getSelectedPiece().getBounds()));
			getParent().repaint();
		});

		btnTurnRight.addActionListener(e -> {

			controller.getSelectedPiece().rotate(Piece.RIGHT);
			repaint();
			System.out.println(Arrays.deepToString(controller.getSelectedPiece().getBounds()));
			getParent().repaint();
		});

		btnReverse.addActionListener(e -> {

			controller.getSelectedPiece().reverse();
			System.out.println(Arrays.deepToString(controller.getSelectedPiece().getBounds()));
			getParent().repaint();
		});

		btnCancel.addActionListener(e -> {
@@ -52,7 +64,7 @@ public class PieceBoard extends JPanel {

		final int MARGIN_TOP = SwingUtils.getHeightTimesPourcent(this, 0.5f);

		final int BTN_WIDTH  = SwingUtils.getWidthTimesPourcent(this, 0.33f);
		final int BTN_WIDTH  = SwingUtils.getWidthTimesPourcent(this, 0.2f);
		final int BTN_HEIGHT = SwingUtils.getHeightTimesPourcent(this, 0.2f);

		final int BTN_MARGIN = SwingUtils.getWidthTimesPourcent(this, 0.11f);
@@ -64,13 +76,20 @@ public class PieceBoard extends JPanel {
			BTN_HEIGHT
		);

		btnTurnRight.setBounds(
		btnReverse.setBounds(
			BTN_MARGIN + BTN_WIDTH + BTN_MARGIN,
			MARGIN_TOP,
			BTN_WIDTH,
			BTN_HEIGHT
		);

		btnTurnRight.setBounds(
			BTN_MARGIN + BTN_WIDTH + BTN_MARGIN + BTN_WIDTH + BTN_MARGIN,
			MARGIN_TOP,
			BTN_WIDTH,
			BTN_HEIGHT
		);

		final int BTN_CANCEL_WIDTH  = SwingUtils.getWidthTimesPourcent(this, 0.5f);
		final int BTN_CANCEL_HEIGHT = BTN_HEIGHT;

@@ -93,18 +112,11 @@ public class PieceBoard extends JPanel {

        SwingUtils.drawDebugBounds(this, g);

		if (this.controller.getSelectedPiece() == null) {

			btnTurnLeft.setVisible(false);
			btnTurnRight.setVisible(false);
			btnCancel.setVisible(false);
		}
		else {

			btnTurnLeft.setVisible(true);
			btnTurnRight.setVisible(true);
			btnCancel.setVisible(true);
		}
		boolean hasSelectedPiece = controller.getSelectedPiece() != null;
		btnTurnLeft.setVisible(hasSelectedPiece);
		btnTurnRight.setVisible(hasSelectedPiece);
		btnReverse.setVisible(hasSelectedPiece);
		btnCancel.setVisible(hasSelectedPiece);

		final Piece p = this.controller.getSelectedPiece();
		if (p == null) return;
@@ -171,8 +183,8 @@ public class PieceBoard extends JPanel {

						g2d.drawImage(
							img,
							i * CELL_SIZE,
							j * CELL_SIZE,
							i * CELL_SIZE,
							CELL_SIZE,
							CELL_SIZE,
							null
+42 −32
Changes for piece_puzzle/src/pieces/Piece.java: 42 added lines, 32 removed lines.
Original line number Diff line number Diff line
@@ -13,6 +13,8 @@ public abstract class Piece implements Cloneable {
    protected int rotate;
    protected int size;

	private int[][] bounds = getInitialBounds();

    //CONSTRUCTORS
    private Piece(int x, int y, int size, int rotate, int instanceId) {
        this.x = x;
@@ -37,12 +39,30 @@ public abstract class Piece implements Cloneable {

    //METHODS
    public void rotate(int sense) {
        if (sense == Piece.LEFT) {
            this.rotate = (this.rotate - 1) % 4;
        }

		if (sense == Piece.RIGHT) {
            this.rotate = (this.rotate + 1) % 4;

			int rows = bounds.length;
			int cols = bounds[0].length;

			int[][] rotatedMatrix = new int[cols][rows];

			for (int i = 0; i < rows; i++) {

				for (int j = 0; j < cols; j++) {

					rotatedMatrix[j][rows - 1 - i] = bounds[i][j];
				}
			}

			bounds = rotatedMatrix;
        }

        if (sense == Piece.LEFT) {

			rotate(Piece.RIGHT);
			rotate(Piece.RIGHT);
			rotate(Piece.RIGHT);
        }
    }

@@ -63,46 +83,36 @@ public abstract class Piece implements Cloneable {
    //ABSTRACTS
    protected abstract int[][] getInitialBounds();

    //BOUNDS
    private int[][] getRotateBounds () {
        int[][] initBounds = getInitialBounds();
        int[][] retBounds  = new int[initBounds.length][initBounds[0].length];
	public void reverse() {

        if ( this.rotate == 0) return initBounds;
		int rows = bounds.length;
		int cols = bounds[0].length;

        if ( this.rotate == 2) {
            for (int i = 0; i < initBounds.length; i++) {
                for ( int j = 0 ; j < initBounds[i].length; j++) {
                    retBounds[i][j] = initBounds[initBounds.length - 1 -j][i];
                }
            }
        }
		int[][] reversedMatrix = new int[rows][cols];

        if ( this.rotate == 1) {
            for (int i = 0; i < initBounds.length; i++) {
                for ( int j = 0 ; j < initBounds[i].length; j++) {
                    retBounds[i][j] = initBounds[j][initBounds.length - 1 -i];
                }
            }
        }
		for (int i = 0; i < rows; i++) {

        if ( this.rotate == 3 ) {
            for (int i = 0; i < initBounds.length; i++) {
                for ( int j = 0 ; j < initBounds[i].length; j++) {
                    retBounds[i][j] = initBounds[initBounds.length - 1 -j][initBounds.length - 1 -i];
                }
			for (int j = 0; j < cols; j++) {

				reversedMatrix[i][j] = bounds[i][cols - 1 - j];
			}
		}

        return retBounds;
		bounds = reversedMatrix;
	}

    public int[][] getBounds() {
        return this.getRotateBounds();
        return this.bounds;
    }

    public int getWidth () {
		
		return this.bounds[0].length;
	}
    public int getHeight() {
		
    public int getWidth () { return 3; }
    public int getHeight() { return 3; }
		return this.bounds.length;
	}

    public void destroy() {
        Piece.id--;
Loading