From 7688de83b239d6316157e4b972be4c34001a0f9f Mon Sep 17 00:00:00 2001 From: HolyGlory Date: Sun, 1 Aug 2021 00:19:05 +0300 Subject: [PATCH] hitbox: circle - working,rectangle - coords 0 0 --- src/GUI.java | 123 +++++++++++++++++++++++++++++------------ src/Load_XML_File.java | 88 ++++++++++++++++++++++++----- 2 files changed, 163 insertions(+), 48 deletions(-) diff --git a/src/GUI.java b/src/GUI.java index 8f1173d..28b3222 100644 --- a/src/GUI.java +++ b/src/GUI.java @@ -27,7 +27,9 @@ import javax.swing.filechooser.FileNameExtensionFilter; public class GUI extends JFrame { List listPoints = new ArrayList<>(); + List saveListPoints = new ArrayList<>(); String filePath; + ImageIcon icon; JRadioButton radioButtonDrawBox; JRadioButton radioButtonHBRectangle; JRadioButton radioButtonHBCircle; @@ -35,7 +37,7 @@ public class GUI extends JFrame { //фрейм setTitle("DrawBox points");//название setLayout(null);//отключаем автоматическое расположение - setSize(800,600);//размер фрейма + setSize(1200,780);//размер фрейма setLocationRelativeTo(null);//окно по центру setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -43,22 +45,22 @@ public class GUI extends JFrame { JPanel buttonCase = new JPanel(); //buttonCase.setBorder(new LineBorder(Color.black)); buttonCase.setLayout(null); - buttonCase.setSize(200, 600);//позже задать значения + buttonCase.setSize(220, 780);//позже задать значения ButtonGroup groupRadioButton = new ButtonGroup(); - radioButtonDrawBox = createRadioButton("DrawBox Quadrangle ",buttonCase,groupRadioButton,10,200); - radioButtonHBRectangle = createRadioButton("Hitbox Rectangle ",buttonCase,groupRadioButton,10,230); - radioButtonHBCircle = createRadioButton("HitBox Circle ",buttonCase,groupRadioButton,10,260); + radioButtonDrawBox = createRadioButton("DrawBox Quadrangle ",buttonCase,groupRadioButton,10,340); + radioButtonHBRectangle = createRadioButton("Hitbox Rectangle ",buttonCase,groupRadioButton,10,370); + radioButtonHBCircle = createRadioButton("HitBox Circle ",buttonCase,groupRadioButton,10,400); JLabel labelPicture = new JLabel(); labelPicture.setLayout(null); labelPicture.setHorizontalAlignment(JLabel.CENTER); labelPicture.setVerticalAlignment(JLabel.CENTER); - labelPicture.setLocation(202, 2); + labelPicture.setLocation(222, 2); labelPicture.setBorder(new EtchedBorder(EtchedBorder.RAISED)); - labelPicture.setSize(579,557); + labelPicture.setSize(958,737); PaintJLabel labelDrawing = new PaintJLabel(listPoints); @@ -73,14 +75,23 @@ public class GUI extends JFrame { } } - if(radioButtonHBRectangle.isSelected()||radioButtonHBCircle.isSelected()) { - if(labelDrawing.listPoints.size()<2) { + if(radioButtonHBRectangle.isSelected()) { + if(labelDrawing.listPoints.size()<3) { labelDrawing.x = e.getX(); 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() { @@ -96,11 +107,11 @@ public class GUI extends JFrame { } if(labelDrawing.listPoints.size() ==4){ - PointLine p1 = labelDrawing.listPoints.get(0); + PointLine p1 = labelDrawing.listPoints.get(0); PointLine p2 = labelDrawing.listPoints.get(labelDrawing.listPoints.size()-1); labelDrawing.listLines.add(new Line2D.Float(p1.x,p1.y,p2.x,p2.y)); 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()); if(radioButtonHBRectangle.isSelected()) { - if(labelDrawing.listPoints.size()<3) { + if(labelDrawing.listPoints.size()<4) { labelDrawing.listPoints.add(new PointLine(e.getX(), e.getY())); - - 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)); - labelDrawing.listPoints.add(new PointLine(listPoints.get(1).x,listPoints.get(0).y-(listPoints.get(1).y-listPoints.get(0).y))); + 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()==3) { + 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(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)); @@ -126,10 +142,13 @@ public class GUI extends JFrame { if(radioButtonHBCircle.isSelected()) { - labelDrawing.listPoints.add(new PointLine(e.getX(), e.getY())); + if(labelDrawing.listPoints.size()<2) { + labelDrawing.listPoints.add(new PointLine(e.getX(), e.getY())); + } } 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("XML-file path",buttonCase,3,50,openXmlFile); createButton("Save",buttonCase,3,100,new CustomButtonListenerSave()); - createButton("Clear lines",buttonCase,3,512,ClicklabelDraw); + createButton("Clear lines",buttonCase,3,688,ClicklabelDraw); add(buttonCase); @@ -167,7 +186,7 @@ public class GUI extends JFrame { private JButton createButton(String text,JPanel buttonCase,int width,int height,ActionListener listener) { JButton button = new JButton(text); - button.setSize(new Dimension(196, 50)); + button.setSize(new Dimension(218, 50)); button.setLocation(width, height); button.addActionListener(listener); buttonCase.add(button); @@ -213,44 +232,69 @@ public class GUI extends JFrame { graphic2d.setColor(Color.BLUE); graphic2d.draw(new Line2D.Float(a.x,a.y,x,y)); } - if(listPoints.size()==4) { + if(listPoints.size()>=1) { for(int i = 0;i1) { + 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.drawRect(0, 0, width-1, heigth-1); } void print() { for(int i=0;i list,String file){ + Load_XML_File(String mainName,List list,String file,String artibute,String radiobutt,ImageIcon iconMain){ filePath = file; + icon = iconMain; + radioButton = radiobutt; xmlFile = new File(filePath); dbFactory = DocumentBuilderFactory.newInstance(); name = mainName; + atrr = artibute; try { dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); @@ -45,13 +52,13 @@ public class Load_XML_File { Element elementProperty = null; for (int j = 0; j < propertyList.getLength(); j++) { elementProperty = (Element) propertyList.item(j); - if (elementProperty.getAttribute("name").equalsIgnoreCase("drawbox")) + if (elementProperty.getAttribute("name").equalsIgnoreCase(atrr)) currentObjecttypeNode.removeChild(elementProperty); } Element newProperty = doc.createElement("property"); Attr attr = doc.createAttribute("name"); - attr.setValue("drawbox"); + attr.setValue(atrr); newProperty.setAttributeNode(attr); attr = doc.createAttribute("type"); @@ -62,19 +69,44 @@ public class Load_XML_File { System.out.println("elementProperty" + elementProperty.getTagName()); System.out.println("newProperty" + newProperty.getTagName()); System.out.println("currentObjecttypeNode is parent to elementProperty" + elementProperty.getParentNode()); - String text = ""; - int coord; - for (int k = 0; k < list.size(); k++) { - coord = Math.round(list.get(k).x); - text += coord; - text += ' '; - coord = Math.round(list.get(k).y); - text += coord; - if (k != (list.size() - 1)) + if(radioButton.equalsIgnoreCase("drawbox")) { + + int coord; + for (int k = 0; k < list.size(); k++) { + coord = Math.round(list.get(k).x); + text += coord; text += ' '; + coord = Math.round(list.get(k).y); + text += coord; + if (k != (list.size() - 1)) + 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); } - System.out.println("cords: " + text); attr = doc.createAttribute("default"); attr.setValue(text); newProperty.setAttributeNode(attr); @@ -111,4 +143,34 @@ public class Load_XML_File { } return r; } + public String RectangleData(List list,ImageIcon iconMain) { + + List 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; + } } +