commit dc54ff2ce7454a7f364e6993be4ec9ba61e21f22 Author: Maggistrator Date: Tue Jul 20 21:33:23 2021 +0300 bugs fixed, drawboxes now working properly diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..7d3b4d8 --- /dev/null +++ b/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..df4cb0d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/bin/ +.settings/org.eclipse.core.resources.prefs diff --git a/.project b/.project new file mode 100644 index 0000000..26f1df6 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Creating-drawbox-points + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..223b166 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=15 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=15 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=15 diff --git a/src/ClassicMainClass.java b/src/ClassicMainClass.java new file mode 100644 index 0000000..c4a466a --- /dev/null +++ b/src/ClassicMainClass.java @@ -0,0 +1,47 @@ +import javax.swing.UIManager; +import javax.swing.UnsupportedLookAndFeelException; + +/** + * + */ + +/** + * @author USER_1 + * + */ +public class ClassicMainClass { + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException | InstantiationException | javax.swing.UnsupportedLookAndFeelException | IllegalAccessException ex) { + System.out.println("laf sucks"); + } + + GUI frame = new GUI(); + frame.setVisible(true); + + + } + + public void interface1() { + + } + void interface2() { + try { + UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { + e.printStackTrace(); + } + } +} diff --git a/src/GUI.java b/src/GUI.java new file mode 100644 index 0000000..8f1173d --- /dev/null +++ b/src/GUI.java @@ -0,0 +1,332 @@ +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.geom.Line2D; +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import javax.swing.ButtonGroup; +import javax.swing.ImageIcon; +import javax.swing.JButton; +import javax.swing.JFileChooser; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JRadioButton; +import javax.swing.border.EtchedBorder; +import javax.swing.filechooser.FileNameExtensionFilter; + + +public class GUI extends JFrame { + List listPoints = new ArrayList<>(); + String filePath; + JRadioButton radioButtonDrawBox; + JRadioButton radioButtonHBRectangle; + JRadioButton radioButtonHBCircle; + GUI(){ + //фрейм + setTitle("DrawBox points");//название + setLayout(null);//отключаем автоматическое расположение + setSize(800,600);//размер фрейма + setLocationRelativeTo(null);//окно по центру + setResizable(false); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + //создание панели для кнопок + JPanel buttonCase = new JPanel(); + //buttonCase.setBorder(new LineBorder(Color.black)); + buttonCase.setLayout(null); + buttonCase.setSize(200, 600);//позже задать значения + + + 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); + + + JLabel labelPicture = new JLabel(); + labelPicture.setLayout(null); + labelPicture.setHorizontalAlignment(JLabel.CENTER); + labelPicture.setVerticalAlignment(JLabel.CENTER); + labelPicture.setLocation(202, 2); + labelPicture.setBorder(new EtchedBorder(EtchedBorder.RAISED)); + labelPicture.setSize(579,557); + + + PaintJLabel labelDrawing = new PaintJLabel(listPoints); + labelDrawing.addMouseMotionListener(new MouseAdapter() { + @Override + public void mouseMoved(MouseEvent e) { + if(radioButtonDrawBox.isSelected()) { + if(labelDrawing.listPoints.size()<4) { + labelDrawing.x = e.getX(); + labelDrawing.y = e.getY(); + labelDrawing.repaint(); + } + + } + if(radioButtonHBRectangle.isSelected()||radioButtonHBCircle.isSelected()) { + if(labelDrawing.listPoints.size()<2) { + labelDrawing.x = e.getX(); + labelDrawing.y = e.getY(); + } + labelDrawing.repaint(); + } + + } + }); + labelDrawing.addMouseListener(new MouseAdapter() { + @Override + public void mousePressed(MouseEvent e) { + if(radioButtonDrawBox.isSelected()) { + if(labelDrawing.listPoints.size()<4) { + labelDrawing.listPoints.add(new PointLine(e.getX(), e.getY())); + if(labelDrawing.listPoints.size() > 1 && labelDrawing.listPoints.size() <=4) { + 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() ==4){ + 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)); + } + + } + } + + System.out.println("labelDrawing.listLines.size() = "+labelDrawing.listLines.size()); + + if(radioButtonHBRectangle.isSelected()) { + if(labelDrawing.listPoints.size()<3) { + 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))); + 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)); + labelDrawing.listLines.add(new Line2D.Float(listPoints.get(3).x,listPoints.get(3).y,listPoints.get(0).x,listPoints.get(0).y)); + } + } + + } + + + if(radioButtonHBCircle.isSelected()) { + labelDrawing.listPoints.add(new PointLine(e.getX(), e.getY())); + } + labelDrawing.repaint(); + System.out.println("Size = "+labelDrawing.listPoints.size()); + } + }); + //Обьект выбора путей + JFileChooser fileOpen = new JFileChooser("F:\\The-Story-script\\story-dev\\res\\test"); + FileNameExtensionFilter extFilter = new FileNameExtensionFilter("JPEG,PNG file", "jpg", "jpeg","png"); + FileNameExtensionFilter FilterXML = new FileNameExtensionFilter("XML file", "xml"); + fileOpen.setAcceptAllFileFilterUsed(false); + fileOpen.addChoosableFileFilter(extFilter); + fileOpen.addChoosableFileFilter(FilterXML); + + //создание слушателей + CustomButtonListenerOpenPicture openPicture = new CustomButtonListenerOpenPicture(labelPicture,fileOpen,labelDrawing); + CustomButtonListenerXmlFilePath openXmlFile = new CustomButtonListenerXmlFilePath(fileOpen); + CustomButtonListenerClearLines ClicklabelDraw = new CustomButtonListenerClearLines(labelDrawing); +// создание кнопок + 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); + + + add(buttonCase); + add(labelDrawing); + add(labelPicture); + + } + + private JRadioButton createRadioButton(String text,JPanel buttonCase,ButtonGroup groupRadioButton,int width,int height) { + JRadioButton radioButton = new JRadioButton(text); + radioButton.setBounds(width, height,200,30); + groupRadioButton.add(radioButton); + buttonCase.add(radioButton); + return radioButton; + } + + 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.setLocation(width, height); + button.addActionListener(listener); + buttonCase.add(button); + return button; + } + + public class PaintJLabel extends JLabel{ + List listPoints; + List listLines = new ArrayList<>(); + boolean clear = false; + int width,heigth; + float x,y; + PaintJLabel(List listP){ + listPoints = listP; + } + public void paint(Graphics g) { + Graphics2D graphic2d = (Graphics2D) g; + if(radioButtonDrawBox.isSelected()) { + //это условие для линии которая двигается за мышкой + if(listPoints.size() >= 1) { + PointLine a = listPoints.get(listPoints.size()-1); + graphic2d.setColor(Color.BLUE); + graphic2d.draw(new Line2D.Float(a.x,a.y,x,y)); + } + //отрисовка всех линий + if(listLines.size() >= 1) { + for(int i = 0;i= 1&&listPoints.size()<3) { + PointLine a = listPoints.get(listPoints.size()-1); + graphic2d.setColor(Color.BLUE); + graphic2d.draw(new Line2D.Float(a.x,a.y,x,y)); + } + if(listPoints.size()==4) { + for(int i = 0;ilistPoints.get(j).y) { + Collections.swap(listPoints, i, j); + } + } + } + if(listPoints.get(0).x>listPoints.get(1).x) { + Collections.swap(listPoints, 0, 1); + } + if(listPoints.get(2).x list,String file){ + filePath = file; + xmlFile = new File(filePath); + dbFactory = DocumentBuilderFactory.newInstance(); + name = mainName; + try { + dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(xmlFile); + doc.getDocumentElement().normalize(); + + NodeList objecttypeNodeList = doc.getElementsByTagName("objecttype"); + NodeList propertyList; + // now XML is loaded as Document in memory, lets convert it to Object List + for (int i = 0; i < objecttypeNodeList.getLength(); i++) { + Element currentObjecttypeNode = (Element) objecttypeNodeList.item(i); + if (currentObjecttypeNode.getAttribute("name").equalsIgnoreCase(name)) { + propertyList = currentObjecttypeNode.getElementsByTagName("property"); + Element elementProperty = null; + for (int j = 0; j < propertyList.getLength(); j++) { + elementProperty = (Element) propertyList.item(j); + if (elementProperty.getAttribute("name").equalsIgnoreCase("drawbox")) + currentObjecttypeNode.removeChild(elementProperty); + + } + Element newProperty = doc.createElement("property"); + Attr attr = doc.createAttribute("name"); + attr.setValue("drawbox"); + newProperty.setAttributeNode(attr); + + attr = doc.createAttribute("type"); + attr.setValue("string"); + newProperty.setAttributeNode(attr); + + System.out.println("currentObjecttypeNode" + currentObjecttypeNode.getTagName()); + 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)) + text += ' '; + } + System.out.println("cords: " + text); + attr = doc.createAttribute("default"); + attr.setValue(text); + newProperty.setAttributeNode(attr); + + currentObjecttypeNode.appendChild(newProperty); + + // currentObjecttypeNode.replaceChild(newProperty, elementProperty); + break; + } + } + + try { + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + DOMSource source = new DOMSource(doc); + StreamResult result = new StreamResult(new File(filePath)); + transformer.transform(source, result); + } catch (TransformerException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } catch (SAXException | ParserConfigurationException | IOException e1) { + e1.printStackTrace(); + } + } + + static List getChildNodes( Node node, String name ){ + ArrayList r = new ArrayList(); + NodeList children = node.getChildNodes(); + int l = children.getLength(); + for( int i = 0; i < l; ++i ){ + if( name.equals( children.item(i).getNodeName() ) ) + r.add( children.item(i) ); + } + return r; + } +}