current position:Home>How does Java cut and drag pictures onto the canvas

How does Java cut and drag pictures onto the canvas

2022-02-03 01:30:14 CSDN Q & A

I made one GUI, I'll put the picture below . Top left panel Can be used to generate images of existing data , Dexter panel Generate some pattern, Lower left corner panel It's the canvas .

)

Simply speaking , I can put the one on the right pattern Drag onto the lower left canvas , Then in other places of the drawing , I can continue drawing by myself .

img


【 This is a drag and drop process 】

img


【 Drag to canvas 】

img


【 Continue to draw on it by yourself 】

Now I hope I can do : For what I dragged down pattern, I can cut on the canvas , Put the selected part cut fall , Leave what I think is useful . But I don't know what to do here .

Because the interface itself is also left by a schoolgirl after she finishes , I just made some simple changes on it , But I really don't know how to do this part of drawing and dragging . Please help us to provide ideas .

I attach a part of the code .

JPanel leftDownPanel = new JPanel();        leftDownPanel.setBackground(Color.WHITE);        leftDownPanel.setLayout(null);        leftDownPanel.setBounds(10, 330, 720, 230);        frame.getContentPane().add(leftDownPanel);        frame.getContentPane().setBackground(new Color(252, 252, 252));        // add left down, up panel        JPanel leftDownUpPanel = new JPanel();        leftDownUpPanel.setBackground(Color.WHITE);        leftDownUpPanel.setLayout(null);        leftDownUpPanel.setBounds(10, 0, 720, 30);        leftDownPanel.add(leftDownUpPanel);        JLabel drawLabel = new JLabel("Sketch");        drawLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));        drawLabel.setBounds(10, 2, 120, 25);        leftDownUpPanel.add(drawLabel);        JButton clearButton = new JButton("Clear");        clearButton.setBounds(540, 2, 100, 25);        clearButton.addActionListener(e -> {            try {                drawPanel.remove(0);                leftDownChartPanel.repaint();            } catch (Exception e1) {                e1.printStackTrace();            }            drawPanel.repaint();            hasLeftDownChartPanel = false;            series = new String[0];            doubles.clear();            dragTimes = 0;            leftChartX = -1;        });        leftDownUpPanel.add(clearButton);   drawPanel = new JPanel();        drawPanel.setBounds(0, 28, 720, 200);        drawPanel.setBackground(Color.WHITE);        drawPanel.addMouseMotionListener(new MouseMotionListener() {            @Override            public void mouseMoved(MouseEvent e) {                x1 = e.getX();                y1 = e.getY();            }            @Override            public void mouseDragged(MouseEvent e) {                Graphics graphics = drawPanel.getGraphics();                graphics.setColor(Color.BLUE);                graphics.drawLine(x1, y1, e.getX(), e.getY());                x1 = e.getX();                y1 = e.getY();                doubles.add(1.5 - y1 * 0.01);                size++;            }        });        leftDownPanel.add(drawPanel);    }  boolean hasLeftDownChartPanel = false;    int dragTimes = 0;    public void setLeftChart(int x, String[] series) {        try {            if (leftChartX == -1) {                leftChartX = x;            }            System.out.println(x);            System.out.println("=======Set Left Chart=========");            dragTimes++;            leftDownChartPanel = new Charts2().createChart(series);            leftDownChartPanel.setMouseZoomable(false);            JFreeChart chart = leftDownChartPanel.getChart();            CategoryPlot categoryPlot = chart.getCategoryPlot();            categoryPlot.setOutlineVisible(false);            ValueAxis rangeAxis = categoryPlot.getRangeAxis();            CategoryAxis domainAxis = categoryPlot.getDomainAxis();            domainAxis.setVisible(false);            rangeAxis.setVisible(false);            leftDownChartPanel.setBackground(null);            leftDownChartPanel.addMouseListener(new java.awt.event.MouseListener() {                private int x;                private int y;                @Override                public void mouseClicked(MouseEvent e) {                }                @Override                public void mousePressed(MouseEvent e) {                    if (e.isControlDown()) {                        this.x = e.getX();                        this.y = e.getY();                    }                }                @Override                public void mouseReleased(MouseEvent e) {                    if (e.isControlDown()) {                        int xLen = e.getX() - x;                        int yLen = e.getY() - y;                        int startX = leftDownChartPanel.getX();                        int half = leftDownChartPanel.getWidth() / 2;                        // left start part,expand                        if (Math.abs(xLen) > Math.abs(yLen)) {                            if (x < startX + half && e.getX() < x) {                                leftChartX = Math.max(leftChartX + xLen, 0);                                leftDownChartPanel.setBounds(leftChartX, 0, Math.min(Math.max(leftDownChartPanel.getWidth() + Math.abs(xLen), 100), 700), 150);                                System.out.println(1);                            } else if (x > startX + half && e.getX() < x) {                                leftDownChartPanel.setBounds(leftChartX, 0, Math.min(Math.max(leftDownChartPanel.getWidth() - Math.abs(xLen), 100), 700), 150);                                System.out.println(2);                            } else if (x < startX + half && e.getX() > x) {                                leftChartX = Math.max(leftChartX + xLen, 0);                                leftDownChartPanel.setBounds(leftChartX, 0, Math.min(Math.max(leftDownChartPanel.getWidth() - Math.abs(xLen), 100), 700), 150);                                System.out.println(3);                            } else if (x > startX + half && e.getX() > x) { // minus                                leftDownChartPanel.setBounds(leftChartX, 0, Math.min(Math.max(leftDownChartPanel.getWidth() + Math.abs(xLen), 100), 700), 150);                                System.out.println(4);                            }                        } else {                            leftDownChartPanel.setBounds(leftChartX, 0, leftDownChartPanel.getWidth(), Math.min(Math.max(leftDownChartPanel.getHeight() - yLen, 100), 250));                        }                        leftDownChartPanel.repaint();                        System.out.println("====xLen: " + xLen + "=====" + ", ====leftChartX: " + leftChartX + "=======");                    }                }                @Override                public void mouseEntered(MouseEvent e) {                }                @Override                public void mouseExited(MouseEvent e) {                }            });            if (hasLeftDownChartPanel) {                drawPanel.remove(0);            }            leftDownChartPanel.setBounds(leftChartX, 20, Math.min(180 * dragTimes, 600), 150);            drawPanel.add(leftDownChartPanel, 0);            drawPanel.repaint();            hasLeftDownChartPanel = true;        } catch (Exception e) {            e.printStackTrace();        }    }    int index = -1;    boolean indexChange = false;    JFrame jFrame = new JFrame();    int time = -1;    MouseListener mouseListener = new MouseListener();    JLabel jLabel = new JLabel();    String[] series = new String[0];    private int leftChartX = -1;    class MouseListener implements MouseInputListener {        MouseListener() {            jFrame.setUndecorated(true);            jFrame.setBackground(new Color(0, 0, 0, 0));            jFrame.setLayout(null);            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        }        @Override        public void mouseReleased(MouseEvent e) {            int x = jFrame.getX();            int y = jFrame.getY();            int x1 = drawPanel.getX() + drawPanel.getParent().getX() + frame.getX();            int y1 = drawPanel.getY() + drawPanel.getParent().getY() + frame.getY();            if (x1 <= x && x <= x1 + 600 && y1 <= y && y <= y1 + 270) {                String[] series1 = datas2.get(index);                int length = series.length + series1.length;                String[] strings = new String[length];                System.arraycopy(series, 0, strings, 0, series.length);                if (length - series.length >= 0)                    System.arraycopy(series1, 0, strings, series.length, length - series.length);                series = strings;                setLeftChart(x - x1, series);            }            jFrame.setBounds(10, 10, 10, 10);            jFrame.setVisible(false);        }        @Override        public void mousePressed(MouseEvent e) {            if (index != e.getComponent().getY() / 180) {                index = e.getComponent().getY() / 180;                indexChange = true;            } else {                indexChange = false;            }        }        @Override        public void mouseExited(MouseEvent e) {            frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));        }        @Override        public void mouseEntered(MouseEvent e) {            frame.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));        }        @Override        public void mouseClicked(MouseEvent e) {            // TODO Auto-generated method stub        }        @Override        public void mouseDragged(MouseEvent e) {            time++;            if (time > 0 && indexChange) {                jFrame.remove(jLabel);                jLabel = new JLabel();            }            indexChange = false;            jLabel.setIcon(labels[index].getIcon());            jLabel.setBounds(0, 0, labels[index].getWidth(), labels[index].getHeight());            Point location = frame.getLocation();            jFrame.setBounds((int) location.getX() + 500 + e.getX(), (int) location.getY() + 50 + index * 180 + e.getY() + jPanel.getY(),                    labels[index].getWidth(), labels[index].getHeight());            jFrame.getContentPane().add(jLabel);            jFrame.repaint();            jFrame.setVisible(true);        }        @Override        public void mouseMoved(MouseEvent e) {        }    }



Refer to the answer 1:

Tell the truth , Or ask questions without money ;
Or high price questions , You ask , So little money
It's hard to ..




Refer to the answer 2:



Refer to the answer 3:

Provide an idea about dragging :
stay pannel Define an area and select objects through mouse events , Also divide an area in your goal , Then replace the target object . learn delphi There are differences , I can't help you any more .




Refer to the answer 4:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030130119330.html

Random recommended