Loading jeuAssemblage/lib/pieces_puzzle-0.1.0.jar +108 B (5.48 KiB) File changed.No diff preview for this file type. View original file View changed file jeuAssemblage/src/main/Controller.java +3 −0 Changes for jeuAssemblage/src/main/Controller.java: 3 added lines, 0 removed lines. Original line number Diff line number Diff line package main; import java.awt.*; import java.awt.image.BufferedImage; import main.listener.EventListener; import java.util.Random; Loading @@ -8,6 +10,7 @@ import main.event.EventManager; import model.PlayBoard; import pieces.Piece; import view.MainFrame; import view.utils.PieceRenderUtils; public class Controller { Loading jeuAssemblage/src/model/PlayBoard.java +10 −2 Changes for jeuAssemblage/src/model/PlayBoard.java: 10 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -45,7 +45,15 @@ public class PlayBoard { public Piece getPieceById (int id) { return this.alPieceOnBoard.get(id-1);} // Setters public void setPieceSelected(Piece p) { this.selectedPiece = (Piece) p.clone(); } public void setPieceSelected(Piece p) { if (p == null) { this.selectedPiece = null; return; } this.selectedPiece = (Piece) p.clone(); } // Public /** Loading Loading @@ -124,7 +132,7 @@ public class PlayBoard { private void createPieceImage() { for (Piece p : this.alPieceOnBoard) { BufferedImage image = PieceRenderUtils.createPieceImage(p.getBounds()); BufferedImage image = PieceRenderUtils.createCellImage(); this.hmPieceImage.put(p.getInstanceId(), image); } } Loading jeuAssemblage/src/view/component/board/Grid.java +81 −15 Changes for jeuAssemblage/src/view/component/board/Grid.java: 81 added lines, 15 removed lines. Original line number Diff line number Diff line Loading @@ -3,11 +3,14 @@ package view.component.board; import static view.utils.SwingUtils.*; import main.Controller; import model.PlayBoard; import pieces.Piece; import view.utils.SwingUtils; import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.Rectangle2D; import java.util.ArrayList; Loading @@ -15,12 +18,21 @@ public class Grid extends JPanel { private final Controller controller; private int xGridDeb = 0; private int yGridDeb = 0; private int xGridFin = 0; private int yGridFin = 0; public Grid() { this.controller = Controller.getInstance(); this.setLayout(null); this.setVisible(true); this.addMouseListener(new GridClickListener()); } @Override Loading @@ -30,7 +42,7 @@ public class Grid extends JPanel { SwingUtils.drawDebugBounds(this, g); double componentSize = Math.min( int componentSize = (int) Math.min( getHeightTimesPourcent(this, 0.95f)/(this.controller.getHeightBoard()*1d), getWidthTimesPourcent(this,0.95f)/(this.controller.getWidthBoard()*1d) ); Loading @@ -38,38 +50,92 @@ public class Grid extends JPanel { double paddingWidth = this.getWidth () - (componentSize * this.controller.getWidthBoard ()); double paddingHeight = this.getHeight() - (componentSize * this.controller.getHeightBoard()); xGridDeb = (int) (paddingWidth / 2); yGridFin = (int) (paddingHeight / 2); xGridFin = (int) (getWidth() - paddingWidth / 2); yGridFin = (int) (getHeight() - paddingHeight / 2); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.WHITE); int[][] matrices = this.controller.getPlayBoard(); ArrayList<Piece> alreadyDraw = new ArrayList<Piece>(); for ( int i = 0; i < matrices.length; i++) { double y = i*componentSize + paddingHeight/2; int y = i * (int) componentSize + (int) paddingHeight / 2; for ( int j = 0; j < matrices[i].length; j++) { double x = j*componentSize + paddingWidth/2; int x = j * (int) componentSize + (int) paddingWidth / 2; Rectangle2D shape = new Rectangle2D.Double(x,y, componentSize, componentSize); if (matrices[i][j] != 0) { Piece p = this.controller.getPieceById(matrices[i][j]); if (!alreadyDraw.contains(p)) { // TODO rotate the image if needed Image img = this.controller.getImageById(p.getInstanceId()); g2d.drawImage(img, (int) Math.ceil(x) + 1, (int) Math.ceil(y) + 1, (int) Math.floor(componentSize * 3), (int) Math.floor(componentSize * 3), null); if (p.equals(controller.getSelectedPiece())) { alreadyDraw.add(p); System.out.println("Piece selected"); g2d.setColor(Color.RED); g2d.fill(shape); g2d.setColor(Color.WHITE); continue; } Image img = this.controller.getImageById(p.getInstanceId()); g2d.drawImage( img, x + 1, y + 1, componentSize, componentSize, null ); } } else { else { g2d.draw(shape); } } } } private class GridClickListener extends MouseAdapter { @Override public void mouseClicked(MouseEvent e) { System.out.println("Grid clicked"); int[][] grille = controller.getPlayBoard(); // Check if the click is in the grid if (e.getX() < xGridDeb || e.getX() > xGridFin || e.getY() < yGridDeb || e.getY() > yGridFin) { System.out.println("Click out of the grid"); return; } // Get the position of the click in the grid 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"); return; } System.out.println("Piece id: " + pieceId); Piece piece = controller.getPieceById(pieceId); controller.setPieceSelected(piece); getParent().repaint(); } } } jeuAssemblage/src/view/component/board/PieceBoard.java +157 −3 Changes for jeuAssemblage/src/view/component/board/PieceBoard.java: 157 added lines, 3 removed lines. Original line number Diff line number Diff line package view.component.board; import main.Controller; import pieces.Piece; import view.utils.SwingUtils; import javax.swing.*; import java.awt.*; import java.awt.geom.AffineTransform; public class PieceBoard extends JPanel { private final Controller controller = Controller.getInstance(); private final JButton btnTurnLeft = new JButton("<-"); private final JButton btnTurnRight = new JButton("->"); private final JButton btnCancel = new JButton("X"); public PieceBoard() { // controller.getPlayBoard(). this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); this.setLayout(null); this.add(btnTurnLeft); this.add(btnTurnRight); this.add(btnCancel); btnTurnLeft.addActionListener(e -> { controller.getSelectedPiece().rotate(Piece.LEFT); repaint(); }); btnTurnRight.addActionListener(e -> { controller.getSelectedPiece().rotate(Piece.RIGHT); repaint(); }); btnCancel.addActionListener(e -> { controller.setPieceSelected(null); getParent().repaint(); }); } @Override public void doLayout() { final int MARGIN_TOP = SwingUtils.getHeightTimesPourcent(this, 0.5f); this.add(Box.createHorizontalGlue()); this.add(new JLabel("PieceBoard")); final int BTN_WIDTH = SwingUtils.getWidthTimesPourcent(this, 0.33f); final int BTN_HEIGHT = SwingUtils.getHeightTimesPourcent(this, 0.2f); final int BTN_MARGIN = SwingUtils.getWidthTimesPourcent(this, 0.11f); btnTurnLeft.setBounds( BTN_MARGIN, MARGIN_TOP, BTN_WIDTH, BTN_HEIGHT ); btnTurnRight.setBounds( 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; final int BTN_CANCEL_GAP = SwingUtils.getWidthTimesPourcent(this, 0.05f); final int BTN_CANCEL_MARGIN = SwingUtils.getWidthTimesPourcent(this, 0.25f); btnCancel.setBounds( BTN_CANCEL_MARGIN, MARGIN_TOP + BTN_HEIGHT + BTN_CANCEL_GAP, BTN_CANCEL_WIDTH, BTN_CANCEL_HEIGHT ); } @Override Loading @@ -27,5 +93,93 @@ 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); } final Piece p = this.controller.getSelectedPiece(); if (p == null) return; final int PIECE_SIZE_X = p.getWidth(); final int PIECE_SIZE_Y = p.getHeight(); final int PIECE_MARGIN = SwingUtils.getWidthTimesPourcent(this, 0.05f); final int PIECE_HEIGHT = SwingUtils.getHeightTimesPourcent(this, 0.4f); final int PIECE_WIDTH; if (PIECE_SIZE_X > PIECE_SIZE_Y) { PIECE_WIDTH = PIECE_HEIGHT * PIECE_SIZE_X / PIECE_SIZE_Y; } else { PIECE_WIDTH = PIECE_HEIGHT; } final int PIECE_MARGIN_LEFT = (getWidth() - PIECE_WIDTH) / 2; Image img = controller.getImageById(p.getInstanceId()); img = img.getScaledInstance(PIECE_WIDTH, PIECE_HEIGHT, Image.SCALE_SMOOTH); AffineTransform at = new AffineTransform(); at.translate(PIECE_MARGIN_LEFT, PIECE_MARGIN); // switch (p.getRotate()) { // case 1: // at.rotate(Math.toRadians(90), img.getWidth(null) / 2, img.getHeight(null) / 2); // break; // case 2: // at.rotate(Math.toRadians(180), img.getWidth(null) / 2, img.getHeight(null) / 2); // break; // case 3: // at.rotate(Math.toRadians(270), img.getWidth(null) / 2, img.getHeight(null) / 2); // break; // } if (g instanceof Graphics2D) { Graphics2D g2d = (Graphics2D) g; int[][] bounds = p.getBounds(); final int CELL_SIZE; if (bounds.length > bounds[0].length) { CELL_SIZE = PIECE_HEIGHT / bounds.length; } else { CELL_SIZE = PIECE_WIDTH / bounds[0].length; } for (int i = 0; i < bounds.length; i++) { for (int j = 0; j < bounds[i].length; j++) { if (bounds[i][j] != 0) { g2d.drawImage( img, i * CELL_SIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE, null ); } } } } } } Loading
jeuAssemblage/lib/pieces_puzzle-0.1.0.jar +108 B (5.48 KiB) File changed.No diff preview for this file type. View original file View changed file
jeuAssemblage/src/main/Controller.java +3 −0 Changes for jeuAssemblage/src/main/Controller.java: 3 added lines, 0 removed lines. Original line number Diff line number Diff line package main; import java.awt.*; import java.awt.image.BufferedImage; import main.listener.EventListener; import java.util.Random; Loading @@ -8,6 +10,7 @@ import main.event.EventManager; import model.PlayBoard; import pieces.Piece; import view.MainFrame; import view.utils.PieceRenderUtils; public class Controller { Loading
jeuAssemblage/src/model/PlayBoard.java +10 −2 Changes for jeuAssemblage/src/model/PlayBoard.java: 10 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -45,7 +45,15 @@ public class PlayBoard { public Piece getPieceById (int id) { return this.alPieceOnBoard.get(id-1);} // Setters public void setPieceSelected(Piece p) { this.selectedPiece = (Piece) p.clone(); } public void setPieceSelected(Piece p) { if (p == null) { this.selectedPiece = null; return; } this.selectedPiece = (Piece) p.clone(); } // Public /** Loading Loading @@ -124,7 +132,7 @@ public class PlayBoard { private void createPieceImage() { for (Piece p : this.alPieceOnBoard) { BufferedImage image = PieceRenderUtils.createPieceImage(p.getBounds()); BufferedImage image = PieceRenderUtils.createCellImage(); this.hmPieceImage.put(p.getInstanceId(), image); } } Loading
jeuAssemblage/src/view/component/board/Grid.java +81 −15 Changes for jeuAssemblage/src/view/component/board/Grid.java: 81 added lines, 15 removed lines. Original line number Diff line number Diff line Loading @@ -3,11 +3,14 @@ package view.component.board; import static view.utils.SwingUtils.*; import main.Controller; import model.PlayBoard; import pieces.Piece; import view.utils.SwingUtils; import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.Rectangle2D; import java.util.ArrayList; Loading @@ -15,12 +18,21 @@ public class Grid extends JPanel { private final Controller controller; private int xGridDeb = 0; private int yGridDeb = 0; private int xGridFin = 0; private int yGridFin = 0; public Grid() { this.controller = Controller.getInstance(); this.setLayout(null); this.setVisible(true); this.addMouseListener(new GridClickListener()); } @Override Loading @@ -30,7 +42,7 @@ public class Grid extends JPanel { SwingUtils.drawDebugBounds(this, g); double componentSize = Math.min( int componentSize = (int) Math.min( getHeightTimesPourcent(this, 0.95f)/(this.controller.getHeightBoard()*1d), getWidthTimesPourcent(this,0.95f)/(this.controller.getWidthBoard()*1d) ); Loading @@ -38,38 +50,92 @@ public class Grid extends JPanel { double paddingWidth = this.getWidth () - (componentSize * this.controller.getWidthBoard ()); double paddingHeight = this.getHeight() - (componentSize * this.controller.getHeightBoard()); xGridDeb = (int) (paddingWidth / 2); yGridFin = (int) (paddingHeight / 2); xGridFin = (int) (getWidth() - paddingWidth / 2); yGridFin = (int) (getHeight() - paddingHeight / 2); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.WHITE); int[][] matrices = this.controller.getPlayBoard(); ArrayList<Piece> alreadyDraw = new ArrayList<Piece>(); for ( int i = 0; i < matrices.length; i++) { double y = i*componentSize + paddingHeight/2; int y = i * (int) componentSize + (int) paddingHeight / 2; for ( int j = 0; j < matrices[i].length; j++) { double x = j*componentSize + paddingWidth/2; int x = j * (int) componentSize + (int) paddingWidth / 2; Rectangle2D shape = new Rectangle2D.Double(x,y, componentSize, componentSize); if (matrices[i][j] != 0) { Piece p = this.controller.getPieceById(matrices[i][j]); if (!alreadyDraw.contains(p)) { // TODO rotate the image if needed Image img = this.controller.getImageById(p.getInstanceId()); g2d.drawImage(img, (int) Math.ceil(x) + 1, (int) Math.ceil(y) + 1, (int) Math.floor(componentSize * 3), (int) Math.floor(componentSize * 3), null); if (p.equals(controller.getSelectedPiece())) { alreadyDraw.add(p); System.out.println("Piece selected"); g2d.setColor(Color.RED); g2d.fill(shape); g2d.setColor(Color.WHITE); continue; } Image img = this.controller.getImageById(p.getInstanceId()); g2d.drawImage( img, x + 1, y + 1, componentSize, componentSize, null ); } } else { else { g2d.draw(shape); } } } } private class GridClickListener extends MouseAdapter { @Override public void mouseClicked(MouseEvent e) { System.out.println("Grid clicked"); int[][] grille = controller.getPlayBoard(); // Check if the click is in the grid if (e.getX() < xGridDeb || e.getX() > xGridFin || e.getY() < yGridDeb || e.getY() > yGridFin) { System.out.println("Click out of the grid"); return; } // Get the position of the click in the grid 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"); return; } System.out.println("Piece id: " + pieceId); Piece piece = controller.getPieceById(pieceId); controller.setPieceSelected(piece); getParent().repaint(); } } }
jeuAssemblage/src/view/component/board/PieceBoard.java +157 −3 Changes for jeuAssemblage/src/view/component/board/PieceBoard.java: 157 added lines, 3 removed lines. Original line number Diff line number Diff line package view.component.board; import main.Controller; import pieces.Piece; import view.utils.SwingUtils; import javax.swing.*; import java.awt.*; import java.awt.geom.AffineTransform; public class PieceBoard extends JPanel { private final Controller controller = Controller.getInstance(); private final JButton btnTurnLeft = new JButton("<-"); private final JButton btnTurnRight = new JButton("->"); private final JButton btnCancel = new JButton("X"); public PieceBoard() { // controller.getPlayBoard(). this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); this.setLayout(null); this.add(btnTurnLeft); this.add(btnTurnRight); this.add(btnCancel); btnTurnLeft.addActionListener(e -> { controller.getSelectedPiece().rotate(Piece.LEFT); repaint(); }); btnTurnRight.addActionListener(e -> { controller.getSelectedPiece().rotate(Piece.RIGHT); repaint(); }); btnCancel.addActionListener(e -> { controller.setPieceSelected(null); getParent().repaint(); }); } @Override public void doLayout() { final int MARGIN_TOP = SwingUtils.getHeightTimesPourcent(this, 0.5f); this.add(Box.createHorizontalGlue()); this.add(new JLabel("PieceBoard")); final int BTN_WIDTH = SwingUtils.getWidthTimesPourcent(this, 0.33f); final int BTN_HEIGHT = SwingUtils.getHeightTimesPourcent(this, 0.2f); final int BTN_MARGIN = SwingUtils.getWidthTimesPourcent(this, 0.11f); btnTurnLeft.setBounds( BTN_MARGIN, MARGIN_TOP, BTN_WIDTH, BTN_HEIGHT ); btnTurnRight.setBounds( 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; final int BTN_CANCEL_GAP = SwingUtils.getWidthTimesPourcent(this, 0.05f); final int BTN_CANCEL_MARGIN = SwingUtils.getWidthTimesPourcent(this, 0.25f); btnCancel.setBounds( BTN_CANCEL_MARGIN, MARGIN_TOP + BTN_HEIGHT + BTN_CANCEL_GAP, BTN_CANCEL_WIDTH, BTN_CANCEL_HEIGHT ); } @Override Loading @@ -27,5 +93,93 @@ 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); } final Piece p = this.controller.getSelectedPiece(); if (p == null) return; final int PIECE_SIZE_X = p.getWidth(); final int PIECE_SIZE_Y = p.getHeight(); final int PIECE_MARGIN = SwingUtils.getWidthTimesPourcent(this, 0.05f); final int PIECE_HEIGHT = SwingUtils.getHeightTimesPourcent(this, 0.4f); final int PIECE_WIDTH; if (PIECE_SIZE_X > PIECE_SIZE_Y) { PIECE_WIDTH = PIECE_HEIGHT * PIECE_SIZE_X / PIECE_SIZE_Y; } else { PIECE_WIDTH = PIECE_HEIGHT; } final int PIECE_MARGIN_LEFT = (getWidth() - PIECE_WIDTH) / 2; Image img = controller.getImageById(p.getInstanceId()); img = img.getScaledInstance(PIECE_WIDTH, PIECE_HEIGHT, Image.SCALE_SMOOTH); AffineTransform at = new AffineTransform(); at.translate(PIECE_MARGIN_LEFT, PIECE_MARGIN); // switch (p.getRotate()) { // case 1: // at.rotate(Math.toRadians(90), img.getWidth(null) / 2, img.getHeight(null) / 2); // break; // case 2: // at.rotate(Math.toRadians(180), img.getWidth(null) / 2, img.getHeight(null) / 2); // break; // case 3: // at.rotate(Math.toRadians(270), img.getWidth(null) / 2, img.getHeight(null) / 2); // break; // } if (g instanceof Graphics2D) { Graphics2D g2d = (Graphics2D) g; int[][] bounds = p.getBounds(); final int CELL_SIZE; if (bounds.length > bounds[0].length) { CELL_SIZE = PIECE_HEIGHT / bounds.length; } else { CELL_SIZE = PIECE_WIDTH / bounds[0].length; } for (int i = 0; i < bounds.length; i++) { for (int j = 0; j < bounds[i].length; j++) { if (bounds[i][j] != 0) { g2d.drawImage( img, i * CELL_SIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE, null ); } } } } } }