bugs fixed, drawboxes now working properly

This commit is contained in:
Maggistrator
2021-07-20 21:33:23 +03:00
commit dc54ff2ce7
7 changed files with 536 additions and 0 deletions

10
.classpath Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-15">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/bin/
.settings/org.eclipse.core.resources.prefs

17
.project Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Creating-drawbox-points</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -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

47
src/ClassicMainClass.java Normal file
View File

@ -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();
}
}
}

332
src/GUI.java Normal file
View File

@ -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<PointLine> 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<PointLine> listPoints;
List<Line2D> listLines = new ArrayList<>();
boolean clear = false;
int width,heigth;
float x,y;
PaintJLabel(List<PointLine> 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<listLines.size();i++) {
graphic2d.draw(listLines.get(i));
}
if(listLines.size() == 5) {
graphic2d.setColor(Color.MAGENTA);
graphic2d.draw(listLines.get(4));
}
}
}
if(radioButtonHBRectangle.isSelected()) {
graphic2d.setColor(Color.BLUE);
if(listPoints.size() >= 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;i<listLines.size();i++) {
graphic2d.draw(listLines.get(i));
}
}
}
graphic2d.setColor(Color.BLACK);
graphic2d.drawRect(0, 0, width-1, heigth-1);
}
void print() {
for(int i=0;i<this.listPoints.size();i++) {
System.out.println("PointX["+i+"]"+listPoints.get(i).x+"PointY["+i+"]"+listPoints.get(i).y);
}
}
public void sortPoint() {
for(int i = 0;i<3;i++) {
for(int j = i+1;j<4;j++) {
if(listPoints.get(i).y>listPoints.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<listPoints.get(3).x) {
Collections.swap(listPoints, 2, 3);
}
print();
}
}
class CustomButtonListenerOpenPicture implements ActionListener {
private JLabel label;
ImageIcon icon;
JFileChooser fileChooser;
PaintJLabel labelDraw;
CustomButtonListenerOpenPicture(JLabel mainLabel,JFileChooser file,PaintJLabel mainPaintJlabel){
label = mainLabel;
fileChooser = file;
labelDraw = mainPaintJlabel;
}
@Override
public void actionPerformed(ActionEvent e) {
/*���� ��� ������*/
int ret = fileChooser.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
icon = new ImageIcon(file.getPath());
label.setIcon(icon);
}
if(icon!=null) {
int widthIcon = icon.getIconWidth();
int heigthIcon = icon.getIconHeight();
labelDraw.setLocation(((579-widthIcon)/2+202), (557-heigthIcon)/2+2);
labelDraw.setBorder(new EtchedBorder(EtchedBorder.RAISED));
labelDraw.setSize(widthIcon,heigthIcon);
labelDraw.width = widthIcon;
labelDraw.heigth = heigthIcon;
}
}
}
class CustomButtonListenerXmlFilePath implements ActionListener {
JFileChooser fileChooser;
CustomButtonListenerXmlFilePath(JFileChooser file){
fileChooser = file;
}
@Override
public void actionPerformed(ActionEvent e) {
/*���� ��� ������ ������*/
int ret = fileChooser.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
filePath = file.getPath();
}
}
}
class CustomButtonListenerSave implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
/*���� ��� ������ ������*/
String objectName = JOptionPane.showInputDialog(null, "������� ��� �������");
new Load_XML_File(objectName,listPoints,filePath);
}
}
class CustomButtonListenerClearLines implements ActionListener {
private PaintJLabel label;
CustomButtonListenerClearLines(PaintJLabel drawLabel){
label = drawLabel;
}
@Override
public void actionPerformed(ActionEvent e) {
/*���� ��� ������ ������*/
label.listLines.clear();
label.listPoints.clear();
repaint();
}
}
}
class PointLine {
float x;
float y;
PointLine(float mainX,float mainY){
x = mainX;
y = mainY;
}
}

114
src/Load_XML_File.java Normal file
View File

@ -0,0 +1,114 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Load_XML_File {
String name;
String filePath;
File xmlFile;
DocumentBuilder dBuilder;
DocumentBuilderFactory dbFactory;
Load_XML_File(String mainName,List<PointLine> 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<Node> getChildNodes( Node node, String name ){
ArrayList<Node> r = new ArrayList<Node>();
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;
}
}