package Tools.Pixel2D.PixelGenerator;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.JTextComponent.KeyBinding;
import Tools.Pixel2D.*;
import Tools.Menu.*;
public class PixelGenerator extends Importable implements ActionListener {
JFrame frame;
public PixelScreen pframe;
PixelLayer layer;
PixelMap map;
PixelGrid grid;
int fx = 0;
Scheduals s;
Accounts ac;
List worlds;
JButton add;
JButton delete;
JButton join;
JButton edit;
JTextField name;
JLabel nameL;
JLabel fieldSizeX;
JLabel fieldSizeXAmount;
JButton fieldSizeXPlus;
JButton fieldSizeXMinus;
int fSizeX = 10;
JLabel fieldSizeY;
JLabel fieldSizeYAmount;
JButton fieldSizeYPlus;
JButton fieldSizeYMinus;
int fSizeY = 10;
JLabel pixelSize;
JLabel pixelSizeAmount;
JButton pixelSizePlus;
JButton pixelSizeMinus;
int pSize = 10;
JLabel gridSize;
JLabel gridSizeAmount;
JButton gridSizePlus;
JButton gridSizeMinus;
int gSize = 20;
JLabel layers;
JLabel layersAmount;
JButton layersPlus;
JButton layersMinus;
int lAmount = 1;
JButton addNewGame;
JButton deleteNewGame;
World ws = new World();
//Worldgenerator
Color drawColor;
int currentLayer = 0;
boolean worldIsSafed;
int currentWorld = 0;
Rectangle[] colors = new Rectangle[13];
Color[] colors2 = new Color[13];
JLabel layerModesLabel;
String[] layerModes = {"All Layers", "Only Layer", "Till Layer", "From Layer"};
JComboBox<?> layerModesBox = new JComboBox<Object>(layerModes);
int layerMode = 0;
layerModes modes;
KeyListener keys;
JLabel layerPlus;
JLabel layerLabel;
JLabel layerMinus;
//Objects
JFrame objectMenu;
public PixelGenerator(Accounts ac) {
frame = new JFrame();
frame.setBounds(20, 20, 1200, 650);
frame.setLayout(null);
frame.setVisible(false);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
this.ac = ac;
s = new Scheduals(ac);
spawnGames();
spawnAddNewGame();
drawColor = new Color(0);
}
public void start() {
s.logIn();
s.frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
while(!s.getIfLogInFinish()) {
}
if(!ac.getIfStringExsists(ac.getPath(), ac.getAccountOfSelectedName())) ac.addLine(ac.getPosOfSelectedPassword()+1, ac.getAccountOfSelectedName(), ac.getPath());
frame.setVisible(true);
gamesUi(true);
modes = new layerModes();
modes.run();
}
public void spawnGames() {
worlds = new List(frame, this, 0, 3, 100, 100, 100, 20, true);
worlds.setVisible(false);
worlds.setSelectable(true, Color.yellow);
worlds.setRowSelectable(true);
add = new JButton("new Game");
frame.add(add);
add.setBounds(50, 540, 200, 50);
add.addActionListener(this);
add.setVisible(false);
delete = new JButton("Delete");
frame.add(delete);
delete.setBounds(350, 540, 200, 50);
delete.setVisible(false);
delete.addActionListener(this);
join = new JButton("Join");
frame.add(join);
join.setBounds(650, 540, 200, 50);
join.setVisible(false);
join.addActionListener(this);
/**
edit = new JButton("Edit");
frame.add(edit);
edit.setBounds(950, 540, 200, 50);
edit.setVisible(false);**/
}
public void spawnAddNewGame() {
nameL = new JLabel("Name: ");
frame.add(nameL);
nameL.setBounds(200, 100, 100, 20);
nameL.setVisible(false);
name = new JTextField("");
frame.add(name);
name.setBounds(300, 100, 100, 20);
name.setVisible(false);
fieldSizeX = new JLabel("Field Size X: ");
frame.add(fieldSizeX);
fieldSizeX.setBounds(200, 200, 100, 20);
fieldSizeX.setVisible(false);
fieldSizeXAmount = new JLabel("10");
frame.add(fieldSizeXAmount);
fieldSizeXAmount.setBounds(300, 200, 100, 20);
fieldSizeXAmount.setVisible(false);
fieldSizeXPlus = new JButton("+1");
frame.add(fieldSizeXPlus);
fieldSizeXPlus.setBounds(400, 200, 50, 20);
fieldSizeXPlus.setVisible(false);
fieldSizeXPlus.addActionListener(this);
fieldSizeXMinus = new JButton("-1");
frame.add(fieldSizeXMinus);
fieldSizeXMinus.setBounds(450, 200, 50, 20);
fieldSizeXMinus.setVisible(false);
fieldSizeXMinus.addActionListener(this);
fieldSizeY = new JLabel("Field Size Y: ");
frame.add(fieldSizeY);
fieldSizeY.setBounds(200, 250, 100, 20);
fieldSizeY.setVisible(false);
fieldSizeYAmount = new JLabel("10");
frame.add(fieldSizeYAmount);
fieldSizeYAmount.setBounds(300, 250, 100, 20);
fieldSizeYAmount.setVisible(false);
fieldSizeYPlus = new JButton("+1");
frame.add(fieldSizeYPlus);
fieldSizeYPlus.setBounds(400, 250, 50, 20);
fieldSizeYPlus.setVisible(false);
fieldSizeYPlus.addActionListener(this);
fieldSizeYMinus = new JButton("-1");
frame.add(fieldSizeYMinus);
fieldSizeYMinus.setBounds(450, 250, 50, 20);
fieldSizeYMinus.setVisible(false);
fieldSizeYMinus.addActionListener(this);
pixelSize = new JLabel("PixelSize: ");
frame.add(pixelSize);
pixelSize.setBounds(200, 300, 100, 20);
pixelSize.setVisible(false);
pixelSizeAmount = new JLabel("10");
frame.add(pixelSizeAmount);
pixelSizeAmount.setBounds(300, 300, 100, 20);
pixelSizeAmount.setVisible(false);
pixelSizePlus = new JButton("+1");
frame.add(pixelSizePlus);
pixelSizePlus.setBounds(400, 300, 50, 20);
pixelSizePlus.setVisible(false);
pixelSizePlus.addActionListener(this);
pixelSizeMinus = new JButton("-1");
frame.add(pixelSizeMinus);
pixelSizeMinus.setBounds(450, 300, 50, 20);
pixelSizeMinus.setVisible(false);
pixelSizeMinus.addActionListener(this);
gridSize = new JLabel("Grid Size: ");
frame.add(gridSize);
gridSize.setBounds(200, 350, 100, 20);
gridSize.setVisible(false);
gridSizeAmount = new JLabel("20");
frame.add(gridSizeAmount);
gridSizeAmount.setBounds(300, 350, 100, 20);
gridSizeAmount.setVisible(false);
gridSizePlus = new JButton("+1");
frame.add(gridSizePlus);
gridSizePlus.setBounds(400, 350, 50, 20);
gridSizePlus.setVisible(false);
gridSizePlus.addActionListener(this);
gridSizeMinus = new JButton("-1");
frame.add(gridSizeMinus);
gridSizeMinus.setBounds(450, 350, 50, 20);
gridSizeMinus.setVisible(false);
gridSizeMinus.addActionListener(this);
layers = new JLabel("Layers: ");
frame.add(layers);
layers.setBounds(200, 400, 100, 20);
layers.setVisible(false);
layersAmount = new JLabel("1");
frame.add(layersAmount);
layersAmount.setBounds(300, 400, 100, 20);
layersAmount.setVisible(false);
layersPlus = new JButton("+1");
frame.add(layersPlus);
layersPlus.setBounds(400, 400, 50, 20);
layersPlus.setVisible(false);
layersPlus.addActionListener(this);
layersMinus = new JButton("-1");
frame.add(layersMinus);
layersMinus.setBounds(450, 400, 50, 20);
layersMinus.setVisible(false);
layersMinus.addActionListener(this);
addNewGame = new JButton("Add");
frame.add(addNewGame);
addNewGame.setBounds(200, 500, 100, 20);
addNewGame.setVisible(false);
addNewGame.addActionListener(this);
deleteNewGame = new JButton("Delete");
frame.add(deleteNewGame);
deleteNewGame.setBounds(350, 500, 100, 20);
deleteNewGame.setVisible(false);
deleteNewGame.addActionListener(this);
}
public void spawnWorldUi() {
String path = ac.getPath()+"Worlds/"+ac.readLine(ac.getAccountOfSelectedName(), ac.getPosOfSelectedName())+"/";
String name = worlds.getComponentString(worlds.getSelected()[0],0);
currentWorld = worlds.getSelected()[0];
String[][][] s = ws.scanWorld(path, name);
gamesUi(false);
int[] ints = ws.getIntsOfLastScanned();
worldIsSafed = false;
int lSizeX = ints[0];
int maxSizeX = (int) (Toolkit.getDefaultToolkit().getScreenSize().width/ints[3])-6;
int maxSizeY = (int) (Toolkit.getDefaultToolkit().getScreenSize().height/ints[3])-8;
if(lSizeX > maxSizeX) {
lSizeX = maxSizeX;
}
int lSizeY = ints[1];
if(lSizeY > maxSizeY) {
lSizeY = maxSizeY;
}
pframe = new PixelScreen(lSizeX*ints[3]+4*ints[3]+6, lSizeY*ints[3]+3*ints[3]+38, ints[3], ints[2]);
pframe.setVisible(true);
pframe.setResizable(false);
pframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
layer = new PixelLayer(pframe, ints[3], 0, 3*ints[3], lSizeX, lSizeY, ints[2], pframe.panel);
pframe.add(layer);
map = new PixelMap(layer, ints[3], 0, 3*ints[3], ints[0], ints[1], ints[2]);
layer.setMap(map);
grid = new PixelGrid(pframe, ints[4], layer);
layer.setGrid(grid);
//layer.setPixelOutline(true);
layer.setVisible(true);
map.setStringToPixel(s);
//layer.setStringToPixel(s);
layer.setMapLocation(0, 0);
frame.setVisible(false);
WorldUi2(ints);
pframe.panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int minPix = layer.getMinPixel();
Point p = e.getPoint();
p.x = p.x - p.x % minPix;
System.out.println(p.x + " " + p.y);
p.x -= layer.getPoint().x;
System.out.println(p.x);
p.x /= minPix;
p.y = p.y - p.y % minPix;
p.y -= layer.getPoint().y;
p.y /= minPix;
System.out.println(layer.getPixels().length);
if(p.x < layer.getPixels()[0].length &
p.x >= 0 &
p.y < layer.getPixels().length &
p.y >= 0 &
currentLayer < layer.getPixels()[0][0].length)
if(e.getButton() == e.BUTTON1) layer.setPixel(p.x, p.y, currentLayer, drawColor);
else if(e.getButton() == e.BUTTON2) drawColor = layer.getPixels()[p.y][p.x][currentLayer].getColor();
for(int i = 0; i < colors.length; i++) {
if(colors[i].contains(e.getPoint())) {
drawColor = colors2[i];
}
}
}
}
);
pframe.addKeyListener(keys = new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
keys(e);
}
@Override
public void keyReleased(KeyEvent e) {
}
});
pframe.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
String path = ac.getPath()+"Worlds/"+ac.readLine(ac.getAccountOfSelectedName(), ac.getPosOfSelectedName())+"/";
String name = worlds.getComponentString(currentWorld,0);
int i = JOptionPane.showConfirmDialog(null, "Save " + name + "?", "Save Object", JOptionPane.YES_NO_CANCEL_OPTION);
if(i==0) {
ws.scanWorld(path, name);
System.out.println(path + " " + name);
int[] ints = ws.getIntsOfLastScanned();
layer.saveMapLocation();
ws.setWorld(path, name, map.getPixelToString(), ints[3], ints[4]);
}
if(i!=2) {
worldUi(false);
gamesUi(true);
}
}
});
}
public void spawnObjectMenu() {
objectMenu = new JFrame();
objectMenu.setBounds(20, 20, 1200, 600);
}
public void addNewGameUi(boolean b) {
name.setVisible(b);
nameL.setVisible(b);
fieldSizeX.setVisible(b);
fieldSizeXAmount.setVisible(b);
fieldSizeXPlus.setVisible(b);
fieldSizeXMinus.setVisible(b);
fieldSizeY.setVisible(b);
fieldSizeYAmount.setVisible(b);
fieldSizeYPlus.setVisible(b);
fieldSizeYMinus.setVisible(b);
pixelSize.setVisible(b);
pixelSizeAmount.setVisible(b);
pixelSizePlus.setVisible(b);
pixelSizeMinus.setVisible(b);
gridSize.setVisible(b);
gridSizeAmount.setVisible(b);
gridSizePlus.setVisible(b);
gridSizeMinus.setVisible(b);
layers.setVisible(b);
layersAmount.setVisible(b);
layersPlus.setVisible(b);
layersMinus.setVisible(b);
addNewGame.setVisible(b);
deleteNewGame.setVisible(b);
}
public void gamesUi(boolean b) {
worlds.deleteAll();
setWorlds();
worlds.setVisible(b);
add.setVisible(b);
delete.setVisible(b);
join.setVisible(b);
//edit.setVisible(b);
}
public void worldUi(boolean b) {
frame.setVisible(!b);
pframe.setVisible(b);
layer.setVisible(b);
}
public void setWorlds() {
int acc = ac.getAccountOfSelectedName();
int pos = ac.getPosOfSelectedPassword();
int length = ac.getLengthOfAccount(acc)-3;;
for(int i = 0; i < length-1; i++) {
worlds.addLine();
worlds.setComponentString(ac.readLine(acc, i+4), i, 0);
}
}
public void WorldUi2(int[] ints) {
Graphics g = pframe.panel.getGraphics();
int minX = 0;
int minY = 3*ints[3]-1;
int maxX = pframe.getBounds().width;
int maxY = pframe.getBounds().height;
//Oben
g.setColor(Color.black);
g.fillRect(0, minY, pframe.getBounds().width, 1);
//Rechts
minX = pframe.getBounds().width-4*ints[3]-5;
//maxY = pframe.getBounds().height;
//g.clearRect(minX, 0, maxX, maxY);
g.setColor(Color.black);
g.fillRect(minX-1, 0, 1, maxY);
g.fillRect(minX, 0, ints[3], ints[3]);
colors[0] = new Rectangle(minX, 0, ints[3], ints[3]);
colors2[0] = g.getColor();
g.setColor(Color.blue);
g.fillRect(minX+ints[3], 0, ints[3], ints[3]);
colors[1] = new Rectangle(minX+ints[3], 0, ints[3], ints[3]);
colors2[1] = g.getColor();
g.setColor(Color.cyan);
g.fillRect(minX+2*ints[3], 0, ints[3], ints[3]);
colors[2] = new Rectangle(minX+2*ints[3], 0, ints[3], ints[3]);
colors2[2] = g.getColor();
g.setColor(Color.darkGray);
g.fillRect(minX, ints[3], ints[3], ints[3]);
colors[3] = new Rectangle(minX, ints[3], ints[3], ints[3]);
colors2[3] = g.getColor();
g.setColor(Color.gray);
g.fillRect(minX+ints[3], ints[3], ints[3], ints[3]);
colors[4] = new Rectangle(minX+ints[3], ints[3], ints[3], ints[3]);
colors2[4] = g.getColor();
g.setColor(Color.lightGray);
g.fillRect(minX+2*ints[3], ints[3], ints[3], ints[3]);
colors[5] = new Rectangle(minX+2*ints[3], ints[3], ints[3], ints[3]);
colors2[5] = g.getColor();
g.setColor(Color.green);
g.fillRect(minX, ints[3]*2, ints[3], ints[3]);
colors[6] = new Rectangle(minX, ints[3]*2, ints[3], ints[3]);
colors2[6] = g.getColor();
g.setColor(Color.magenta);
g.fillRect(minX+ints[3], ints[3]*2, ints[3], ints[3]);
colors[7] = new Rectangle(minX+ints[3], ints[3]*2, ints[3], ints[3]);
colors2[7] = g.getColor();
g.setColor(Color.pink);
g.fillRect(minX+2*ints[3], ints[3]*2, ints[3], ints[3]);
colors[8] = new Rectangle(minX+2*ints[3], ints[3]*2, ints[3], ints[3]);
colors2[8] = g.getColor();
g.setColor(Color.red);
g.fillRect(minX, ints[3]*3, ints[3], ints[3]);
colors[9] = new Rectangle(minX, ints[3]*3, ints[3], ints[3]);
colors2[9] = g.getColor();
g.setColor(Color.orange);
g.fillRect(minX+ints[3], ints[3]*3, ints[3], ints[3]);
colors[10] = new Rectangle(minX+ints[3], ints[3]*3, ints[3], ints[3]);
colors2[10] = g.getColor();
g.setColor(Color.yellow);
g.fillRect(minX+2*ints[3], ints[3]*3, ints[3], ints[3]);
colors[11] = new Rectangle(minX+ints[3]*2, ints[3]*3, ints[3], ints[3]);
colors2[11] = g.getColor();
g.setColor(Color.white);
g.fillRect(minX, ints[3]*4, ints[3], ints[3]);
colors[12] = new Rectangle(minX, ints[3]*4, ints[3], ints[3]);
colors2[12] = g.getColor();
g.setColor(drawColor);
layer.panel.setLayout(null);
layerModesLabel = new JLabel("LayerMode: ");
layer.panel.add(layerModesLabel);
layerModesLabel.setBounds(minX+2, maxY-ints[3]*5-30, ints[3]*3, ints[3]);
layerModesLabel.setFont(new Font(layerModesLabel.getFont().getName(), Font.PLAIN, ints[3]/2));
layer.panel.add(layerModesBox);
layerModesBox.setBounds(minX, maxY-ints[3]*4-30, ints[3]*3, ints[3]);
layerModesBox.setLayout(null);
layerModesBox.setFont(new Font(layerModesBox.getFont().getName(), Font.PLAIN, ints[3]/2));
layerModesBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
layerModesBox.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
keys(e);
}
@Override
public void keyReleased(KeyEvent e) {
}
});
/**
layerMinus = new JLabel("+");
layerMinus.setFont(new Font(layerMinus.getFont().getName(), Font.PLAIN, ints[3]));
layerMinus.setBounds(minX, maxY-ints[3]*3-30, ints[3], ints[3]);
layer.panel.add(layerMinus);
layerLabel = new JLabel(" 0 ");
layerLabel.setFont(new Font(layerMinus.getFont().getName(), Font.PLAIN, ints[3]));
layerLabel.setBounds(minX+ints[3], maxY-ints[3]*3-30, ints[3], ints[3]);
layer.panel.add(layerLabel);
layerPlus = new JLabel("+");
layerPlus.setFont(new Font(layerMinus.getFont().getName(), Font.PLAIN, ints[3]));
layerPlus.setBounds(minX+ints[3]*2, maxY-ints[3]*3-30, ints[3], ints[3]);
layer.panel.add(layerPlus);**/
}
//da es mehrere Keylistener gibt
private void keys(KeyEvent e) {
System.out.println("key: " + e.getKeyCode());
switch(e.getKeyChar()) {
case 'w': layer.moveMapLocation(0, -1); break;
case 'a': layer.moveMapLocation(-1, 0); break;
case 'd': layer.moveMapLocation(1, 0); break;
case 's': layer.moveMapLocation(0, 1); break;
}
switch(e.getKeyCode()) {
case KeyEvent.VK_RIGHT: if(layer.mLayer+1 < layer.getPixels()[0][0].length) {
layer.mLayer++;
currentLayer++;
layer.drawPixels();
} break;
case KeyEvent.VK_LEFT: if(layer.mLayer > 0) {
layer.mLayer--;
currentLayer--;
layer.drawPixels();
} break;
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == this.add){
gamesUi(false);
addNewGameUi(true);
}
if(e.getSource() == this.delete){
System.out.println(ac.getPosOfString(worlds.getComponentString(worlds.getSelected()[0],0)));
ac.deleteLine(ac.getPosOfString(worlds.getComponentString(worlds.getSelected()[0],0)), ac.getAccountOfSelectedName());
String path = ac.getPath()+"Worlds/"+ac.readLine(ac.getAccountOfSelectedName(), ac.getPosOfSelectedName())+"/";
String name = worlds.getComponentString(worlds.getSelected()[0],0);
ws.deleteWorld(path, name);
gamesUi(true);
}
if(e.getSource() == this.addNewGame){
if(!name.getText().equals("")) {
int acc = ac.getAccountOfSelectedName();
int pos = ac.getPosOfSelectedPassword();
String path = ac.getPath()+"Worlds/"+ac.readLine(ac.getAccountOfSelectedName(), ac.getPosOfSelectedName())+"/";
String name = this.name.getText();
if(!ac.getIfStringExsists(name, acc) & fSizeX > 0 & fSizeY > 0 & lAmount > 0 & pSize > 0 & gSize > pSize & gSize % pSize == 0) {
ac.addLine(pos+2, acc, name);
ws.addWorld(path, name);
System.out.println(fSizeX + " "+ fSizeY);
ws.createWorld(path, name, fSizeX, fSizeY, lAmount, pSize, gSize);
addNewGameUi(false);
gamesUi(true);
}
}
}
if(e.getSource() == this.join) {
if(worlds.getSelected() != null) {
swUI swUI = new swUI();
swUI.start();
}
}
if(e.getSource() == this.fieldSizeXMinus) {
fSizeX--;
fieldSizeXAmount.setText(String.valueOf(fSizeX));
}
if(e.getSource() == this.fieldSizeXPlus) {
fSizeX++;
fieldSizeXAmount.setText(String.valueOf(fSizeX));
}
if(e.getSource() == this.fieldSizeYMinus) {
fSizeY--;
fieldSizeYAmount.setText(String.valueOf(fSizeY));
}
if(e.getSource() == this.fieldSizeYPlus) {
fSizeY++;
fieldSizeYAmount.setText(String.valueOf(fSizeY));
}
if(e.getSource() == this.pixelSizeMinus) {
pSize--;
pixelSizeAmount.setText(String.valueOf(pSize));
}
if(e.getSource() == this.pixelSizePlus) {
pSize++;
pixelSizeAmount.setText(String.valueOf(pSize));
}
if(e.getSource() == this.gridSizeMinus) {
gSize--;
gridSizeAmount.setText(String.valueOf(gSize));
}
if(e.getSource() == this.gridSizePlus) {
gSize++;
gridSizeAmount.setText(String.valueOf(gSize));
}
if(e.getSource() == this.layersMinus) {
lAmount--;
layersAmount.setText(String.valueOf(lAmount));
}
if(e.getSource() == this.layersPlus) {
lAmount++;
layersAmount.setText(String.valueOf(lAmount));
}
}
@Override
public void action(int number) {
System.out.println("Ja");
}
class swUI extends Thread {
public void run() {
spawnWorldUi();
}
}
class layerModes extends Thread {
public void run() {
while(true) {
layerMode = layerModesBox.getSelectedIndex();
//System.out.println(layerModesBox.getSelectedIndex());
if(layer != null && layer.layerMode != layerMode) {
layer.layerMode = layerMode;
layer.drawPixels();
System.out.println("thread");
}
}
}
}
}