Java ][]儿童; 字符串根=”; 布尔nodeSorted=false; int parentCounter=1; 公共无效treeSort(ArrayList passedQuestions){ //声明节点 arrayLength=passedQuestions.size(); System.out.println(数组长度+“数组长度”); Child=新字符串[arrayLength][3]; 对于(int i=0;i

Java ][]儿童; 字符串根=”; 布尔nodeSorted=false; int parentCounter=1; 公共无效treeSort(ArrayList passedQuestions){ //声明节点 arrayLength=passedQuestions.size(); System.out.println(数组长度+“数组长度”); Child=新字符串[arrayLength][3]; 对于(int i=0;i,java,swing,graphics,paintcomponent,graphics2d,Java,Swing,Graphics,Paintcomponent,Graphics2d,}世跆拳道联合会,一直致力于此 import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.RenderingHints; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; i

}世跆拳道联合会,一直致力于此

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class DragMyFields extends JPanel {
    private static final int PREF_W = 1000;
    private static final int PREF_H = 800;
    private static final int COLS = 8;
    private static final int DELTA_Y = 120;
    private static final int MAX_DEPTH = 3;
    private MySimpleTreeNode<JTextField> treeRoot = new MySimpleTreeNode<>();
    private MyMouse myMouse = new MyMouse();

    public DragMyFields() {
        setLayout(null);  // this is *** BAD ***
              // much better would be to create a custom layout

        JTextField field = new JTextField(COLS);
        field.addMouseListener(myMouse);
        field.addMouseMotionListener(myMouse);
        field.setSize(field.getPreferredSize());
        int x = (PREF_W - field.getPreferredSize().width) / 2;
        int y = DELTA_Y;
        field.setLocation(x, y);
        add(field);
        treeRoot.setNode(field);

        recursiveCreateTree(treeRoot, MAX_DEPTH, x, y);
    }

    private void recursiveCreateTree(MySimpleTreeNode<JTextField> node, int depth, int x, int y) {
        if (depth == 0) {
            return;
        }

        JTextField leftField = new JTextField(COLS);
        JTextField rightField = new JTextField(COLS);

        MySimpleTreeNode<JTextField> leftNode = new MySimpleTreeNode<>(leftField);
        MySimpleTreeNode<JTextField> rightNode = new MySimpleTreeNode<>(rightField);
        node.setLeft(leftNode);
        node.setRight(rightNode);

        int multiplier = 4;
        for (int i = 0; i < MAX_DEPTH - depth; i++) {
            multiplier *= 2;
        }

        int xL = x - getPreferredSize().width / multiplier;
        int xR = x + getPreferredSize().width / multiplier;
        y += DELTA_Y;

        leftField.setSize(leftField.getPreferredSize());
        rightField.setSize(rightField.getPreferredSize());
        leftField.setLocation(xL, y);
        rightField.setLocation(xR, y);
        leftField.addMouseListener(myMouse);
        leftField.addMouseMotionListener(myMouse);
        rightField.addMouseListener(myMouse);
        rightField.addMouseMotionListener(myMouse);
        add(leftField);
        add(rightField);

        recursiveCreateTree(leftNode, depth - 1, xL, y);
        recursiveCreateTree(rightNode, depth - 1, xR, y);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        recursiveDraw(g, treeRoot);
    }

    private void recursiveDraw(Graphics g, MySimpleTreeNode<JTextField> node) {
        MySimpleTreeNode<JTextField> left = node.getLeft();
        MySimpleTreeNode<JTextField> right = node.getRight();

        Point p = getNodeCenter(node);

        if (left != null) {
            Point p2 = getNodeCenter(left);
            g.drawLine(p.x, p.y, p2.x, p2.y);
            recursiveDraw(g, left);
        }
        if (right != null) {
            Point p2 = getNodeCenter(right);
            g.drawLine(p.x, p.y, p2.x, p2.y);
            recursiveDraw(g, right);
        }

    }

    private Point getNodeCenter(MySimpleTreeNode<JTextField> node) {
        JTextField field = node.getNode();
        Point location = field.getLocation();
        Dimension size = field.getSize();
        return new Point(location.x + size.width / 2, location.y + size.height / 2);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private class MyMouse extends MouseAdapter {
        Component source = null;
        private Point pressedP;
        private Point pressedLoc;
        private Point parentP;

        @Override
        public void mousePressed(MouseEvent e) {
            if (e.getButton() != MouseEvent.BUTTON1) {
                return;
            }

            source = e.getComponent();
            parentP = source.getParent().getLocationOnScreen();
            pressedLoc = source.getLocationOnScreen();
            pressedP = e.getLocationOnScreen();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            moveComponent(e);
            source = null;
            pressedP = null;
            pressedLoc = null;
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            if (source == null) {
                return;
            }
            moveComponent(e);
        }

        private void moveComponent(MouseEvent e) {
            Point p = e.getLocationOnScreen();
            int x = pressedLoc.x + p.x - pressedP.x - parentP.x;
            int y = pressedLoc.y + p.y - pressedP.y - parentP.y;

            Point newP = new Point(x, y);
            source.setLocation(newP);
            repaint();
        }
    }

    private static void createAndShowGui() {
        DragMyFields mainPanel = new DragMyFields();

        JFrame frame = new JFrame("DragMyFields");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGui();
        });
    }
}

class MySimpleTreeNode<T> {
    private T node;
    private MySimpleTreeNode<T> left;
    private MySimpleTreeNode<T> right;

    public MySimpleTreeNode() {
        // default constructor
    }

    public MySimpleTreeNode(T node) {
        this.node = node;
    }

    public void setNode(T node) {
        this.node = node;
    }

    public T getNode() {
        return node;
    }

    public MySimpleTreeNode<T> getLeft() {
        return left;
    }

    public void setLeft(MySimpleTreeNode<T> left) {
        this.left = left;
    }

    public MySimpleTreeNode<T> getRight() {
        return right;
    }

    public void setRight(MySimpleTreeNode<T> right) {
        this.right = right;
    }

}
导入java.awt.Component;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.Point;
导入java.awt.RenderingHints;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入javax.swing.*;
@抑制警告(“串行”)
公共类DragMyFields扩展了JPanel{
专用静态最终整型预加值W=1000;
专用静态最终整型参数参数H=800;
专用静态最终int COLS=8;
专用静态最终整数增量Y=120;
专用静态最终int最大深度=3;
private MySimpleTreeNode treeRoot=new MySimpleTreeNode();
private MyMouse MyMouse=新建MyMouse();
公共DragMyFields(){
setLayout(null);//这是***错误***
//最好是创建一个自定义布局
JTextField=新的JTextField(COLS);
field.addMouseListener(myMouse);
addMouseMotionListener(myMouse);
field.setSize(field.getPreferredSize());
intx=(PREF_W-field.getPreferredSize().width)/2;
int y=δy;
字段设置位置(x,y);
添加(字段);
treeRoot.setNode(字段);
递归CreateTree(treeRoot,最大深度,x,y);
}
私有void recursiveCreateTree(MySimpleTreeNode节点,int深度,int x,int y){
如果(深度==0){
返回;
}
JTextField leftField=新的JTextField(COLS);
JTextField rightField=新的JTextField(COLS);
MySimpleTreeNode leftNode=新的MySimpleTreeNode(leftField);
MySimpleTreeNode rightNode=新的MySimpleTreeNode(rightField);
node.setLeft(leftNode);
node.setRight(rightNode);
整数乘数=4;
对于(int i=0;ipublic static int arrayLength;
String[][] Child;
String root = "";
boolean nodeSorted = false;
int parentCounter = 1;

public void treeSort(ArrayList<String> passedQuestions) {
    // declaring nodes
    arrayLength = passedQuestions.size();
    System.out.println(arrayLength + "ARRAY LENGTH");
    Child = new String[arrayLength][3];
    for (int i = 0; i < arrayLength; i++) {
        Child[i][0] = passedQuestions.get(i);
    }
    //initially calling the mainprocess with parentCounter 1;
    root = Child[0][0];
    mainProcess(1);

}

public void mainProcess(int parentCounter) {

    if (parentCounter < Child.length) {
        System.out.println(parentCounter);
        sortingTree(Child[parentCounter][0], root, 0);  //where the next node is passed on the tree is sorted recursively
        for (int i = 0; i < Child.length; i++) {
            System.out.println(Child[i][0]);
            System.out.println(Child[i][1] + "," + Child[i][2]);
        }

    }
}

public void sortingTree(String CurrentNode, String PreviousNode, int PreviousPosition) {
    nodeSorted = false;// node is not sorted in the beginning
    if (isAfter(CurrentNode.toLowerCase(), PreviousNode.toLowerCase())) {
        System.out.println(Child[PreviousPosition][2]);
        if (Child[PreviousPosition][2] == null) { //checks if the right of the node is empty, if found empty the node is placed there.
            Child[PreviousPosition][2] = CurrentNode;
            nodeSorted = true; // if the node finds a position in the array node is sorted.
        } else {
            sortingTree(CurrentNode, Child[PreviousPosition][2], getPositionInArray(Child[PreviousPosition][2]));
            //if the array position was not empty, the loop will process again this time with the item found in the filled position.
        }
    } else if (Child[PreviousPosition][1] == null) { // if the left of the node is empty, the item will be placed there
        Child[PreviousPosition][1] = CurrentNode;
        nodeSorted = true;
    } else {
        sortingTree(CurrentNode, Child[PreviousPosition][1], getPositionInArray(Child[PreviousPosition][1]));
        //if the array position was not empty, the loop will process again this time with the item found in the filled position.
    }

    if (nodeSorted) { // if the node finds a position in the array, the nodeCounter increments and the next node in the question is processed.
        parentCounter++;
        mainProcess(parentCounter);
    }
}

public int getPositionInArray(String node) {
    int position = 0;
    loop:
    for (int i = 0; i < Child.length; i++) {
        if (Child[i][0].equals(node)) {
            position = i;
            break loop;

        }
    }
    return position;
}

public boolean isAfter(String CurrentNode, String PreviousNode) {
    int loopLength = determineLoopLength(CurrentNode, PreviousNode);
    boolean result = false;
    String tempCheck = "";
    loop:
    for (int i = 0; i < loopLength; i++) {
        if ((int) CurrentNode.charAt(i) > (int) PreviousNode.charAt(i)) {
            result = true;
            break loop;
        }
        if ((int) CurrentNode.charAt(i) < (int) PreviousNode.charAt(i)) {
            result = false;
            break loop;
        } else if (CurrentNode.charAt(i) == PreviousNode.charAt(i) && CurrentNode.length() > PreviousNode.length()) {
            System.out.println("I'm here");
            tempCheck = tempCheck + CurrentNode.charAt(i) + "";
            if (i == loopLength - 1 && tempCheck.equals(PreviousNode)) {
                result = true;
                break loop;
            }
        }
    }
    return result;
}

public int determineLoopLength(String CurrentNode, String PreviousNode) {
    int loopLength = 0;
    if (CurrentNode.length() < PreviousNode.length()) {
        loopLength = CurrentNode.length();
    }
    if (PreviousNode.length() < CurrentNode.length()) {
        loopLength = PreviousNode.length();
    } else {
        loopLength = CurrentNode.length();
    }
    return loopLength;
}
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class DragMyFields extends JPanel {
    private static final int PREF_W = 1000;
    private static final int PREF_H = 800;
    private static final int COLS = 8;
    private static final int DELTA_Y = 120;
    private static final int MAX_DEPTH = 3;
    private MySimpleTreeNode<JTextField> treeRoot = new MySimpleTreeNode<>();
    private MyMouse myMouse = new MyMouse();

    public DragMyFields() {
        setLayout(null);  // this is *** BAD ***
              // much better would be to create a custom layout

        JTextField field = new JTextField(COLS);
        field.addMouseListener(myMouse);
        field.addMouseMotionListener(myMouse);
        field.setSize(field.getPreferredSize());
        int x = (PREF_W - field.getPreferredSize().width) / 2;
        int y = DELTA_Y;
        field.setLocation(x, y);
        add(field);
        treeRoot.setNode(field);

        recursiveCreateTree(treeRoot, MAX_DEPTH, x, y);
    }

    private void recursiveCreateTree(MySimpleTreeNode<JTextField> node, int depth, int x, int y) {
        if (depth == 0) {
            return;
        }

        JTextField leftField = new JTextField(COLS);
        JTextField rightField = new JTextField(COLS);

        MySimpleTreeNode<JTextField> leftNode = new MySimpleTreeNode<>(leftField);
        MySimpleTreeNode<JTextField> rightNode = new MySimpleTreeNode<>(rightField);
        node.setLeft(leftNode);
        node.setRight(rightNode);

        int multiplier = 4;
        for (int i = 0; i < MAX_DEPTH - depth; i++) {
            multiplier *= 2;
        }

        int xL = x - getPreferredSize().width / multiplier;
        int xR = x + getPreferredSize().width / multiplier;
        y += DELTA_Y;

        leftField.setSize(leftField.getPreferredSize());
        rightField.setSize(rightField.getPreferredSize());
        leftField.setLocation(xL, y);
        rightField.setLocation(xR, y);
        leftField.addMouseListener(myMouse);
        leftField.addMouseMotionListener(myMouse);
        rightField.addMouseListener(myMouse);
        rightField.addMouseMotionListener(myMouse);
        add(leftField);
        add(rightField);

        recursiveCreateTree(leftNode, depth - 1, xL, y);
        recursiveCreateTree(rightNode, depth - 1, xR, y);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        recursiveDraw(g, treeRoot);
    }

    private void recursiveDraw(Graphics g, MySimpleTreeNode<JTextField> node) {
        MySimpleTreeNode<JTextField> left = node.getLeft();
        MySimpleTreeNode<JTextField> right = node.getRight();

        Point p = getNodeCenter(node);

        if (left != null) {
            Point p2 = getNodeCenter(left);
            g.drawLine(p.x, p.y, p2.x, p2.y);
            recursiveDraw(g, left);
        }
        if (right != null) {
            Point p2 = getNodeCenter(right);
            g.drawLine(p.x, p.y, p2.x, p2.y);
            recursiveDraw(g, right);
        }

    }

    private Point getNodeCenter(MySimpleTreeNode<JTextField> node) {
        JTextField field = node.getNode();
        Point location = field.getLocation();
        Dimension size = field.getSize();
        return new Point(location.x + size.width / 2, location.y + size.height / 2);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private class MyMouse extends MouseAdapter {
        Component source = null;
        private Point pressedP;
        private Point pressedLoc;
        private Point parentP;

        @Override
        public void mousePressed(MouseEvent e) {
            if (e.getButton() != MouseEvent.BUTTON1) {
                return;
            }

            source = e.getComponent();
            parentP = source.getParent().getLocationOnScreen();
            pressedLoc = source.getLocationOnScreen();
            pressedP = e.getLocationOnScreen();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            moveComponent(e);
            source = null;
            pressedP = null;
            pressedLoc = null;
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            if (source == null) {
                return;
            }
            moveComponent(e);
        }

        private void moveComponent(MouseEvent e) {
            Point p = e.getLocationOnScreen();
            int x = pressedLoc.x + p.x - pressedP.x - parentP.x;
            int y = pressedLoc.y + p.y - pressedP.y - parentP.y;

            Point newP = new Point(x, y);
            source.setLocation(newP);
            repaint();
        }
    }

    private static void createAndShowGui() {
        DragMyFields mainPanel = new DragMyFields();

        JFrame frame = new JFrame("DragMyFields");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGui();
        });
    }
}

class MySimpleTreeNode<T> {
    private T node;
    private MySimpleTreeNode<T> left;
    private MySimpleTreeNode<T> right;

    public MySimpleTreeNode() {
        // default constructor
    }

    public MySimpleTreeNode(T node) {
        this.node = node;
    }

    public void setNode(T node) {
        this.node = node;
    }

    public T getNode() {
        return node;
    }

    public MySimpleTreeNode<T> getLeft() {
        return left;
    }

    public void setLeft(MySimpleTreeNode<T> left) {
        this.left = left;
    }

    public MySimpleTreeNode<T> getRight() {
        return right;
    }

    public void setRight(MySimpleTreeNode<T> right) {
        this.right = right;
    }

}