package views;
import gui.*;
import java.awt.Graphics;
import modell.Cell;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.JComponent;
public class CellView extends JComponent {
private Cell mycell;
private Coordinates mycoords;
private Color bg_color = new Color(255, 255, 255);
private Color path_bg_color = new Color(173, 216, 230);
private Color fg_color = new Color(0, 0, 0);
private Color nyil_color = new Color(0, 0, 0);
private Color vonas_color = new Color(0, 0, 0);
private int topleftx = 0;
private int toplefty = 0;
private int diagonal_size = 60; //cella atmeroje
private int cropsize = 10; //mennyivel beljebb kezdjunk egy vonalat
private int honnan_x;
private int honnan_y;
private int hova_x;
private int hova_y;
private int vonas1_x;
private int vonas1_y;
private int vonas2_x;
private int vonas2_y;
BasicStroke korvonal_stroke = new BasicStroke(3);
BasicStroke athuzas_stroke = new BasicStroke(3);
BasicStroke nyil_stroke = new BasicStroke(3);
BasicStroke vonas_stroke = new BasicStroke(3);
public CellView(Cell cell, Coordinates coords) {
this.mycell = cell;
mycell.setMyview(this);
this.setMycoords(coords);
topleftx = mycoords.getX() - diagonal_size / 2;
toplefty = mycoords.getY() - diagonal_size / 2;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//cella hátterének kirajzolása attól függően hogy benne van-e a path-ben
if (this.mycell.getGame().getProg().getMain_controller().getPath().contains(this.mycell)) {
g2d.setColor(getPath_bg_color());
} else {
g2d.setColor(this.getBg_color());
}
g2d.fillOval(getTopleftx(), getToplefty(), getDiagonal_size(), getDiagonal_size());
//cella kerületének és a rajta lévő "X" kirajzolása
g2d.setColor(getFg_color());
g2d.setStroke(korvonal_stroke);
g2d.drawOval(getTopleftx(), getToplefty(), getDiagonal_size(), getDiagonal_size());
g2d.setStroke(athuzas_stroke);
g2d.drawLine(getTopleftx() + getCropsize(), getToplefty() + getCropsize(), getTopleftx() + getDiagonal_size() - getCropsize(), getToplefty() + getDiagonal_size() - getCropsize());
g2d.drawLine(getTopleftx() + getCropsize(), getToplefty() + getDiagonal_size() - getCropsize(), getTopleftx() + getDiagonal_size() - getCropsize(), getToplefty() + getCropsize());
//a szomszédok irányába mutató nyilak kirajzolása
g2d.setColor(getNyil_color());
for (int i = 0; i < this.mycell.getNeighbours_out().size(); i++) {
//honnan hova tart a nyil (cellak kozepe elso kozelitesben)
honnan_x = mycoords.getX();
honnan_y = mycoords.getY();
hova_x = mycell.getNeighbours_out().get(i).getMyview().getMycoords().getX();
hova_y = mycell.getNeighbours_out().get(i).getMyview().getMycoords().getY();
g2d.setStroke(nyil_stroke);
//kiszamoljuk mennyivel lesz rövidebb a nyil hogy ne lógjon rá a cellára
int delta_x = hova_x - honnan_x;
int delta_y = hova_y - honnan_y;
if(delta_x < 0) delta_x = delta_x * (-1);
if(delta_y < 0) delta_y = delta_y * (-1);
int atlo = (int) Math.sqrt(delta_x * delta_x + delta_y * delta_y);
int kisdelta_x = delta_x * (diagonal_size / 2) / atlo;
int kisdelta_y = delta_y * (diagonal_size / 2) / atlo;
// System.out.println(atlo + " " + kisdelta_x + " " + kisdelta_y);
//feltételrendszer amiben beállítjuk a végleges koordinátákat az irány függvényében
if (honnan_x < hova_x) {
if (honnan_y < hova_y) {
//jobbra lefele
honnan_x = honnan_x + kisdelta_x;
honnan_y = honnan_y + kisdelta_y;
hova_x = hova_x - kisdelta_x;
hova_y = hova_y - kisdelta_y;
vonas1_x = hova_x;
vonas1_y = hova_y - 4;
vonas2_x = hova_x - 4;
vonas2_y = hova_y;
} else if (honnan_y > hova_y) {
//jobbra felfele
honnan_x = honnan_x + kisdelta_x;
honnan_y = honnan_y - kisdelta_y;
hova_x = hova_x - kisdelta_x;
hova_y = hova_y + kisdelta_y;
vonas1_x = hova_x;
vonas1_y = hova_y + 4;
vonas2_x = hova_x - 4;
vonas2_y = hova_y;
} else {
//jobbra
honnan_x = honnan_x + kisdelta_x;
hova_x = hova_x - kisdelta_x;
vonas1_x = hova_x - 4;
vonas1_y = hova_y - 4;
vonas2_x = hova_x - 4;
vonas2_y = hova_y + 4;
}
} else if (honnan_x > hova_x) {
if (honnan_y < hova_y) {
//balra lefele
honnan_x = honnan_x - kisdelta_x;
honnan_y = honnan_y + kisdelta_y;
hova_x = hova_x + kisdelta_x;
hova_y = hova_y - kisdelta_y;
vonas1_x = hova_x;
vonas1_y = hova_y - 4;
vonas2_x = hova_x + 4;
vonas2_y = hova_y;
} else if (honnan_y > hova_y) {
//balra felfele
honnan_x = honnan_x - kisdelta_x;
honnan_y = honnan_y - kisdelta_y;
hova_x = hova_x + kisdelta_x;
hova_y = hova_y + kisdelta_y;
vonas1_x = hova_x;
vonas1_y = hova_y + 4;
vonas2_x = hova_x + 4;
vonas2_y = hova_y;
} else {
//balra
honnan_x = honnan_x - kisdelta_x;
hova_x = hova_x + kisdelta_x;
vonas1_x = hova_x + 4;
vonas1_y = hova_y - 4;
vonas2_x = hova_x + 4;
vonas2_y = hova_y + 4;
}
} else {
if (honnan_y < hova_y) {
//lefele
honnan_y = honnan_y + kisdelta_y;
hova_y = hova_y - kisdelta_y;
vonas1_x = hova_x + 4;
vonas1_y = hova_y - 4;
vonas2_x = hova_x - 4;
vonas2_y = hova_y - 4;
} else {
//felfele
honnan_y = honnan_y - kisdelta_y;
hova_y = hova_y + kisdelta_y;
vonas1_x = hova_x + 4;
vonas1_y = hova_y + 4;
vonas2_x = hova_x - 4;
vonas2_y = hova_y + 4;
}
}
//nyil testének kirajzolása
g2d.drawLine(honnan_x, honnan_y, hova_x, hova_y);
//nyilak végén lévő vonások kirajzolása
g2d.setStroke(vonas_stroke);
g2d.setColor(getVonas_color());
g2d.drawLine(hova_x, hova_y, vonas1_x, vonas1_y);
g2d.drawLine(hova_x, hova_y, vonas2_x, vonas2_y);
}
}
/**
* @return the mycell
*/
public Cell getMycell() {
return mycell;
}
/**
* @param mycell the mycell to set
*/
public void setMycell(Cell mycell) {
this.mycell = mycell;
}
/**
* @return the diagonal_size
*/
public int getDiagonal_size() {
return diagonal_size;
}
/**
* @param diagonal_size the diagonal_size to set
*/
public void setDiagonal_size(int diagonal_size) {
this.diagonal_size = diagonal_size;
}
/**
* @return the cropsize
*/
public int getCropsize() {
return cropsize;
}
/**
* @param cropsize the cropsize to set
*/
public void setCropsize(int cropsize) {
this.cropsize = cropsize;
}
/**
* @return the topleftx
*/
public int getTopleftx() {
return topleftx;
}
/**
* @param topleftx the topleftx to set
*/
public void setTopleftx(int topleftx) {
this.topleftx = topleftx;
}
/**
* @return the toplefty
*/
public int getToplefty() {
return toplefty;
}
/**
* @param toplefty the toplefty to set
*/
public void setToplefty(int toplefty) {
this.toplefty = toplefty;
}
/**
* @return the bg_color
*/
public Color getBg_color() {
return bg_color;
}
/**
* @param bg_color the bg_color to set
*/
public void setBg_color(Color bg_color) {
this.bg_color = bg_color;
}
/**
* @return the mycoords
*/
public Coordinates getMycoords() {
return mycoords;
}
/**
* @param mycoords the mycoords to set
*/
public void setMycoords(Coordinates mycoords) {
this.mycoords = mycoords;
}
/**
* @return the fg_color
*/
public Color getFg_color() {
return fg_color;
}
/**
* @param fg_color the fg_color to set
*/
public void setFg_color(Color fg_color) {
this.fg_color = fg_color;
}
/**
* @return the nyil_color
*/
public Color getNyil_color() {
return nyil_color;
}
/**
* @param nyil_color the nyil_color to set
*/
public void setNyil_color(Color nyil_color) {
this.nyil_color = nyil_color;
}
/**
* @return the vonas_color
*/
public Color getVonas_color() {
return vonas_color;
}
/**
* @param vonas_color the vonas_color to set
*/
public void setVonas_color(Color vonas_color) {
this.vonas_color = vonas_color;
}
/**
* @return the path_bg_color
*/
public Color getPath_bg_color() {
return path_bg_color;
}
/**
* @param path_bg_color the path_bg_color to set
*/
public void setPath_bg_color(Color path_bg_color) {
this.path_bg_color = path_bg_color;
}
}