hitbox: circle - working,rectangle - coords 0 0

This commit is contained in:
HolyGlory
2021-08-01 00:19:05 +03:00
parent af20ae5dd2
commit 7688de83b2
2 changed files with 163 additions and 48 deletions

View File

@ -27,7 +27,9 @@ import javax.swing.filechooser.FileNameExtensionFilter;
public class GUI extends JFrame { public class GUI extends JFrame {
List<PointLine> listPoints = new ArrayList<>(); List<PointLine> listPoints = new ArrayList<>();
List<PointLine> saveListPoints = new ArrayList<>();
String filePath; String filePath;
ImageIcon icon;
JRadioButton radioButtonDrawBox; JRadioButton radioButtonDrawBox;
JRadioButton radioButtonHBRectangle; JRadioButton radioButtonHBRectangle;
JRadioButton radioButtonHBCircle; JRadioButton radioButtonHBCircle;
@ -35,7 +37,7 @@ public class GUI extends JFrame {
//����� //�����
setTitle("DrawBox points");//�������� setTitle("DrawBox points");//��������
setLayout(null);//��������� �������������� ������������ setLayout(null);//��������� �������������� ������������
setSize(800,600);//������ ������ setSize(1200,780);//������ ������
setLocationRelativeTo(null);//���� �� ������ setLocationRelativeTo(null);//���� �� ������
setResizable(false); setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@ -43,22 +45,22 @@ public class GUI extends JFrame {
JPanel buttonCase = new JPanel(); JPanel buttonCase = new JPanel();
//buttonCase.setBorder(new LineBorder(Color.black)); //buttonCase.setBorder(new LineBorder(Color.black));
buttonCase.setLayout(null); buttonCase.setLayout(null);
buttonCase.setSize(200, 600);//����� ������ �������� buttonCase.setSize(220, 780);//����� ������ ��������
ButtonGroup groupRadioButton = new ButtonGroup(); ButtonGroup groupRadioButton = new ButtonGroup();
radioButtonDrawBox = createRadioButton("DrawBox Quadrangle ",buttonCase,groupRadioButton,10,200); radioButtonDrawBox = createRadioButton("DrawBox Quadrangle ",buttonCase,groupRadioButton,10,340);
radioButtonHBRectangle = createRadioButton("Hitbox Rectangle ",buttonCase,groupRadioButton,10,230); radioButtonHBRectangle = createRadioButton("Hitbox Rectangle ",buttonCase,groupRadioButton,10,370);
radioButtonHBCircle = createRadioButton("HitBox Circle ",buttonCase,groupRadioButton,10,260); radioButtonHBCircle = createRadioButton("HitBox Circle ",buttonCase,groupRadioButton,10,400);
JLabel labelPicture = new JLabel(); JLabel labelPicture = new JLabel();
labelPicture.setLayout(null); labelPicture.setLayout(null);
labelPicture.setHorizontalAlignment(JLabel.CENTER); labelPicture.setHorizontalAlignment(JLabel.CENTER);
labelPicture.setVerticalAlignment(JLabel.CENTER); labelPicture.setVerticalAlignment(JLabel.CENTER);
labelPicture.setLocation(202, 2); labelPicture.setLocation(222, 2);
labelPicture.setBorder(new EtchedBorder(EtchedBorder.RAISED)); labelPicture.setBorder(new EtchedBorder(EtchedBorder.RAISED));
labelPicture.setSize(579,557); labelPicture.setSize(958,737);
PaintJLabel labelDrawing = new PaintJLabel(listPoints); PaintJLabel labelDrawing = new PaintJLabel(listPoints);
@ -73,14 +75,23 @@ public class GUI extends JFrame {
} }
} }
if(radioButtonHBRectangle.isSelected()||radioButtonHBCircle.isSelected()) { if(radioButtonHBRectangle.isSelected()) {
if(labelDrawing.listPoints.size()<2) { if(labelDrawing.listPoints.size()<3) {
labelDrawing.x = e.getX(); labelDrawing.x = e.getX();
labelDrawing.y = e.getY(); labelDrawing.y = e.getY();
}
labelDrawing.repaint(); labelDrawing.repaint();
} }
// System.out.println("X = "+e.getX()+" Y = "+e.getY());
}
if(radioButtonHBCircle.isSelected()) {
if(labelDrawing.listPoints.size()<3) {
labelDrawing.x = e.getX();
labelDrawing.y = e.getY();
labelDrawing.repaint();
}
}
} }
}); });
labelDrawing.addMouseListener(new MouseAdapter() { labelDrawing.addMouseListener(new MouseAdapter() {
@ -100,7 +111,7 @@ public class GUI extends JFrame {
PointLine p2 = labelDrawing.listPoints.get(labelDrawing.listPoints.size()-1); PointLine p2 = labelDrawing.listPoints.get(labelDrawing.listPoints.size()-1);
labelDrawing.listLines.add(new Line2D.Float(p1.x,p1.y,p2.x,p2.y)); labelDrawing.listLines.add(new Line2D.Float(p1.x,p1.y,p2.x,p2.y));
labelDrawing.sortPoint(); labelDrawing.sortPoint();
labelDrawing.listLines.add(new Line2D.Float(labelDrawing.listPoints.get(2).x,labelDrawing.listPoints.get(2).y+3,labelDrawing.listPoints.get(3).x,labelDrawing.listPoints.get(3).y+3)); labelDrawing.listLines.add(new Line2D.Float(saveListPoints.get(2).x,saveListPoints.get(2).y+3,saveListPoints.get(3).x,saveListPoints.get(3).y+3));
} }
} }
@ -109,12 +120,17 @@ public class GUI extends JFrame {
System.out.println("labelDrawing.listLines.size() = "+labelDrawing.listLines.size()); System.out.println("labelDrawing.listLines.size() = "+labelDrawing.listLines.size());
if(radioButtonHBRectangle.isSelected()) { if(radioButtonHBRectangle.isSelected()) {
if(labelDrawing.listPoints.size()<3) { if(labelDrawing.listPoints.size()<4) {
labelDrawing.listPoints.add(new PointLine(e.getX(), e.getY())); labelDrawing.listPoints.add(new PointLine(e.getX(), e.getY()));
if(labelDrawing.listPoints.size() > 1 && labelDrawing.listPoints.size() <=3) {
PointLine p1 = labelDrawing.listPoints.get(labelDrawing.listPoints.size()-2);
PointLine p2 = labelDrawing.listPoints.get(labelDrawing.listPoints.size()-1);
labelDrawing.listLines.add(new Line2D.Float(p1.x,p1.y,p2.x,p2.y));
if(labelDrawing.listPoints.size()==2) { }
labelDrawing.listPoints.add(new PointLine(listPoints.get(1).x+(listPoints.get(1).x-listPoints.get(0).x),listPoints.get(0).y)); if(labelDrawing.listPoints.size()==3) {
labelDrawing.listPoints.add(new PointLine(listPoints.get(1).x,listPoints.get(0).y-(listPoints.get(1).y-listPoints.get(0).y))); labelDrawing.listLines.clear();
labelDrawing.listPoints.add(new PointLine(listPoints.get(2).x - (listPoints.get(1).x-listPoints.get(0).x),listPoints.get(2).y-(listPoints.get(1).y-listPoints.get(0).y)));
labelDrawing.listLines.add(new Line2D.Float(listPoints.get(0).x,listPoints.get(0).y,listPoints.get(1).x,listPoints.get(1).y)); labelDrawing.listLines.add(new Line2D.Float(listPoints.get(0).x,listPoints.get(0).y,listPoints.get(1).x,listPoints.get(1).y));
labelDrawing.listLines.add(new Line2D.Float(listPoints.get(1).x,listPoints.get(1).y,listPoints.get(2).x,listPoints.get(2).y)); labelDrawing.listLines.add(new Line2D.Float(listPoints.get(1).x,listPoints.get(1).y,listPoints.get(2).x,listPoints.get(2).y));
labelDrawing.listLines.add(new Line2D.Float(listPoints.get(2).x,listPoints.get(2).y,listPoints.get(3).x,listPoints.get(3).y)); labelDrawing.listLines.add(new Line2D.Float(listPoints.get(2).x,listPoints.get(2).y,listPoints.get(3).x,listPoints.get(3).y));
@ -126,10 +142,13 @@ public class GUI extends JFrame {
if(radioButtonHBCircle.isSelected()) { if(radioButtonHBCircle.isSelected()) {
if(labelDrawing.listPoints.size()<2) {
labelDrawing.listPoints.add(new PointLine(e.getX(), e.getY())); labelDrawing.listPoints.add(new PointLine(e.getX(), e.getY()));
} }
}
labelDrawing.repaint(); labelDrawing.repaint();
System.out.println("Size = "+labelDrawing.listPoints.size()); System.out.println("Size listPoints = "+labelDrawing.listPoints.size());
System.out.println("Size listLine = "+labelDrawing.listLines.size());
} }
}); });
//������ ������ ����� //������ ������ �����
@ -148,7 +167,7 @@ public class GUI extends JFrame {
createButton("Open picture",buttonCase,3,0,openPicture); createButton("Open picture",buttonCase,3,0,openPicture);
createButton("XML-file path",buttonCase,3,50,openXmlFile); createButton("XML-file path",buttonCase,3,50,openXmlFile);
createButton("Save",buttonCase,3,100,new CustomButtonListenerSave()); createButton("Save",buttonCase,3,100,new CustomButtonListenerSave());
createButton("Clear lines",buttonCase,3,512,ClicklabelDraw); createButton("Clear lines",buttonCase,3,688,ClicklabelDraw);
add(buttonCase); add(buttonCase);
@ -167,7 +186,7 @@ public class GUI extends JFrame {
private JButton createButton(String text,JPanel buttonCase,int width,int height,ActionListener listener) { private JButton createButton(String text,JPanel buttonCase,int width,int height,ActionListener listener) {
JButton button = new JButton(text); JButton button = new JButton(text);
button.setSize(new Dimension(196, 50)); button.setSize(new Dimension(218, 50));
button.setLocation(width, height); button.setLocation(width, height);
button.addActionListener(listener); button.addActionListener(listener);
buttonCase.add(button); buttonCase.add(button);
@ -213,44 +232,69 @@ public class GUI extends JFrame {
graphic2d.setColor(Color.BLUE); graphic2d.setColor(Color.BLUE);
graphic2d.draw(new Line2D.Float(a.x,a.y,x,y)); graphic2d.draw(new Line2D.Float(a.x,a.y,x,y));
} }
if(listPoints.size()==4) { if(listPoints.size()>=1) {
for(int i = 0;i<listLines.size();i++) { for(int i = 0;i<listLines.size();i++) {
graphic2d.draw(listLines.get(i)); graphic2d.draw(listLines.get(i));
} }
} }
}
if(radioButtonHBCircle.isSelected()) {
if(listPoints.size()==1) {
int radiusX = Math.round(x-listPoints.get(0).x);
int radiusY = radiusX/2;
g.drawOval(Math.round(listPoints.get(0).x), Math.round(listPoints.get(0).y-radiusY/2), radiusX,radiusY);
}
if(listPoints.size()>1) {
if(listPoints.get(0).x>listPoints.get(1).x) {
Collections.swap(listPoints, 0, 1);
}
int radiusX = Math.round(listPoints.get(1).x-listPoints.get(0).x);
int radiusY = radiusX/2;
g.drawOval(Math.round(listPoints.get(0).x), Math.round(listPoints.get(0).y-radiusY/2), radiusX,radiusY);
}
} }
graphic2d.setColor(Color.BLACK); graphic2d.setColor(Color.BLACK);
graphic2d.drawRect(0, 0, width-1, heigth-1); graphic2d.drawRect(0, 0, width-1, heigth-1);
} }
void print() { void print() {
for(int i=0;i<this.listPoints.size();i++) { for(int i=0;i<this.listPoints.size();i++) {
System.out.println("PointX["+i+"]"+listPoints.get(i).x+"PointY["+i+"]"+listPoints.get(i).y); System.out.println("PointX["+i+"] = "+listPoints.get(i).x+"; PointY["+i+"] ="+listPoints.get(i).y);
} }
} }
public void sortPoint() { public void sortPoint() {
for(int i = 0;i<3;i++) { copyPointList();
for(int j = i+1;j<4;j++) { for(int i = 0;i<listPoints.size()-1;i++) {
if(listPoints.get(i).y>listPoints.get(j).y) { for(int j = i+1;j<listPoints.size();j++) {
Collections.swap(listPoints, i, j); if(saveListPoints.get(i).y>saveListPoints.get(j).y) {
Collections.swap(saveListPoints, i, j);
} }
} }
} }
if(listPoints.get(0).x>listPoints.get(1).x) { if(listPoints.size()>2) {
Collections.swap(listPoints, 0, 1); if(saveListPoints.get(0).x>saveListPoints.get(1).x) {
Collections.swap(saveListPoints, 0, 1);
} }
if(listPoints.get(2).x<listPoints.get(3).x) { if(saveListPoints.get(2).x<saveListPoints.get(3).x) {
Collections.swap(listPoints, 2, 3); Collections.swap(saveListPoints, 2, 3);
}
}
}
void copyPointList() {
for(int i = 0;i<listPoints.size();i++) {
saveListPoints.add(listPoints.get(i));
} }
print();
} }
} }
class CustomButtonListenerOpenPicture implements ActionListener { class CustomButtonListenerOpenPicture implements ActionListener {
private JLabel label; private JLabel label;
ImageIcon icon;
JFileChooser fileChooser; JFileChooser fileChooser;
PaintJLabel labelDraw; PaintJLabel labelDraw;
CustomButtonListenerOpenPicture(JLabel mainLabel,JFileChooser file,PaintJLabel mainPaintJlabel){ CustomButtonListenerOpenPicture(JLabel mainLabel,JFileChooser file,PaintJLabel mainPaintJlabel){
@ -272,7 +316,7 @@ public class GUI extends JFrame {
if(icon!=null) { if(icon!=null) {
int widthIcon = icon.getIconWidth(); int widthIcon = icon.getIconWidth();
int heigthIcon = icon.getIconHeight(); int heigthIcon = icon.getIconHeight();
labelDraw.setLocation(((579-widthIcon)/2+202), (557-heigthIcon)/2+2); labelDraw.setLocation(((958-widthIcon)/2+222), (737-heigthIcon)/2+2);
labelDraw.setBorder(new EtchedBorder(EtchedBorder.RAISED)); labelDraw.setBorder(new EtchedBorder(EtchedBorder.RAISED));
labelDraw.setSize(widthIcon,heigthIcon); labelDraw.setSize(widthIcon,heigthIcon);
labelDraw.width = widthIcon; labelDraw.width = widthIcon;
@ -303,7 +347,15 @@ public class GUI extends JFrame {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
/*���� ��� ������ ������*/ /*���� ��� ������ ������*/
String objectName = JOptionPane.showInputDialog(null, "������� ��� �������"); String objectName = JOptionPane.showInputDialog(null, "������� ��� �������");
new Load_XML_File(objectName,listPoints,filePath); if(radioButtonDrawBox.isSelected()) {
new Load_XML_File(objectName,saveListPoints,filePath,"drawbox","drawbox",icon);
}
if(radioButtonHBRectangle.isSelected()) {
new Load_XML_File(objectName,listPoints,filePath,"hitbox","hitboxRectangle",icon);
}
if(radioButtonHBCircle.isSelected()) {
new Load_XML_File(objectName,listPoints,filePath,"hitbox","hitboxCircle",icon);
}
} }
} }
@ -317,6 +369,7 @@ public class GUI extends JFrame {
/*���� ��� ������ ������*/ /*���� ��� ������ ������*/
label.listLines.clear(); label.listLines.clear();
label.listPoints.clear(); label.listPoints.clear();
saveListPoints.clear();
repaint(); repaint();
} }
} }

View File

@ -3,6 +3,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.swing.ImageIcon;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
@ -20,16 +21,22 @@ import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
public class Load_XML_File { public class Load_XML_File {
ImageIcon icon;
String name; String name;
String atrr;
String radioButton;
String filePath; String filePath;
File xmlFile; File xmlFile;
DocumentBuilder dBuilder; DocumentBuilder dBuilder;
DocumentBuilderFactory dbFactory; DocumentBuilderFactory dbFactory;
Load_XML_File(String mainName,List<PointLine> list,String file){ Load_XML_File(String mainName,List<PointLine> list,String file,String artibute,String radiobutt,ImageIcon iconMain){
filePath = file; filePath = file;
icon = iconMain;
radioButton = radiobutt;
xmlFile = new File(filePath); xmlFile = new File(filePath);
dbFactory = DocumentBuilderFactory.newInstance(); dbFactory = DocumentBuilderFactory.newInstance();
name = mainName; name = mainName;
atrr = artibute;
try { try {
dBuilder = dbFactory.newDocumentBuilder(); dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile); Document doc = dBuilder.parse(xmlFile);
@ -45,13 +52,13 @@ public class Load_XML_File {
Element elementProperty = null; Element elementProperty = null;
for (int j = 0; j < propertyList.getLength(); j++) { for (int j = 0; j < propertyList.getLength(); j++) {
elementProperty = (Element) propertyList.item(j); elementProperty = (Element) propertyList.item(j);
if (elementProperty.getAttribute("name").equalsIgnoreCase("drawbox")) if (elementProperty.getAttribute("name").equalsIgnoreCase(atrr))
currentObjecttypeNode.removeChild(elementProperty); currentObjecttypeNode.removeChild(elementProperty);
} }
Element newProperty = doc.createElement("property"); Element newProperty = doc.createElement("property");
Attr attr = doc.createAttribute("name"); Attr attr = doc.createAttribute("name");
attr.setValue("drawbox"); attr.setValue(atrr);
newProperty.setAttributeNode(attr); newProperty.setAttributeNode(attr);
attr = doc.createAttribute("type"); attr = doc.createAttribute("type");
@ -62,8 +69,9 @@ public class Load_XML_File {
System.out.println("elementProperty" + elementProperty.getTagName()); System.out.println("elementProperty" + elementProperty.getTagName());
System.out.println("newProperty" + newProperty.getTagName()); System.out.println("newProperty" + newProperty.getTagName());
System.out.println("currentObjecttypeNode is parent to elementProperty" + elementProperty.getParentNode()); System.out.println("currentObjecttypeNode is parent to elementProperty" + elementProperty.getParentNode());
String text = ""; String text = "";
if(radioButton.equalsIgnoreCase("drawbox")) {
int coord; int coord;
for (int k = 0; k < list.size(); k++) { for (int k = 0; k < list.size(); k++) {
coord = Math.round(list.get(k).x); coord = Math.round(list.get(k).x);
@ -75,6 +83,30 @@ public class Load_XML_File {
text += ' '; text += ' ';
} }
System.out.println("cords: " + text); System.out.println("cords: " + text);
}
if(radioButton.equalsIgnoreCase("hitboxRectangle")) {
text = RectangleData(list,icon);
System.out.println("cords: " + text);
}
if(radioButton.equalsIgnoreCase("hitboxCircle")) {
int dianetrX = Math.round(list.get(1).x-list.get(0).x);
int dianetrY = dianetrX/2;
PointLine pointUnderCenter = pointCartesian(list.get(0).x+dianetrX/2,list.get(0).y+dianetrY/2);
PointLine pointCenter = pointCartesian(list.get(0).x+dianetrX/2,list.get(0).y);
PointLine pointStart = pointCartesian(iconMain.getIconWidth()/2,iconMain.getIconHeight());
int coord;
text += "Circle ";
int x = Math.round(pointUnderCenter.x-pointStart.x)*(-1);
int y = Math.round(pointUnderCenter.y-pointStart.y)*(-1);
text+= x+" ";
text+= y+" ";
float temp = (float)Math.sqrt(2);
coord = Math.round((dianetrY/2)*temp);
text += coord;
System.out.println("cords: " + text);
}
attr = doc.createAttribute("default"); attr = doc.createAttribute("default");
attr.setValue(text); attr.setValue(text);
newProperty.setAttributeNode(attr); newProperty.setAttributeNode(attr);
@ -111,4 +143,34 @@ public class Load_XML_File {
} }
return r; return r;
} }
public String RectangleData(List<PointLine> list,ImageIcon iconMain) {
List<PointLine> listRectangle = new ArrayList<>();
listRectangle.add(pointCartesian(list.get(0).x, list.get(0).y));
listRectangle.add(pointCartesian(list.get(2).x, list.get(2).y));
listRectangle.add(cartesianToIsometric(listRectangle.get(1).x, list.get(0).y));
PointLine pointStartRectangle = pointCartesian(list.get(0).x+(Math.abs(list.get(2).x-list.get(0).x))/2, listRectangle.get(2).y);
PointLine pointStart = pointCartesian(iconMain.getIconWidth()/2,iconMain.getIconHeight());
//
// int x = Math.round(pointStartRectangle.x-pointStart.x)*(-1);
// int y = Math.round(pointStartRectangle.y-pointStart.y)*(-1);
String textData="Rectangle 0 0 ";
// textData+= x+" ";
// textData+= y+" ";
//
int coords = Math.abs(Math.round(listRectangle.get(0).x -listRectangle.get(1).x));
textData += coords+" ";
coords = Math.abs(Math.round(listRectangle.get(0).y -listRectangle.get(1).y));
textData += coords;
return textData;
} }
public PointLine pointCartesian(float isox, float isoy) {
PointLine point = new PointLine((2 * isoy + isox) / 2,(2 * isoy - isox) / 2);
return point;
}
public PointLine cartesianToIsometric(float cartX, float cartY) {
PointLine point = new PointLine(cartX - cartY,(cartX + cartY) / 2);
return point;
}
}