Commit 5d0419c9 authored by Nooaah's avatar Nooaah

First project commit

parents
Pipeline #1162 canceled with stages
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectKey">
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" default="true" project-jdk-name="13" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/WarGame.iml" filepath="$PROJECT_DIR$/WarGame.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<template>
<input-field default="com.company">IJ_BASE_PACKAGE</input-field>
</template>
\ No newline at end of file
<?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>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
# WarGame Project ![IMT Project](https://img.shields.io/badge/IMT-project-blue) ![Java](https://img.shields.io/badge/language-java-red)
The WarGame is a project that was proposed to us during our second year of DUT, which aimed to improve our knowledge in Object Oriented Programming.
Unfortunately, because of the health crisis, we did not have the opportunity to work on this project, which was therefore abandoned.
The object-oriented course at IMT Lille Douai was a very good opportunity to realize this project.<br>
The only code we were provided with was the *gui* file, which contains the basic shapes for our game.
## Installation
```bash
git clone http://gvipers.imt-lille-douai.fr/noah.chatelain/wargame.git
```
## Usage
```bash
cd WarGame/src/com
java wargame/Carre
```
## Contributing
- Noah Châtelain
- Faustine Fiolet
- Ambroise Gyre
Please make sure to update tests as appropriate.
## License
[MIT](https://choosealicense.com/licenses/mit/)
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
package wargame;
import wargame.gui.Tile;
import wargame.gui.TileLayout;
import wargame.gui.hex.HexLayout;
import wargame.gui.hex.HexTile;
import wargame.gui.square.SquareLayout;
import wargame.gui.square.SquareTile;
import java.awt.event.*;
import wargame.Map;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class Army {
public int size = 0;
public Army() {}
public int getSizeArmy() {
return this.size;
}
public void setSizeArmy(int size) {
this.size = size;
}
}
\ No newline at end of file
package wargame;
import wargame.gui.Tile;
import wargame.gui.TileLayout;
import wargame.gui.hex.HexLayout;
import wargame.gui.hex.HexTile;
import wargame.gui.square.SquareLayout;
import wargame.gui.square.SquareTile;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.*;
import java.awt.Color;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.swing.filechooser.*;
import java.io.File;
/**
* @author Noah Chatelain
* @author Thierry Xu
* @author Leo Heumel
*/
public abstract class Carre {
public static void listFilesForFolder(final File folder, int styleOfMap, JMenu submenuCarre) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry, styleOfMap, submenuCarre);
} else {
if (fileEntry.getName().equals(".DS_Store") == false)
{
JMenuItem item;
item = new JMenuItem(new AbstractAction(fileEntry.getName().substring(0, fileEntry.getName().length() - 4)) {
public void actionPerformed(ActionEvent e) {
String command = "java wargame/Carre " + fileEntry.getName() + " " + styleOfMap;
try {
Process proc = Runtime.getRuntime().exec(command);
System.exit(-1);
}
catch(IOException event) {
event.printStackTrace();
}
}
});
submenuCarre.add(item);
}
}
}
}
public static void main(String[] args) {
int numberMap = 1;
String[][] tab;
Map map = new Map();
String title = "map1.map";
try {
map.readFile("wargame/maps/" + args[0]);
title = args[0];
} catch (ArrayIndexOutOfBoundsException e) {
map.readFile("wargame/maps/Map1.map");
}
tab = map.tabMap;
PlayerRound playerRound = new PlayerRound();
final int width = map.width;
final int height = map.height;
int interStyleMap;
try {
interStyleMap = Integer.parseInt(args[1]);
} catch (ArrayIndexOutOfBoundsException e) {
interStyleMap = 1;
}
TileLayout squareLayout;
JPanel squarePanel;
if (interStyleMap == 1) {
squareLayout = new SquareLayout(height, width);
squarePanel = new JPanel(squareLayout);
squarePanel.setPreferredSize(squareLayout.getPreferredDimension(800));
} else {
squareLayout = new HexLayout(height, width);
squarePanel = new JPanel(squareLayout);
squarePanel.setPreferredSize(squareLayout.getPreferredDimension(800));
}
BufferedImage grass = null;
BufferedImage water = null;
BufferedImage ground = null;
BufferedImage sand = null;
BufferedImage ice = null;
//Create TextField Hello Word
JTextField textField = new JTextField();
textField.setHorizontalAlignment(JTextField.CENTER);
textField.setText("La partie commence avec " + playerRound.getPlayerName1() + ", GO ! Armée de " + playerRound.getNumberArmyDeployed());
textField.setForeground(Color.red);
textField.setEditable(false);
JTextField textFieldRed = new JTextField();
textFieldRed.setHorizontalAlignment(JTextField.CENTER);
textFieldRed.setText(playerRound.getPlayerName1() + " | " + playerRound.getTotalRed());
textFieldRed.setForeground(Color.red);
textFieldRed.setEditable(false);
JTextField textFieldBlue = new JTextField();
textFieldBlue.setHorizontalAlignment(JTextField.CENTER);
textFieldBlue.setText(playerRound.getPlayerName2() + " | " + playerRound.getTotalBlue());
textFieldBlue.setForeground(Color.blue);
textFieldBlue.setEditable(false);
JMenuBar mb = new JMenuBar();
JMenu menu, submenuMap, submenuCarre, submenuHex;
JMenuItem restartPlay, closeMenu, changeNamePlayers, i3, i4, i5, i6, openMap;
menu = new JMenu("Menu");
submenuMap = new JMenu("Changer de Map");
final int nM = numberMap;
//MENU
restartPlay = new JMenuItem(new AbstractAction("Recommencer") {
public void actionPerformed(ActionEvent e) {
String command = "java wargame/Carre " + args[0];
try {
Process proc = Runtime.getRuntime().exec(command);
System.exit(-1);
} catch(IOException event) {
event.printStackTrace();
}
//TODO: Refresh le contentPane a la place
}
});
closeMenu = new JMenuItem(new AbstractAction("Fermer") {
public void actionPerformed(ActionEvent e) {
System.exit(-1);
}
});
changeNamePlayers = new JMenuItem(new AbstractAction("Changer le nom des joueurs") {
public void actionPerformed(ActionEvent e) {
JFrame f = new JFrame("Changer le nom des joueurs");
f.setLocationRelativeTo(null);
JPanel contentPane2 = new JPanel();
contentPane2.setLayout(new BoxLayout(contentPane2, BoxLayout.Y_AXIS));
f.setContentPane(contentPane2);
JLabel label1 = new JLabel("Nom joueur n°1");
JTextField textfield1 = new JTextField(playerRound.getPlayerName1(), 20);
JLabel label2 = new JLabel("Nom joueur n°2");
JTextField textfield2 = new JTextField(playerRound.getPlayerName2(), 20);
JButton buttonAddPlayer;
buttonAddPlayer = new JButton(new AbstractAction("Continuer la partie") {
public void actionPerformed(ActionEvent e) {
playerRound.setPlayerName1(textfield1.getText());
textFieldRed.setText(playerRound.getPlayerName1() + " | " + playerRound.getTotalRed());
playerRound.setPlayerName2(textfield2.getText());
textFieldBlue.setText(playerRound.getPlayerName2() + " | " + playerRound.getTotalBlue());
f.setVisible(false);
}
});
contentPane2.add(label1);
contentPane2.add(textfield1);
contentPane2.add(label2);
contentPane2.add(textfield2);
contentPane2.add(buttonAddPlayer);
f.pack();
f.setVisible(true);
}
});
openMap = new JMenuItem(new AbstractAction("Ouvrir ma propre carte") {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser("./wargame/maps/");
FileNameExtensionFilter filter = new FileNameExtensionFilter("Only map format", "map");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
//System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
String command = "java wargame/Carre " + chooser.getSelectedFile().getName();
try {
Process proc = Runtime.getRuntime().exec(command);
System.exit(-1);
}
catch(IOException event) {
event.printStackTrace();
}
}
}
});
//SUB MENU
submenuCarre = new JMenu("Carre");
submenuHex = new JMenu("Hexagonal");
final File folder = new File("./wargame/maps/");
listFilesForFolder(folder, 1, submenuCarre);
listFilesForFolder(folder, 2, submenuHex);
menu.add(changeNamePlayers);
menu.add(restartPlay);
menu.add(closeMenu);
menu.add(openMap);
submenuMap.add(submenuCarre);
submenuMap.add(submenuHex);
menu.add(submenuMap);
mb.add(menu);
try {
grass = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("assets/grass.png"));
water = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("assets/water.jpg"));
ground = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("assets/ground.png"));
sand = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("assets/sand.png"));
ice = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("assets/ice.png"));
} catch (IOException e) {
System.out.println("ERROR: file not found");
} catch (NullPointerException e) {
System.err.println("ERROR: an unexpected error occurred");
}
if (grass == null || water == null || ground == null || sand == null || ice == null) {
// si on n'a pas réussi à charger l'image, on quitte le programme
System.exit(-1);
}
for (int row = 0 ; row < height ; row++) {
for (int column = 0 ; column < width ; column++) {
final int r = row;
final int c = column;
final int styleMap = interStyleMap;
Tile squareTile;
if (tab[row][column].equals("0")) {
if (styleMap == 1) {
squareTile = new SquareTile("", water);
} else {
squareTile = new HexTile("", water);
}
} else if (tab[row][column].equals("1")) {
if (styleMap == 1) {
squareTile = new SquareTile("", grass);
} else {
squareTile = new HexTile("", grass);
}
} else if (tab[row][column].equals("2")) {
if (styleMap == 1) {
squareTile = new SquareTile("", ground);
} else {
squareTile = new HexTile("", ground);
}
} else if (tab[row][column].equals("3")) {
if (styleMap == 1) {
squareTile = new SquareTile("", sand);
} else {
squareTile = new HexTile("", sand);
}
} else if (tab[row][column].equals("4")) {
if (styleMap == 1) {
squareTile = new SquareTile("", ice);
} else {
squareTile = new HexTile("", ice);
}
}
else {
if (styleMap == 1) {
squareTile = new SquareTile("", grass);
} else {
squareTile = new HexTile("", grass);
}
}
squareTile.field.set(tab[row][column]);
squareTile.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (map.get(r, c).army.getSizeArmy() == 0 && map.get(r, c).field.get().equals("0") != true) {
squareTile.army.setSizeArmy(playerRound.getNumberArmyDeployed());
squareTile.setTextContent("" + squareTile.army.getSizeArmy());
playerRound.setNumberArmyDeployed();
if (playerRound.getPlayerRound() == 1) {
textField.setText(playerRound.getPlayerName2() + " : Armée de " + playerRound.getNumberArmyDeployed());
textField.setForeground(Color.blue);
squareTile.player.setNumberPlayer(1);
squareTile.setTextBackgroundColor(255, 0, 0);
playerRound.setPlayerRound(2);
} else {
textField.setText(playerRound.getPlayerName1() + " : Armée de " + playerRound.getNumberArmyDeployed());
textField.setForeground(Color.red);
squareTile.player.setNumberPlayer(2);
squareTile.setTextBackgroundColor(0, 0, 255);
playerRound.setPlayerRound(1);
}
try {
if (map.get(r + 1, c).player.getNumberPlayer() != map.get(r, c).player.getNumberPlayer()) {
if (map.get(r, c).army.getSizeArmy() > map.get(r + 1, c).army.getSizeArmy() && map.get(r + 1, c).getTextContent() != "")
{
map.get(r + 1, c).player.setNumberPlayer(map.get(r, c).player.getNumberPlayer());
if (map.get(r + 1, c).player.getNumberPlayer() != 1) {
map.get(r + 1, c).setTextBackgroundColor(0, 0, 255);
} else {
map.get(r + 1, c).setTextBackgroundColor(255, 0, 0);
}
}
}
else {
if (map.get(r, c).army.getSizeArmy() > map.get(r + 1, c).army.getSizeArmy() && map.get(r + 1, c).getTextContent() != "")
{
map.get(r + 1, c).army.setSizeArmy(map.get(r + 1, c).army.getSizeArmy() + 1);
map.get(r + 1, c).setTextContent("" + map.get(r + 1, c).army.getSizeArmy());
}
}
} catch(NullPointerException e1) {
} catch(ArrayIndexOutOfBoundsException e2) {
}
try {
if (map.get(r - 1, c).player.getNumberPlayer() != map.get(r, c).player.getNumberPlayer()) {
if (map.get(r, c).army.getSizeArmy() > map.get(r - 1, c).army.getSizeArmy() && map.get(r - 1, c).getTextContent() != "")
{
map.get(r - 1, c).player.setNumberPlayer(map.get(r, c).player.getNumberPlayer());
if (map.get(r - 1, c).player.getNumberPlayer() != 1) {
map.get(r - 1, c).setTextBackgroundColor(0, 0, 255);
} else {
map.get(r - 1, c).setTextBackgroundColor(255, 0, 0);
}
}
}
else {
if (map.get(r, c).army.getSizeArmy() > map.get(r - 1, c).army.getSizeArmy() && map.get(r - 1, c).getTextContent() != "")
{
map.get(r - 1, c).army.setSizeArmy(map.get(r - 1, c).army.getSizeArmy() + 1);
map.get(r - 1, c).setTextContent("" + map.get(r - 1, c).army.getSizeArmy());
}
}
} catch(NullPointerException e1) {
} catch(ArrayIndexOutOfBoundsException e2) {
}
try {
if (map.get(r, c + 1).player.getNumberPlayer() != map.get(r, c).player.getNumberPlayer()) {
if (map.get(r, c).army.getSizeArmy() > map.get(r, c + 1).army.getSizeArmy() && map.get(r, c + 1).getTextContent() != "")
{
map.get(r, c + 1).player.setNumberPlayer(map.get(r, c).player.getNumberPlayer());
if (map.get(r, c + 1).player.getNumberPlayer() != 1) {
map.get(r, c + 1).setTextBackgroundColor(0, 0, 255);
} else {
map.get(r, c + 1).setTextBackgroundColor(255, 0, 0);
}
}
}
else {
if (map.get(r, c).army.getSizeArmy() > map.get(r, c + 1).army.getSizeArmy() && map.get(r, c + 1).getTextContent() != "")
{
map.get(r, c + 1).army.setSizeArmy(map.get(r, c + 1).army.getSizeArmy() + 1);
map.get(r, c + 1).setTextContent("" + map.get(r, c + 1).army.getSizeArmy());
}
}
} catch(NullPointerException e1) {
} catch(ArrayIndexOutOfBoundsException e2) {
}
try {
if (map.get(r, c - 1).player.getNumberPlayer() != map.get(r, c).player.getNumberPlayer()) {
if (map.get(r, c).army.getSizeArmy() > map.get(r, c - 1).army.getSizeArmy() && map.get(r, c - 1).getTextContent() != "")
{
map.get(r, c - 1).player.setNumberPlayer(map.get(r, c).player.getNumberPlayer());
if (map.get(r, c - 1).player.getNumberPlayer() != 1) {
map.get(r, c - 1).setTextBackgroundColor(0, 0, 255);
} else {
map.get(r, c - 1).setTextBackgroundColor(255, 0, 0);
}
}
}
else {
if (map.get(r, c).army.getSizeArmy() > map.get(r, c - 1).army.getSizeArmy() && map.get(r, c - 1).getTextContent() != "")
{
map.get(r, c - 1).army.setSizeArmy(map.get(r, c - 1).army.getSizeArmy() + 1);
map.get(r, c - 1).setTextContent("" + map.get(r, c - 1).army.getSizeArmy());
}
}
} catch(NullPointerException e1) {
} catch(ArrayIndexOutOfBoundsException e2) {
}
int totalRed = 0;
int totalBlue = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (map.get(i, j).player.getNumberPlayer() == 1) {
totalRed += (int)map.get(i, j).army.getSizeArmy();
} else {
totalBlue += (int)map.get(i, j).army.getSizeArmy();
}
}
}
playerRound.setTotalRed(totalRed);
playerRound.setTotalBlue(totalBlue);
textFieldRed.setText(playerRound.getPlayerName1() + " | " + playerRound.getTotalRed());
textFieldBlue.setText(playerRound.getPlayerName2() + " | " + playerRound.getTotalBlue());
}
}
});
//System.out.println(map.get(3, 1));
map.cells[row][column] = squareTile;
squarePanel.add(squareTile);
}
}
JPanel contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
JPanel contentTextField = new JPanel();
contentTextField.setLayout(new BoxLayout(contentTextField, BoxLayout.X_AXIS));
JFrame frame = new JFrame("Wargame (" + title + ")");
frame.setContentPane(contentPane);
frame.setLocationRelativeTo(null);
frame.setJMenuBar(mb);
contentPane.add(squarePanel);
contentTextField.add(textFieldRed);
contentTextField.add(textField);
contentTextField.add(textFieldBlue);
contentPane.add(contentTextField);
// on demande à notre JFrame de se dimensionner automatiquement en fonction de la taille de son contenu
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
/*
JFrame f = new JFrame("Text Field Examples");
JPanel contentPane2 = new JPanel();
contentPane2.setLayout(new BoxLayout(contentPane2, BoxLayout.Y_AXIS));
f.setContentPane(contentPane2);
JLabel label1 = new JLabel("Nom joueur n°1");
JTextField textfield1 = new JTextField("Text field 1", 20);
JLabel label2 = new JLabel("Nom joueur n°2");
JTextField textfield2 = new JTextField("Text field 2", 20);
JButton buttonAddPlayer;
buttonAddPlayer = new JButton(new AbstractAction("Jouer !") {
public void actionPerformed(ActionEvent e) {
playerRound.setPlayerName1(textfield1.getText());
textFieldRed.setText(playerRound.getPlayerName1() + " | " + playerRound.getTotalRed());
playerRound.setPlayerName2(textfield2.getText());
textFieldBlue.setText(playerRound.getPlayerName2() + " | " + playerRound.getTotalRed());
f.setVisible(false);
}
});
contentPane2.add(label1);
contentPane2.add(textfield1);
contentPane2.add(label2);
contentPane2.add(textfield2);
contentPane2.add(buttonAddPlayer);
f.pack();
f.setVisible(true);
*/
}
}
\ No newline at end of file
package wargame;
import wargame.gui.Tile;
import wargame.gui.TileLayout;
import wargame.gui.hex.HexLayout;
import wargame.gui.hex.HexTile;
import wargame.gui.square.SquareLayout;
import wargame.gui.square.SquareTile;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class Field {
public String field = "0";
public Field() {}
public String get() {
return this.field;
}
public void set(String field) {
this.field = field;
}
}
\ No newline at end of file
package wargame;
import wargame.gui.Tile;
import wargame.gui.TileLayout;
import wargame.gui.hex.HexLayout;
import wargame.gui.hex.HexTile;
import wargame.gui.square.SquareLayout;
import wargame.gui.square.SquareTile;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
final int width = 8;
final int height = 5;
/*
Préparation de 2 JPanel, un pour chaque 2 grilles
*/
TileLayout hexLayout = new HexLayout(height, width);
TileLayout squareLayout = new SquareLayout(height, width);
// on fournit un HexLayout pour définir un agencement en grille hexagonale des composants dans ce JPanel
JPanel hexPanel = new JPanel(hexLayout);
// on fournit un SquareLayout pour définir un agencement en grille carrée des composants dans ce JPanel
JPanel squarePanel = new JPanel(squareLayout);
/*
on demande aux layouts de fournir la dimension optimale pour chaque JPanel, à partir d'une largeur souhaitée
(ici : 600 pixels)
*/
squarePanel.setPreferredSize(squareLayout.getPreferredDimension(600));
hexPanel.setPreferredSize(hexLayout.getPreferredDimension(600));
/*
Chargement du fichier image qui servira de décoration aux tuiles graphiques
*/
BufferedImage image = null;
try {
image = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("assets/grass.png"));
} catch (IOException e) {
System.out.println("ERROR: file not found");
} catch (NullPointerException e) {
System.err.println("ERROR: an unexpected error occurred");
}
if (image == null) {
// si on n'a pas réussi à charger l'image, on quitte le programme
System.exit(-1);
}
/*
Remplissage des JPanel avec des tuiles graphiques
*/
for (int row = 0 ; row < height ; row++) {
for (int column = 0 ; column < width ; column++) {
// construction et ajout d'une HexTile au JPanel agencé en grille hexagonale
Tile hexTile = new HexTile(row + " ; " + column, image);
hexPanel.add(hexTile);
// construction et ajout d'une SquareTile au JPanel agencé en grille carrée
Tile squareTile = new SquareTile(row + " ; " + column, image);
squarePanel.add(squareTile);
}
}
// on construit le contentPane de notre JFrame, et on y ajoute les JPanel contenant nos grilles
JPanel contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
contentPane.add(squarePanel);
contentPane.add(hexPanel);
// on construit notre JFrame et on définit son contentPane
JFrame frame = new JFrame("Deux grilles de tuiles graphiques");
frame.setContentPane(contentPane);
// on demande à notre JFrame de se dimensionner automatiquement en fonction de la taille de son contenu
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
package wargame;
import wargame.gui.Tile;
import wargame.gui.TileLayout;
import wargame.gui.hex.HexLayout;
import wargame.gui.hex.HexTile;
import wargame.gui.square.SquareLayout;
import wargame.gui.square.SquareTile;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import wargame.Army;
import wargame.Player;
import wargame.PlayerRound;
import wargame.Field;
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files
import java.util.ArrayList; // import the ArrayList class
public class Map {
public int width = 15;
public int height = 8;
String[][] tabMap;
//TODO: il faut agir
public Tile[][] cells = new Tile[200][200];
public Map() {}
public Tile get(int x, int y) {
return this.cells[x][y];
}
public void readFile(String road) {
try {
String[][] dataMap = new String[200][200];
File myObj = new File(road);
Scanner myReader = new Scanner(myObj);
int i = 0;
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
//System.out.println(data);
dataMap[i] = data.split(",");
i++;
}
myReader.close();
String[][] dataMap2 = new String[i][dataMap[0].length]; //12 16
for (int j = 0 ; j < i ; j++) {
for (int k = 0 ; k < dataMap[0].length ; k++) {
dataMap2[j][k] = dataMap[j][k];
}
}
this.width = dataMap[0].length;
this.height = i;
this.tabMap = dataMap2;
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
\ No newline at end of file
package wargame;
import wargame.gui.Tile;
import wargame.gui.TileLayout;
import wargame.gui.hex.HexLayout;
import wargame.gui.hex.HexTile;
import wargame.gui.square.SquareLayout;
import wargame.gui.square.SquareTile;
import java.awt.event.*;
import wargame.Map;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class Player {
int numberPlayer = 1;
//TODO: accepter un paramètre et virer le setter
public Player() {}
public int getNumberPlayer() {
return this.numberPlayer;
}
public void setNumberPlayer(int numberPlayer) {
this.numberPlayer = numberPlayer;
}
}
\ No newline at end of file
package wargame;
import wargame.gui.Tile;
import wargame.gui.TileLayout;
import wargame.gui.hex.HexLayout;
import wargame.gui.hex.HexTile;
import wargame.gui.square.SquareLayout;
import wargame.gui.square.SquareTile;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
//TODO: voir pour passer à N joueurs
public class PlayerRound {
int PlayerRound = 1;
int totalRed = 0;
int totalBlue = 0;
int numberArmyDeployed = (int)Math.round(Math.random() * (5 - 1) + 1);
String playerName1 = "J1";
String playerName2 = "J2";
public PlayerRound() {}
public void setPlayerRound(int PlayerRound) {
this.PlayerRound = PlayerRound;
}
public int getPlayerRound() {
return this.PlayerRound;
}
public void setTotalRed(int totalRed) {
this.totalRed = totalRed;
}
public int getTotalRed() {
return this.totalRed;
}
public void setTotalBlue(int totalBlue) {
this.totalBlue = totalBlue;
}
public int getTotalBlue() {
return this.totalBlue;
}
public int getNumberArmyDeployed() {
return this.numberArmyDeployed;
}
public void setNumberArmyDeployed() {
this.numberArmyDeployed = (int)Math.round(Math.random() * (5 - 1) + 1);
}
public void setPlayerName1(String playerName) {
this.playerName1 = playerName;
}
public String getPlayerName1() {
return this.playerName1;
}
public void setPlayerName2(String playerName) {
this.playerName2 = playerName;
}
public String getPlayerName2() {
return this.playerName2;
}
}
\ No newline at end of file
package wargame.gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import wargame.Army;
import wargame.Player;
import wargame.PlayerRound;
import wargame.Field;
/**
* Base class of graphical tiles
* This software is released under the GNU GPLv3 license
*/
public abstract class Tile extends JComponent implements MouseListener {
private String text = "";
protected BufferedImage tile;
private BufferedImage scaledTile;
private boolean highlightable = true;
private Color highlightColor = new Color(255, 0, 0, 64);
protected Polygon border = new Polygon();
protected Rectangle boundingBox = new Rectangle();
private boolean isHighlighted = false;
private int previousHeight = 0;
private int previousWidth = 0;
private Color textBackgroundColor = new Color(64, 64, 64);
public Army army = new Army();
public Player player = new Player();
public Field field = new Field();
public Tile(String text, BufferedImage image) {
this.text = text;
this.recomputeBorder();
this.addMouseListener(this);
this.setTile(image);
Font f = new Font(Font.SANS_SERIF, Font.BOLD, 10);
this.setFont(f);
this.setCursor(new Cursor(Cursor.HAND_CURSOR));
this.setLayout(new FlowLayout());
}
/**
* Functions and methods to play
*/
public void setTextBackgroundColor(int r, int g, int b) {
this.textBackgroundColor = new Color(r, g, b);
this.repaint();
}
public Color getTextBackgroundColor() {
return this.textBackgroundColor;
}
/**
* Provides a shape-specific border computation of the attribute border
*/
protected abstract void recomputeBorder();
@Override
public boolean contains(Point p) {
return border.contains(p);
}
@Override
public boolean contains(int x, int y) {
return border.contains(x, y);
}
@Override
public void setSize(Dimension d) {
super.setSize(d);
this.recomputeBorder();
}
@Override
public void setSize(int w, int h) {
super.setSize(w, h);
this.recomputeBorder();
}
@Override
public void setBounds(int x, int y, int width, int height) {
super.setBounds(x, y, width, height);
this.recomputeBorder();
}
@Override
public void setBounds(Rectangle r) {
super.setBounds(r.x, r.y, r.width, r.height);
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
if (this.army.getSizeArmy() == 0) {
if (this.field.get().equals("0") != true) {
setHighlightColor(new Color(0, 255, 0, 64));
} else {
setHighlightColor(new Color(255, 0, 0, 64));
}
} else {
setHighlightColor(new Color(255, 0, 0, 64));
}
this.setHighlighted(true);
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
this.setHighlighted(false);
}
@Override
public void mouseClicked(MouseEvent mouseEvent) {
if (this.army.getSizeArmy() == 0) {
if (this.field.get() != "0") {
this.setHighlighted(false);
}
else {
this.setHighlighted(true);
}
}
}
@Override
public void mousePressed(MouseEvent mouseEvent) {}
@Override
public void mouseReleased(MouseEvent mouseEvent) {}
/**
*
* Draws additional content inside the tile (default implementation : some text in a circle)
*
* @param g2D Graphic context
*/
protected void drawContents(Graphics2D g2D) {
if (this.text.length() == 0) return;
FontMetrics fontMetrics = getFontMetrics(this.getFont());
Rectangle iconR = new Rectangle();
Rectangle textR = new Rectangle();
SwingUtilities.layoutCompoundLabel(
null,
fontMetrics,
text,
null,
SwingUtilities.CENTER,
SwingUtilities.CENTER,
SwingUtilities.BOTTOM,
SwingUtilities.CENTER,
this.getBounds(),
iconR,
textR,
0
);
Point loc = getLocation();
//System.out.println(loc);
g2D.setColor(Color.white);
int margin = 5;
g2D.fillOval(
textR.x - loc.x - margin,
textR.y - loc.y - textR.width / 2 + fontMetrics.getHeight() / 2 + this.boundingBox.height / 4 - margin,
textR.width + 2 * margin,
textR.width + 2 * margin
);
g2D.setColor(this.textBackgroundColor);
margin = 3;
g2D.fillOval(
textR.x - loc.x - margin,
textR.y - loc.y - textR.width / 2 + fontMetrics.getHeight() / 2 + this.boundingBox.height / 4 - margin,
textR.width + 2 * margin,
textR.width + 2 * margin
);
g2D.setColor(Color.white);
g2D.drawString(
text,
textR.x - loc.x,
textR.y - loc.y + fontMetrics.getAscent() + this.boundingBox.height / 4
);
}
private void rescaleTile() {
AffineTransform transform = AffineTransform.getScaleInstance(((double) this.boundingBox.width) / this.tile.getWidth(), ((double) this.boundingBox.height) / this.tile.getHeight());
BufferedImage scaled = new BufferedImage(this.boundingBox.width, this.boundingBox.height, BufferedImage.TYPE_INT_ARGB);
AffineTransformOp scaleOp = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
this.scaledTile = scaleOp.filter(this.tile, scaled);
this.scaledTile = new BufferedImage(this.boundingBox.width, this.boundingBox.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2D = this.scaledTile.createGraphics();
g2D.setClip(this.border);
g2D.drawImage(scaled, 0, 0, null);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2D = (Graphics2D) (g.create());
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
if (this.previousHeight != this.boundingBox.height || this.previousWidth != this.boundingBox.width) {
this.rescaleTile();
this.previousWidth = this.boundingBox.width;
this.previousHeight = this.boundingBox.height;
}
g2D.drawImage(this.scaledTile, 0, 0, null);
this.drawContents(g2D);
// highlights the tile if hovered by mouse
if (this.isHighlighted && this.highlightable) {
g2D.setColor(this.highlightColor);
g2D.fillPolygon(this.border);
}
// draw border
g2D.setColor(Color.black);
g2D.drawPolygon(border);
g2D.dispose();
}
@Override
protected void paintBorder(Graphics g) {
// no border, thanks
}
/**
* Changes the color used to render the circle around the (text) content
* @param color
*/
public void setTextBackgroundColor(Color color) {
this.textBackgroundColor = color;
}
/**
* sets if this tile is highlightable
* @param highlightable
*/
public void setHighlightable(boolean highlightable) {
this.highlightable = highlightable;
}
/**
* sets the color used to render a highlighted tile. Should be a RGBA color
* @param color the new (RGBA) color
*/
public void setHighlightColor(Color color) {
this.highlightColor = color;
}
/**
* forces a tile to be highlighted or not
* @param highlighted
*/
public void setHighlighted(boolean highlighted) {
this.isHighlighted = highlighted;
this.repaint();
}
/**
* sets new text content for the tile
* @param text
*/
public void setTextContent(String text) {
this.text = text;
this.invalidate();
this.repaint();
}
public String getTextContent() {
return this.text;
}
/**
* sets a new image background for the tile
* @param image
*/
public void setTile(BufferedImage image) {
this.tile = image;
this.previousHeight = -1;
this.previousWidth = -1;
}
}
package wargame.gui;
import java.awt.*;
/**
* Common interface of Tile Layout Managers
* This software is released under the GNU GPLv3 license
*/
public interface TileLayout extends LayoutManager {
/**
* @param width desired width for a container using this layout manager
* @return Aspect-ratio correct corresponding dimension
*/
public Dimension getPreferredDimension(int width);
}
package wargame.gui.hex;
import wargame.gui.TileLayout;
import java.awt.*;
/**
* This layout manager is based on java.awt.GridLayout
* <p>
* The <code>GridLayout</code> class is a layout manager that
* lays out a container's components in a hexagonal grid.
* The container is divided into equal-sized hexagons,
* and one component is placed in each hexagon.
*
* @author keang
* @date 5 Jun 2009
*/
public class HexLayout implements TileLayout {
private static final long serialVersionUID = 42L;
/**
* This is the gap (in pixels) which specifies the space
* between components. They can be changed at any time.
* This should be a non-negative integer.
*
* @serial
*/
int cgap;
/**
* This is the number of rows specified for the grid. The number
* of rows can be changed at any time.
* This should be a non negative integer, where '0' means
* 'any number' meaning that the number of Rows in that
* dimension depends on the other dimension.
*
* @serial
* @see #getRows()
* @see #setRows(int)
*/
int rows;
/**
* This is the number of columns specified for the grid. The number
* of columns can be changed at any time.
* This should be a non negative integer, where '0' means
* 'any number' meaning that the number of Columns in that
* dimension depends on the other dimension.
*
* @serial
* @see #getColumns()
* @see #setColumns(int)
*/
int cols;
/**
* Creates a grid layout with a default of one column per component,
* in a single row.
*
* @since JDK1.1
*/
public HexLayout() {
this(1, 0, 0);
}
/**
* Creates a grid layout with the specified number of rows and
* columns. All components in the layout are given equal size.
* <p>
* One, but not both, of <code>rows</code> and <code>cols</code> can
* be zero, which means that any number of objects can be placed in a
* row or in a column.
*
* @param rows the rows, with the value zero meaning
* any number of rows.
* @param columns the columns, with the value zero meaning
* any number of columns.
*/
public HexLayout(int rows, int columns) {
this(rows, columns, 0);
}
/**
* Creates a grid layout with the specified number of rows and
* columns. All components in the layout are given equal size.
* <p>
* In addition, the gap between components is set to the
* specified value.
* <p>
* One, but not both, of <code>rows</code> and <code>cols</code> can
* be zero, which means that any number of objects can be placed in a
* row or in a column.
* <p>
* All <code>GridLayout</code> constructors defer to this one.
*
* @param rows the rows, with the value zero meaning
* any number of rows
* @param columns the columns, with the value zero meaning
* any number of columns
* @param hgap the gap around the component
* @throws IllegalArgumentException if the value of both
* <code>rows</code> and <code>cols</code> is
* set to zero
*/
public HexLayout(int rows, int columns, int hgap) {
if ((rows == 0) && (columns == 0)) {
throw new IllegalArgumentException("rows and cols cannot both be zero");
}
this.rows = rows;
this.cols = columns;
this.cgap = hgap;
}
/**
* Gets the number of rows in this layout.
*
* @return the number of rows in this layout
*/
public int getRows() {
return rows;
}
/**
* Sets the number of rows in this layout to the specified value.
*
* @param rows the number of rows in this layout
* @throws IllegalArgumentException if the value of both
* <code>rows</code> and <code>cols</code> is set to zero
*/
public void setRows(int rows) {
if ((rows == 0) && (this.cols == 0)) {
throw new IllegalArgumentException("rows and cols cannot both be zero");
}
this.rows = rows;
}
/**
* Gets the number of columns in this layout.
*
* @return the number of columns in this layout
*/
public int getColumns() {
return cols;
}
/**
* Sets the number of columns in this layout to the specified value.
* Setting the number of columns has no affect on the layout
* if the number of rows specified by a constructor or by
* the <tt>setRows</tt> method is non-zero. In that case, the number
* of columns displayed in the layout is determined by the total
* number of components and the number of rows specified.
*
* @param columns the number of columns in this layout
* @throws IllegalArgumentException if the value of both
* <code>rows</code> and <code>cols</code> is set to zero
*/
public void setColumns(int columns) {
if ((columns == 0) && (this.rows == 0)) {
throw new IllegalArgumentException("rows and cols cannot both be zero");
}
this.cols = columns;
}
/**
* Gets the gap between components.
*
* @return the gap between components
*/
public int getGap() {
return cgap;
}
/**
* Sets the gap between components to the specified value.
*
* @param gap the gap between components
*/
public void setGap(int gap) {
this.cgap = gap;
}
/**
* Adds the specified component with the specified name to the layout.
*
* @param name the name of the component
* @param comp the component to be added
*/
public void addLayoutComponent(String name, Component comp) {
// do nothing
}
/**
* Removes the specified component from the layout.
*
* @param comp the component to be removed
*/
public void removeLayoutComponent(Component comp) {
// do nothing
}
/**
* Determines the preferred size of the container argument using
* this grid layout.
* <p>
* The preferred width of a grid layout is the largest preferred width
* of all of the components in the container times the number of columns,
* plus the horizontal padding times the number of columns minus one,
* plus the left and right insets of the target container, plus the width
* of half a component if there is more than one row.
* <p>
* The preferred height of a grid layout is the largest preferred height
* of all of the components in the container plus three quarters of the
* largest minimum height of all of the components in the container times
* the number of rows greater than one,
* plus the vertical padding times the number of rows minus one, plus
* the top and bottom insets of the target container.
*
* @param parent the container in which to do the layout
* @return the preferred dimensions to lay out the
* subcomponents of the specified container
* @see java.awt.GridLayout#minimumLayoutSize
* @see java.awt.Container#getPreferredSize()
*/
public Dimension preferredLayoutSize(Container parent) {
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = rows;
int ncols = cols;
if (nrows > 0) {
ncols = (ncomponents + nrows - 1) / nrows;
} else {
nrows = (ncomponents + ncols - 1) / ncols;
}
int w = 0;
int h = 0;
for (int i = 0; i < ncomponents; i++) {
Component comp = parent.getComponent(i);
Dimension d = comp.getPreferredSize();
if (w < d.width) {
w = d.width;
}
if (h < d.height) {
h = d.height;
}
}
int dx = insets.left + insets.right + ncols * w + (ncols - 1) * cgap;
int dy = insets.top + insets.bottom + nrows * h + (nrows - 1) * cgap;
if (nrows > 1) {
dx += w / 2;
dy /= nrows;
dy += (3 * dy * (nrows - 1)) / 4;
}
return new Dimension(dx, dy);
}
}
/**
* Determines the minimum size of the container argument using this
* grid layout.
* <p>
* The minimum width of a grid layout is the largest minimum width
* of all of the components in the container times the number of columns,
* plus the horizontal padding times the number of columns minus one,
* plus the left and right insets of the target container, plus the width
* of half a component if there is more than one row.
* <p>
* The minimum height of a grid layout is the largest minimum height
* of all of the components in the container plus three quarters of the
* largest minimum height of all of the components in the container times
* the number of rows greater than one,
* plus the vertical padding times the number of rows minus one, plus
* the top and bottom insets of the target container.
*
* @param parent the container in which to do the layout
* @return the minimum dimensions needed to lay out the
* subcomponents of the specified container
* @see java.awt.GridLayout#preferredLayoutSize
* @see java.awt.Container#doLayout
*/
public Dimension minimumLayoutSize(Container parent) {
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = rows;
int ncols = cols;
if (nrows > 0) {
ncols = (ncomponents + nrows - 1) / nrows;
} else {
nrows = (ncomponents + ncols - 1) / ncols;
}
int w = 0;
int h = 0;
for (int i = 0; i < ncomponents; i++) {
Component comp = parent.getComponent(i);
Dimension d = comp.getMinimumSize();
if (w < d.width) {
w = d.width;
}
if (h < d.height) {
h = d.height;
}
}
int dx = insets.left + insets.right + ncols * w + (ncols - 1) * cgap;
int dy = insets.top + insets.bottom + nrows * h + (nrows - 1) * cgap;
if (nrows > 1) {
dx += dx + w / 2;
dy /= nrows;
dy += (3 * dy * (nrows - 1)) / 4;
}
return new Dimension(dx, dy);
}
}
/**
* Lays out the specified container using this layout.
* <p>
* This method reshapes the components in the specified target
* container in order to satisfy the constraints of the
* <code>GridLayout</code> object.
* <p>
* The grid layout manager determines the size of individual
* components by dividing the free space in the container into
* equal-sized portions according to the number of rows and columns
* in the layout. The container's free space equals the container's
* size minus any insets and any specified horizontal or vertical
* gap. All components in a grid layout are given the same size.
*
* @param parent the container in which to do the layout
* @see java.awt.Container
* @see java.awt.Container#doLayout
*/
public void layoutContainer(Container parent) {
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = rows;
int ncols = cols;
if (ncomponents == 0) {
return;
}
if (nrows > 0) {
ncols = (ncomponents + nrows - 1) / nrows;
} else {
nrows = (ncomponents + ncols - 1) / ncols;
}
int w = parent.getWidth() - (insets.left + insets.right);
int h = parent.getHeight() - (insets.top + insets.bottom);
w = (int) ((w - (ncols - 1) * cgap) / (ncols + (nrows > 1 ? 0.5f : 0.0f)));
float effectiveRows = 1 + ((nrows - 1) * 0.75f);
h = (int) ((h - (nrows - 1) * cgap) / effectiveRows);
int xoffset = (w + cgap) / 2;
int yoffset = (int) (h * 0.75f);
boolean staggeredRow = false;
for (int r = 0, y = insets.top; r < nrows; r++, y += yoffset + cgap) {
int offset = 0;
if (staggeredRow)
offset = xoffset;
for (int c = 0, x = insets.left; c < ncols; c++, x += w + cgap) {
int i = r * ncols + c;
if (i < ncomponents) {
parent.getComponent(i).setBounds(x + offset, y, w, h);
}
}
staggeredRow = !staggeredRow;
}
}
}
/**
* Returns the string representation of this grid layout's values.
*
* @return a string representation of this grid layout
*/
public String toString() {
return getClass().getName() + "[gap=" + cgap +
",rows=" + rows + ",cols=" + cols + "]";
}
@Override
public Dimension getPreferredDimension(int width) {
double w = ((double)width) / (this.cols + .5);
double h = 2 * w / 1.73205;
double nH = (1 + (this.rows - 1) * .75);
return new Dimension(width, (int)(nH * h));
}
}
package wargame.gui.hex;
import wargame.gui.Tile;
import java.awt.*;
import java.awt.image.BufferedImage;
/**
* Defines a Hexagonal tile, meant to be used with a HexLayout
* Basedased on keang's Hex button package http://www.keang.co.uk/hex.php
*/
public class HexTile extends Tile {
private static final long serialVersionUID = 42L;
public HexTile(String text, BufferedImage image) {
super(text, image);
}
@Override
protected void recomputeBorder() {
this.getBounds(this.boundingBox);
int width = getWidth() - 1;
int height = getHeight() - 1;
int ratio = height / 4;
int[] hexX = new int[6];
int[] hexY = new int[6];
computeCoords(width, height, ratio, hexX, hexY);
this.border = new Polygon(hexX, hexY, 6);
}
private void computeCoords(int width, int height, int ratio, int[] hexX, int[] hexY) {
hexX[0] = width / 2;
hexY[0] = 0;
hexX[1] = width;
hexY[1] = ratio;
hexX[2] = width;
hexY[2] = height - ratio;
hexX[3] = width / 2;
hexY[3] = height;
hexX[4] = 0;
hexY[4] = height - ratio;
hexX[5] = 0;
hexY[5] = ratio;
}
}
\ No newline at end of file
package wargame.gui.square;
import wargame.gui.TileLayout;
import java.awt.*;
public class SquareLayout extends GridLayout implements TileLayout {
private int rows;
private int columns;
double preferredAspectRatio;
public SquareLayout(int rows, int columns) {
super(rows, columns);
this.rows = rows;
this.columns = columns;
this.preferredAspectRatio = ((double)columns) / rows;
}
@Override
public Dimension getPreferredDimension(int width) {
return new Dimension(width, (int) (width/preferredAspectRatio));
}
}
package wargame.gui.square;
import wargame.gui.Tile;
import java.awt.*;
import java.awt.image.BufferedImage;
public class SquareTile extends Tile {
public SquareTile(String text, BufferedImage image) {
super(text, image);
}
public SquareTile(BufferedImage image) {
this("", image);
}
@Override
protected void recomputeBorder() {
this.getBounds(this.boundingBox);
int width = getWidth() - 1;
int height = getHeight() - 1;
this.border = new Polygon(
new int[] {0, width, width, 0},
new int[] {0, 0, height, height},
4
);
}
}
0,0,0,0,0,0,0
0,1,1,1,1,1,0
0,1,1,1,1,1,0
0,1,1,1,1,1,0
0,1,1,1,1,1,0
0,0,0,0,0,0,0
\ No newline at end of file
0,0,0,0,0,0,0,0,0,0,0,0,4,4,4,4,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,1,1,1,0,4,4,0,0,0,0,0,1,1,1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,1,4,4,1,0,4,4,0,0,0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0
0,0,1,4,4,1,4,1,4,1,0,4,0,0,0,0,0,1,0,0,0,1,1,1,1,1,1,1,4,4,4,4,1,4,4,1,0,0,0
0,1,4,1,1,1,1,1,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,4,1,1,4,1,4,1,1,0,0
0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,0,0
0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,0,0
0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0,0,1,0
0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,1,0,0
0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,1,1,0,0,0,0,0,1,1,1,1,0,0,0,0,0
0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0
0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0
0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0
0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0
0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0
0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0
0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,4,4,4,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0
0,0,0,0,0,0,0,0,1,1,0,0,0,4,4,4,0,4,4,4,4,4,4,4,0,4,4,4,4,4,0,0,0,1,1,0,0,1,0
0,0,0,0,0,0,0,0,0,1,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,1,1,0,0,1,3,2,3,1,0,0,1,1,0
0,0,1,1,0,0,0,3,0,0,0,1,1,0,0
0,0,0,1,1,0,0,1,0,0,1,1,0,0,0
0,1,0,0,1,1,0,1,0,1,1,0,0,1,0
0,1,0,0,0,1,1,1,1,1,0,0,0,1,0
0,3,0,0,0,0,1,1,1,0,0,0,0,3,0
0,2,3,1,1,0,0,1,0,0,1,1,3,2,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
\ No newline at end of file
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,2,2,2,0,0,0,0,0,0,1,1,1,0
0,0,0,0,2,1,0,0,0,0,0,0,0,1,1,0
0,0,1,0,0,0,1,3,0,0,1,1,0,0,3,0
0,3,2,3,0,0,0,0,0,0,0,2,0,1,3,0
0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,1,0,0,0,1,3,1,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment