Skip to content
Snippets Groups Projects
Commit d2e4de1c authored by BAUER Oscar's avatar BAUER Oscar
Browse files

Q4 - version1

parent 63f3c434
No related branches found
No related tags found
No related merge requests found
Showing
with 0 additions and 839 deletions
package tp1.version1;
public class MainGui {
public static void main(String[] args) {
new MinesweeperFrame();
}
}
package tp1.version1;
public class Minesweeper {
}
package tp1.version1;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
@SuppressWarnings("serial")
public class MinesweeperFrame extends JFrame {
Minesweeper minesweeper;
MinesweeperPanel minesweeperPanel;
public MinesweeperFrame() {
this.minesweeper = new Minesweeper();
this.minesweeperPanel = new MinesweeperPanel(minesweeper);
add(minesweeperPanel);
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
}
package tp1.version1;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MinesweeperPanel extends JPanel {
Minesweeper minesweeper;
public MinesweeperPanel(Minesweeper minesweeper) {
this.minesweeper = minesweeper;
setPreferredSize(new Dimension(500, 500));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawLine(getWidth()/2, getHeight()/2, getWidth(), getHeight());
g.setColor(Color.BLUE);
g.drawLine(getWidth(), 0, getWidth() / 2, getHeight() / 2);
g.setColor(Color.BLACK);
g.drawLine(0, 0, getWidth() / 2, getHeight() / 2);
g.setColor(Color.CYAN);
g.drawLine(0, getHeight(), getWidth() / 2, getHeight() / 2);
g.setColor(Color.LIGHT_GRAY);
g.fillRect(200, 200, 30, 30);
g.setColor(Color.GRAY);
g.drawRect(200, 200, 30, 30);
}
}
package tp1.version2;
public class MainGui {
public static void main(String[] args) {
new MinesweeperFrame();
}
}
package tp1.version2;
public class Minesweeper {
}
package tp1.version2;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MinesweeperPanel extends JPanel {
Minesweeper minesweeper;
public MinesweeperPanel(Minesweeper minesweeper) {
this.minesweeper = minesweeper;
setPreferredSize(new Dimension(500, 500));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawLine(0, 0, getWidth(), getHeight());
g.setColor(Color.BLUE);
g.drawLine(500, 0, getWidth() / 2, getHeight() / 2);
g.setColor(Color.BLACK);
g.drawRect(100, 200, 30, 50);
g.setColor(Color.LIGHT_GRAY);
g.fillRect(200, 200, 30, 30);
g.setColor(Color.GRAY);
g.drawRect(200, 200, 30, 30);
}
}
package tp2.v1;
public class MainGui {
public static void main(String[] args) {
new MinesweeperFrame();
}
}
package tp2.v1;
public class Minesweeper {
int nbColumns;
int nbLines;
int score;
int selectedLine;
int selectedColumn;
}
package tp2.v1;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
@SuppressWarnings("serial")
public class MinesweeperFrame extends JFrame {
Minesweeper minesweeper;
MinesweeperPanel minesweeperPanel;
public MinesweeperFrame() {
this.minesweeper = new Minesweeper();
this.minesweeper.nbColumns = 15;
this.minesweeper.nbLines = 20;
this.minesweeper.score = 30;
this.minesweeper.selectedLine = 3;
this.minesweeper.selectedColumn = 8;
this.minesweeperPanel = new MinesweeperPanel(minesweeper);
add(minesweeperPanel);
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}}
\ No newline at end of file
package tp2.v1;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MinesweeperPanel extends JPanel {
Minesweeper minesweeper;
int spaceHor;
int spaceVert;
public MinesweeperPanel(Minesweeper minesweeper) {
this.minesweeper = minesweeper;
setPreferredSize(new Dimension(500, 500));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// grand rect
int spaceBorder = 20;
int nbColumns = this.minesweeper.nbColumns;
int nbRows = this.minesweeper.nbLines;
int carreWidth = (getWidth() - (spaceBorder * 2)) - ((getWidth() - (spaceBorder * 2)) % nbColumns);
int carreHeight = (getHeight() - (spaceBorder * 2)) - ((getHeight() - (spaceBorder * 2)) % nbRows);
this.spaceHor = carreWidth / nbColumns;
this.spaceVert = carreHeight / nbRows;
int lineEndH = carreHeight + spaceBorder;
int lineEndV = carreWidth + spaceBorder;
// creating collumns
int posH = spaceBorder;
g.setColor(Color.BLACK);
g.drawLine(spaceBorder, spaceBorder, spaceBorder, lineEndH);
for (int idx = 1; idx <= nbColumns; idx++) {
posH = (idx * spaceHor) + spaceBorder;
g.setColor(Color.BLACK);
g.drawLine(posH, spaceBorder, posH, lineEndH);
}
// creating rows
int posV = spaceBorder;
g.setColor(Color.BLACK);
g.drawLine(spaceBorder, spaceBorder, lineEndV, spaceBorder);
for (int idx = 1; idx <= nbRows; idx++) {
posV = (idx * spaceVert) + spaceBorder;
g.setColor(Color.BLACK);
g.drawLine(spaceBorder, posV, lineEndV, posV);
}
// show score
g.setColor(Color.darkGray);
g.setFont(new Font("Ariel", Font.BOLD, spaceBorder - 3));
g.drawString("Score : " + this.minesweeper.score, (getWidth() / 2) - 40, (spaceBorder - 3));
}
}
package tp2.v1;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MinesweeperPanelDamier extends JPanel {
Minesweeper minesweeper;
public MinesweeperPanelDamier(Minesweeper minesweeper) {
this.minesweeper = minesweeper;
setPreferredSize(new Dimension(500, 500));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawLine(0, 0, getWidth(), getHeight());
g.setColor(Color.BLUE);
g.drawLine(getWidth(), getHeight(), getWidth() / 2, getHeight() / 2);
// grand rect
int widthHouse = getWidth() / 3;
int heightHouse = getHeight() / 3;
int xMid = getWidth() / 2;
int yMid = getHeight() / 2;
int unten = yMid + (heightHouse / 2) ;
int oben = yMid - heightHouse / 2;
int links = xMid - widthHouse / 2;
int rechts = xMid + (widthHouse / 2);
// house
g.setColor(Color.YELLOW);
g.fillRect(links,
oben,
widthHouse,
heightHouse);
g.setColor(Color.BLACK);
g.drawRect(links,
oben,
widthHouse,
heightHouse);
g.setColor(Color.BLACK);
g.drawLine(links, oben, rechts - widthHouse / 2, oben - heightHouse / 2);
g.setColor(Color.BLACK);
g.drawLine(rechts, oben, links + widthHouse / 2, oben - heightHouse / 2);
////port
// g.setColor(Color.LIGHT_GRAY);
// g.fillRect(200, 200, 30, 30);
// g.setColor(Color.GRAY);
// g.drawRect(200, 200, 30, 30);
// g.setColor(Color.BLUE);
g.setColor(Color.RED);
g.drawLine(links, oben, rechts, oben);
// Oval getWidth() getHeight()
g.setColor(Color.RED);
g.drawOval(0, 100, 10, 10);
}
}
package tp2.v1;
public class fiddling {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 20%9;
System.out.println(x);
}
}
package tp4.v1;
import java.awt.Color;
public class Cell {
boolean isRevealed;
boolean isMine;
boolean isSelected;
Color color;
int row;
int column;
boolean active;
public Cell(boolean isMine, int row, int column) {
this.isRevealed = false;
this.isSelected = false;
this.color = Color.GRAY;
this.isMine = isMine;
this.row = row;
this.column = column;
this.active = true;
}
private void refreshColor() {
if (this.isSelected)
this.color = Color.RED;
else if (!this.isRevealed)
this.color = Color.DARK_GRAY;
else
this.color = Color.lightGray;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
public void setRevealed(boolean status) {
this.isRevealed = status;
this.active = false;
if (status && !this.isMine)
this.color = Color.DARK_GRAY;
}
public void setMine(boolean isMine) {
this.isMine = isMine;
}
public boolean getIsSelected() {
return isSelected;
}
public Color getColor() {
if (active)
refreshColor();
return color;
}
public void setColor(Color color) {
this.color = color;
}
public boolean isRevealed() {
return isRevealed;
}
public boolean getIsMine() {
return isMine;
}
public void setActive(boolean active) {
this.active = active;
}
}
package tp4.v1;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Minesweeper {
int nbColumns;
int nbLines;
int score;
int selectedLine;
int selectedColumn;
double difficulte = 0.25;
Color colorSweeper = Color.RED;
ArrayList<List<Cell>> board;
public Minesweeper() {
this.nbColumns = 10;
this.nbLines = 10;
this.score = this.nbColumns * this.nbLines;
this.selectedLine = 0;
this.selectedColumn = 0;
this.board = new ArrayList<List<Cell>>();
for (int row = 0; row < this.nbLines; row++) {
this.board.add(new ArrayList<Cell>());
for (int column = 0; column < this.nbColumns; column++) {
boolean isMine = ((int) (Math.random() + difficulte)) == 1;
Cell cell = new Cell(isMine, row, column);
if (isMine)
score--;
this.board.get(row).add(cell);
}
}
}
public double getDifficulte() {
return difficulte;
}
public void setDifficulte(double difficulte) {
this.difficulte = difficulte;
}
public Color getColorSweeper() {
return colorSweeper;
}
public void setColorSweeper(Color color) {
this.colorSweeper = color;
}
}
package tp4.v1;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.WindowConstants;
@SuppressWarnings("serial")
public class MinesweeperFrame extends JFrame {
Minesweeper minesweeper;
MinesweeperPanel minesweeperPanel;
static JMenuBar mb;
static JMenu menu, size, difficulte, color;
// static JMenuItem size, difficulte, color;
static JMenuItem size1, size2, size3, diff1, diff2, diff3, color1, color2, color3 ;
static JFrame f;
public MinesweeperFrame() {
mb = new JMenuBar();
menu = new JMenu("menu");
size = new JMenu("size in pixel");
difficulte = new JMenu("Mines/no mines");
color = new JMenu("Color");
size1 = new JMenuItem("500");
size2 = new JMenuItem("700");
size3 = new JMenuItem("1000");
size.add(size1);
size.add(size2);
size.add(size3);
diff1 = new JMenuItem("50/50");
diff2 = new JMenuItem("25/75");
diff3 = new JMenuItem("75/25");
difficulte.add(diff1);
difficulte.add(diff2);
difficulte.add(diff3);
color1 = new JMenuItem("red");
color2 = new JMenuItem("blue");
color3 = new JMenuItem("black");
color.add(color1);
color.add(color2);
color.add(color3);
menu.add(size);
menu.add(difficulte);
menu.add(color);
mb.add(menu);
setJMenuBar(mb);
this.minesweeper = new Minesweeper();
this.minesweeperPanel = new MinesweeperPanel(minesweeper);
add(minesweeperPanel);
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
}
\ No newline at end of file
package tp4.v1;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MinesweeperPanel extends JPanel implements MinesweeperPanelable {
Minesweeper minesweeper;
int spaceHor;
int spaceVert;
int nbColumns;
int nbRows;
int selCol;
int selLine;
int carreWidth;
int carreHeight;
int spaceBorder;
static JMenuBar mb;
static JMenu menu;
static JMenuItem size, difficulte, color;
static JFrame f;
public MinesweeperPanel(Minesweeper minesweeper) {
this.minesweeper = minesweeper;
setPreferredSize(new Dimension(500, 500));
addMouseListener(new PanelMouseListener(this));
addMouseMotionListener(new PanelMouseListener(this));
spaceBorder = 20;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.nbColumns = this.minesweeper.nbColumns;
this.nbRows = this.minesweeper.nbLines;
this.carreWidth = (getWidth() - (getBoardBorder() * 2)) - ((getWidth() - (getBoardBorder() * 2)) % nbColumns);
this.carreHeight = (getHeight() - (getBoardBorder() * 2)) - ((getHeight() - (getBoardBorder() * 2)) % nbRows);
this.spaceHor = carreWidth / nbColumns;
this.spaceVert = carreHeight / nbRows;
int lineEndH = carreHeight + getBoardBorder();
int lineEndV = carreWidth + getBoardBorder();
for (int idxRow = 0; idxRow < this.minesweeper.board.size(); idxRow++) {
for (int idxCol = 0; idxCol < this.minesweeper.board.get(idxRow).size(); idxCol++) {
Cell tmp = this.minesweeper.board.get(idxRow).get(idxCol);
drawCell(g, idxRow, idxCol);
tmp.setSelected(false);
}
}
// show score
g.setColor(Color.darkGray);
g.setFont(new Font("Ariel", Font.BOLD, spaceBorder - 3));
g.drawString("Score : " + this.minesweeper.score, (getWidth() / 2) - 40, (spaceBorder - 3));
}
public void drawCell(Graphics g, int line, int column) {
g.setColor(Color.BLACK);
g.drawRect(getCellX(column), getCellY(line), getCellWidth(), getCellHeight());
Cell tmp = this.minesweeper.board.get(line).get(column);
g.setColor(tmp.getColor());
g.fillRect(getCellX(column), getCellY(line), getCellWidth(), getCellHeight());
if (tmp.isRevealed && tmp.isMine) {
int sizeBomb = 25;
g.setColor(Color.BLACK);
g.fillOval(getCellX(column) + (getCellWidth() / 2) - (sizeBomb / 2),
getCellX(line) + (getCellHeight() / 2) - (sizeBomb / 2), sizeBomb, sizeBomb);
}
}
public void drawCell(Graphics g, Cell tmp) {
g.setColor(Color.BLACK);
g.drawRect(getCellX(tmp.column), getCellY(tmp.row), getCellWidth(), getCellHeight());
g.setColor(tmp.getColor());
g.fillRect(getCellX(tmp.column), getCellY(tmp.row), getCellWidth(), getCellHeight());
if (tmp.isRevealed && tmp.isMine) {
int sizeBomb = 25;
g.setColor(Color.BLACK);
g.fillOval(getCellX(tmp.column) + (getCellWidth() / 2) - (sizeBomb / 2),
getCellX(tmp.row) + (getCellHeight() / 2) - (sizeBomb / 2), sizeBomb, sizeBomb);
}
}
public int getColumn(int x) {
boolean condX = x > getBoardBorder() && x < (getBoardBorder() + (this.nbColumns * getCellWidth()));
if (condX) {
int posCarre = x - getBoardBorder();
this.selCol = posCarre / getCellWidth();
return this.selCol;
}
return -1;
}
public int getLine(int y) {
boolean condY = y > getBoardBorder() && y < (getBoardBorder() + (this.nbRows * getCellHeight()));
if (condY) {
int posCarre = y - getBoardBorder();
this.selLine = posCarre / spaceVert;
return this.selLine;
}
return -1;
}
public int getBoardBorder() {
return this.spaceBorder;
}
@Override
public boolean isBlackCell(int line, int column) {
return this.minesweeper.board.get(line).get(column).getColor() == Color.DARK_GRAY;
}
@Override
public boolean isGrayCell(int line, int column) {
return this.minesweeper.board.get(line).get(column).getColor() == Color.GRAY;
}
public boolean isRedCell(int line, int column) {
return this.minesweeper.board.get(line).get(column).getColor() == Color.RED;
}
boolean isMine(int line, int column) {
return this.minesweeper.board.get(line).get(column).isMine;
}
public int getCellX(int column) {
return (getBoardBorder() + (column * getCellWidth()));
}
public int getCellY(int line) {
return (getBoardBorder() + (line * getCellHeight()));
}
public int getCellWidth() {
return this.spaceHor;
}
public int getCellHeight() {
return this.spaceVert;
}
public boolean isOutOfBounds(int x, int y) {
boolean condX = x > getBoardBorder() && x < (getBoardBorder() + (this.nbColumns * getCellWidth()));
boolean condY = y > getBoardBorder() && y < (getBoardBorder() + (this.nbRows * getCellHeight()));
if (condX && condY)
return true;
return false;
}
@Override
public void drawMinesweeper(Graphics g) {
}
}
package tp4.v1;
import java.awt.Graphics;
public interface MinesweeperPanelable {
public int getBoardBorder();
public boolean isBlackCell(int line, int column);
public boolean isGrayCell(int line, int column);
public boolean isRedCell(int line, int column);
public int getCellX(int column);
public int getCellY(int line);
public int getCellWidth();
public int getCellHeight();
public void drawCell(Graphics g, int line, int column);
public int getColumn(int x);
public int getLine(int y);
void drawMinesweeper(Graphics g);
}
package tp4.v1;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class PanelMouseListener extends MouseAdapter {
MinesweeperPanel panel;
public PanelMouseListener(MinesweeperPanel panel) {
this.panel = panel;
}
@Override
public void mouseClicked(MouseEvent e) {
// System.out.println("Click Line = " + panel.minesweeper.selectedLine);
// System.out.println("Click Column = " + panel.minesweeper.selectedColumn);
int line = panel.getLine(e.getY());
int column = panel.getColumn(e.getX());
panel.minesweeper.board.get(line).get(column).isRevealed = true;
// panel.minesweeper.score--;
panel.repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
// panel.minesweeper.selectedLine = panel.getLine(e.getY());
// panel.minesweeper.selectedColumn = panel.getColumn(e.getX());
try {
int line = panel.getLine(e.getY());
int column = panel.getColumn(e.getX());
if (!panel.isOutOfBounds(line, column)) {
panel.minesweeper.board.get(line).get(column).setSelected(true);
panel.repaint();
}
} catch (Exception e2) {
// TODO: handle exception
// e2.printStackTrace();
}
}
}
package tp5.v1;
import java.awt.Color;
public class Cell {
boolean isRevealed;
boolean isMine;
boolean isSelected;
Color color;
int row;
int column;
boolean active;
public Cell(boolean isMine, int row, int column) {
this.isRevealed = false;
this.isSelected = false;
this.color = Color.GRAY;
this.isMine = isMine;
this.row = row;
this.column = column;
this.active = true;
}
private void refreshColor() {
if (this.isSelected)
this.color = Color.RED;
else if (!this.isRevealed)
this.color = Color.DARK_GRAY;
else
this.color = Color.lightGray;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
public void setRevealed(boolean status) {
this.isRevealed = status;
this.active = false;
if (status && !this.isMine)
this.color = Color.DARK_GRAY;
}
public void setMine(boolean isMine) {
this.isMine = isMine;
}
public boolean getIsSelected() {
return isSelected;
}
public Color getColor() {
if (active)
refreshColor();
return color;
}
public void setColor(Color color) {
this.color = color;
}
public boolean isRevealed() {
return isRevealed;
}
public boolean getIsMine() {
return isMine;
}
public void setActive(boolean active) {
this.active = active;
}
@Override
public String toString() {
if(!isRevealed)
return "C";
else if (isMine)
return "B";
return "V";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment