first commit

This commit is contained in:
2020-06-12 23:49:11 +02:00
commit ea19e5ad2a
214 changed files with 43291 additions and 0 deletions

12
Democracy/Democracy.iml Normal file
View File

@@ -0,0 +1,12 @@
<?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" />
<orderEntry type="module" module-name="TOOLS" exported="" />
</component>
</module>

View File

@@ -0,0 +1,51 @@
import basis.*;
public class DayCounter implements Runnable {
private Stift pen;
private TextFeld textField;
private int days;
private int discussion;
public DayCounter(Output output) {
pen = new Stift();
textField = new TextFeld();
//Customizing the textfield
textField.setzeGroesse(140, 20);
textField.setzePosition(output.getFrame().breite()-textField.breite()-15, output.getFrame().hoehe()-textField.hoehe()-10);
textField.setzeText(" Days passed: 0");
textField.setzeHintergrundFarbe(output.getFrame().hintergrundFarbe());
textField.setzeSchriftFarbe(Farbe.rgb(217, 219, 66));
textField.setzeSchriftStil(Schrift.FETT);
textField.setzeRand(textField.schriftFarbe(), 2);
days = 0;
discussion = 0;
}
public void countDays(){
discussion++;
//If 100 discussions were made one day passes
if (discussion % 100 == 1){
days++;
}
}
public void updateDays(){
textField.setzeText("");
textField.setzeText(" Days passed: " + days);
}
public int getDays() {
return days;
}
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()){
updateDays();
Hilfe.kurzePause();
}
}
}

102
Democracy/src/Operator.java Normal file
View File

@@ -0,0 +1,102 @@
import basis.Hilfe;
import util.Timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @Author BiteCoding --> https://github.com/BiteCoding/JavaCode
*/
//----------------------------------------------------------------------------------------------------------------------
public class Operator {
//--------------------------------------------------Needed classes--------------------------------------------------
private Output output;
private Render render;
private Person villagers;
private DayCounter dayCounter;
private Timer timer;
private ExecutorService executor = Executors.newCachedThreadPool();
//----------------------------------------Array that represents the village-----------------------------------------
private Person[][] village;
//---------------------------------------------------Constructor----------------------------------------------------
public Operator() {
output = new Output();
village = new Person[50][50];
render = new Render(village,output.getFrame().breite());
dayCounter = new DayCounter(output);
output.setRenderer(render);
timer = new Timer();
//dummy for operating methods
villagers = new Person(-1, -1, -1);
}
private boolean isAutocracy(int border) {
int value = village[0][0].getParty();
for (int i = 0; i < village.length; i++) {
for (int j = 0; j < village.length; j++) {
if (value > border != village[i][j].getParty() > border){
return false;
}
}
}
return true;
}
private void fillVillage() {
for (int i = 0; i < village.length; i++) {
for (int j = 0; j < village.length; j++) {
//Positions the Person in between a rectangle with x:50, y:50, w:framewidth - 100, h: frameheight - 100
Person villager = new Person(
//------------------------------------------ x -------------------------------------------------
((1 + i) * (output.getFrame().breite() - 100) / village.length) + 50 - 7,
//------------------------------------------ y -------------------------------------------------
((1 + j) * (output.getFrame().hoehe() - 100) / village.length) + 50 - 7,
//----------------------------------------random------------------------------------------------
Hilfe.zufall(1, 100));
village[i][j] = villager;
}
}
}
private void drawBorder() {
output.setBorder(village[0][0].getPosition().getX(), village[0][0].getPosition().getX(),
village[village.length - 1][village.length - 1].getPosition().getX()
- village[0][0].getPosition().getX(),
village[village.length - 1][village.length - 1].getPosition().getY()
- village[0][0].getPosition().getY(), render.getRadius() + render.getRadius() * 0.5);
}
//-------------------------------------------------operating method-------------------------------------------------
private void start(){
while (!output.isStartButtonPressed() || !output.textChanged()) {
Hilfe.kurzePause();
}
timer.start();
render.renderAll(output.getPercentageRed());
output.setFirstValues(village);
executor.execute(dayCounter);
while (!output.isEndButtonPressed() && !this.isAutocracy(output.getPercentageRed())) {
villagers.discuss(village, output.getPercentageRed(), render);
dayCounter.countDays();
}
timer.end();
System.out.println(timer.timeNeeded());
output.setEndValues(village);
output.displayResults(dayCounter);
Hilfe.warte(500);
System.exit(0);
}
//-----------------------------------------------------main method--------------------------------------------------
public static void main(String[] args) {
Operator op = new Operator();
op.fillVillage();
op.drawBorder();
op.start();
}
//----------------------------------------------------------------------------------------------------------------------
}

261
Democracy/src/Output.java Normal file
View File

@@ -0,0 +1,261 @@
import basis.*;
import util.Timer;
import java.awt.*;
import java.text.DecimalFormat;
/**
* @Author BiteCoding --> https://github.com/BiteCoding/JavaCode
*/
//----------------------------------------------------------------------------------------------------------------------
public class Output {
private Fenster frame;
private Stift pen;
private TextFeld textField;
private Knopf startButton;
private Knopf endButton;
//------------------------------------------------------------------------------------------------------------------
private boolean startButtonPressed, endButtonPressed, textChange;
private int percentageRed, width, height, startReds, startBlacks, endReds, endBlacks;
private String text;
private Render render;
//------------------------------------------------------------------------------------------------------------------
public Output() {
this.initFrame();
//--------------------------------------------------------------------------------------------------------------
this.initPen();
//--------------------------------------------------------------------------------------------------------------
this.initTextFields();
//--------------------------------------------------------------------------------------------------------------
this.initButtons();
//--------------------------------------------------------------------------------------------------------------
startButtonPressed = textChange = false;
percentageRed = -1;
text = textField.text();
}
public void setRenderer(Render render){
this.render = render;
pen.setzeLinienBreite((int)render.getRadius());
}
private void initFrame(){
frame = new Fenster();
//Scaling the frame
frame.setzeGroesse(900, 900);
this.getMonitorSize();
//Centering the frame in the middle of the screen
//this.getMonitorSizes();
frame.setzePosition(width / 2 - frame.breite() / 2,
height / 2 - frame.hoehe() / 2);
frame.setzeHintergrundFarbe(Farbe.rgb(61, 50, 91));
//naming the frame
frame.setzeTitel("~~~~ DEMOCRACY ~~~~");
}
private void initPen(){
pen = new Stift();
}
private void initTextFields(){
textField = new TextFeld();
//Customizing the textfield
textField.setzeGroesse(140, 20);
//Centering the textfield
this.getMonitorSize();
textField.setzePosition(15, 5);
textField.setzeText(" Proportion red: ");
textField.setzeHintergrundFarbe(frame.hintergrundFarbe());
textField.setzeSchriftFarbe(Farbe.rgb(217, 219, 66));
textField.setzeSchriftStil(Schrift.FETT);
textField.setzeRand(textField.schriftFarbe(), 2);
}
private void initButtons(){
//START
startButton = new Knopf();
//Customizing the startButton
startButton.setzeGroesse(100, 20);
//Setting the position of the startButton relative to the position of the textfield
startButton.setzePosition(frame.breite() / 2 - startButton.breite()/2,
textField.vPosition());
startButton.setzeText("START");
startButton.setzeRand(Farbe.rgb(217, 219, 66), 2);
startButton.setzeSchriftFarbe(Farbe.rgb(217, 219, 66));
startButton.setzeHintergrundFarbe(frame.hintergrundFarbe());
//END
endButton = new Knopf();
//Customizing the startButton
endButton.setzeGroesse(100, 20);
//Setting the position of the startButton relative to the position of the textfield
endButton.setzePosition(frame.breite()/2 - endButton.breite()/2,
frame.hoehe() - 20 - endButton.hoehe() / 2);
endButton.setzeText("END");
endButton.setzeRand(Farbe.rgb(217, 219, 66), 2);
endButton.setzeSchriftFarbe(Farbe.rgb(217, 219, 66));
endButton.setzeHintergrundFarbe(frame.hintergrundFarbe());
}
/**
* Created my own Method to detect the displaysize because the Method provided by the {@link basis.Hilfe} library
* detected my dual monitor setup as one giant monitor
*/
public void getMonitorSize() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for (int i = 0; i < gs.length; i++) {
DisplayMode dm = gs[i].getDisplayMode();
width = dm.getWidth();
height = dm.getHeight();
}
}
public int getWidth() {
return width;
}
public int getHeight(){
return height;
}
private int reds(Person[][] villager) {
int reds = 0;
for (int i = 0; i < villager.length; i++) {
for (int j = 0; j < villager.length; j++) {
if (villager[i][j].getParty() <= this.getPercentageRed()) {
reds++;
}
}
}
return reds;
}
private int blacks(Person[][] villager) {
int blacks = 0;
for (int i = 0; i < villager.length; i++) {
for (int j = 0; j < villager.length; j++) {
if (villager[i][j].getParty() > this.getPercentageRed()) {
blacks++;
}
}
}
return blacks;
}
public void setBorder(double x, double y, double lastX, double lastY, double radius){
pen.rechteck(x - radius-pen.linienBreite(),y - radius - pen.linienBreite(),
lastX + 2*(radius + pen.linienBreite()),lastY + 2*(radius+pen.linienBreite()));
}
public void setFirstValues(Person[][] villagers) {
startBlacks = blacks(villagers);
startReds = reds(villagers);
}
public void setEndValues(Person[][] villagers) {
endBlacks = blacks(villagers);
endReds = reds(villagers);
}
//------------------------------------------------------------------------------------------------------------------
public Fenster getFrame() {
return frame;
}
//------------------------------------------------------------------------------------------------------------------
private String getNumber(String str) {
String number = "";
for (int i = text.length(); i < str.length(); i++) {
number += (str.charAt(i));
}
return number;
}
//------------------------------------------------------------------------------------------------------------------
/*METHOD THAT RETURNS IF THE TEXT IN THE TEXTBOX CHANGED AND SETS THE CHANGED VALUE
AS THE PERCENTAGE OF THE VOTERS WHO VOTE FOR THE RED PARTY*/
//------------------------------------------------------------------------------------------------------------------
public boolean textChanged() {
//checking if the text changed
if (!textField.text().equals(text) && textField.fokusVerloren()) {
textChange = true;
//is the value a valid value?
try {
percentageRed = Integer.parseInt(getNumber(textField.text()));
//values that are not between 0 and 100 are not allowed
if (percentageRed < 0 || percentageRed > 100) {
textField.setzeText(text);
throw new IllegalArgumentException();
}
textField.setzeEditierbar(false);
} catch (Exception e1) {
System.err.println("NOT A VALID VALUE!");
//resetting the text in the textbox
textField.setzeText(text);
//repeat loop until a valid value got inserted
boolean validValue = false;
while (!validValue) {
//checking if text changed
if (!textField.text().equals(text) && textField.fokusVerloren()) {
//is the value a valid value?
try {
percentageRed = Integer.parseInt(getNumber(textField.text()));
//values that are not between 0 and 100 are not allowed
if (percentageRed < 0 || percentageRed > 100) {
textField.setzeText(text);
throw new IllegalArgumentException();
}
textField.setzeEditierbar(false);
validValue = true;
} catch (Exception e2) {
System.err.println("NOT A VALID VALUE!");
textField.setzeText(text);
}
}
}
}
}
return textChange;
}
//------------------------------------------------------------------------------------------------------------------
public int getPercentageRed() {
return percentageRed;
}
//------------------------------------------------------------------------------------------------------------------
public boolean isStartButtonPressed() {
if (startButton.wurdeGedrueckt()) {
startButtonPressed = true;
}
return startButtonPressed;
}
//------------------------------------------------------------------------------------------------------------------
public boolean isEndButtonPressed() {
if (endButton.wurdeGedrueckt()) {
endButtonPressed = true;
}
return endButtonPressed;
}
//------------------------------------------------------------------------------------------------------------------
public void displayResults(DayCounter dayCounter) {
DecimalFormat dc = new DecimalFormat("#.##");
String timeTaken = "The simulation has ended after " + dayCounter.getDays() + " days.";
String first = "Red starting value: " + startReds + " | " + "Black starting value: " + startBlacks;
String last = "Red ending value: " + endReds + " | " + "Black ending value: " + endBlacks;
String totalChange = "Red: " + (endReds - startReds) + " | " + "Black: " + (endBlacks - startBlacks);
String percentages = "Red: " + dc.format((double)endReds/2500 *100) + "% | "
+ " Blacks: " + dc.format((double)endBlacks/2500 *100)+ "%";
System.out.println(timeTaken);
System.out.println(first);
System.out.println(last);
System.out.println(totalChange);
System.out.println(percentages);
}
}

54
Democracy/src/Person.java Normal file
View File

@@ -0,0 +1,54 @@
import basis.Hilfe;
import java.awt.*;
/**
* @Author BiteCoding --> https://github.com/BiteCoding/JavaCode
*/
//----------------------------------------------------------------------------------------------------------------------
class Person {
//------------------------------------------------------------------------------------------------------------------
private Point position;
private int party;
//------------------------------------------------------------------------------------------------------------------
Person(int x, int y, int rand) {
position = new Point(x, y);
party = rand;
}
//------------------------------------------------------------------------------------------------------------------
void discuss(Person[][] persons, int redBorder, Render render) {
Person person1 = persons[Hilfe.zufall(0, 49)][Hilfe.zufall(0, 49)];
Person person2 = persons[Hilfe.zufall(0, 49)][Hilfe.zufall(0, 49)];
//if person1.getParty() <= redBorder ==> member of the red party
//if person1.getParty() > redBorder ==> member of the black party
if (person1.getParty() > redBorder != person2.getParty() > redBorder) {
int randValue = Hilfe.zufall(0, 1);
if (randValue == 1) {
person1.setParty(person2.getParty());
render.renderOne(person1, redBorder);
} else {
person2.setParty(person1.getParty());
render.renderOne(person2, redBorder);
}
}
}
//------------------------------------------------------------------------------------------------------------------
Point getPosition() {
return position;
}
//------------------------------------------------------------------------------------------------------------------
int getParty() {
return party;
}
//------------------------------------------------------------------------------------------------------------------
private void setParty(int party) {
this.party = party;
}
//------------------------------------------------------------------------------------------------------------------
}

56
Democracy/src/Render.java Normal file
View File

@@ -0,0 +1,56 @@
import basis.Farbe;
import basis.Muster;
import basis.Stift;
/**
* @Author BiteCoding --> https://github.com/BiteCoding/JavaCode
*/
//----------------------------------------------------------------------------------------------------------------------
public class Render {
Stift pen;
Person [][] villager;
double radius;
//------------------------------------------------------------------------------------------------------------------
public Render(Person[][] villager, int frameWidth) {
pen = new Stift();
pen.setzeFuellMuster(Muster.GEFUELLT);
this.villager = villager;
radius = ((frameWidth-50)/ villager.length) / 2 - (frameWidth/villager.length)*0.05;
}
//------------------------------------------------------------------------------------------------------------------
//DRAWING THE VILLAGERS TO THE FRAME
public void renderAll(int redPercentage) {
for (int i = 0; i < villager.length; i++) {
for (int j = 0; j < villager.length; j++) {
if (villager[i][j].getParty() <= redPercentage) {
pen.setzeFarbe(Farbe.ROT);
} else {
pen.setzeFarbe(Farbe.SCHWARZ);
}
pen.kreis(villager[i][j].getPosition().getX(),
villager[i][j].getPosition().getY(),
radius);
}
}
}
public void renderOne(Person villager, int redPercentage) {
if (villager.getParty() <= redPercentage) {
pen.setzeFarbe(Farbe.ROT);
} else {
pen.setzeFarbe(Farbe.SCHWARZ);
}
pen.kreis(villager.getPosition().getX(),
villager.getPosition().getY(),
radius);
}
//------------------------------------------------------------------------------------------------------------------
public double getRadius() {
return radius;
}
}