weixin_38816126
2021-12-17 17:23
采纳率: 54.5%
浏览 331

JAVA怎么裁剪拖拽到画布上的图片

我做了一个GUI, 会在下面放图。左上的panel可以用来生成已有数据的图像,右边的panel生成一些pattern,左下角的panel是画布。

)

简单来说,我可以把右边的pattern拖拽到左下的画布上来,然后在画图的其他位置,我可以自己接着画图。

img


【这是拖拽的过程】

img


【拖拽到画布】

img


【自己继续往上面画】

现在我希望可以做到:对于我拖拽下来的pattern,我可以在画布上进行裁剪,把选中的部分cut掉,留下我认为有用的部分。但是这里不知道应该怎么做。

因为这个界面本身也是一个学姐做完了留下来的,我只是在上面进行一些简单的修改,但是对于绘图和拖拽这部分实在是不知道怎么做。麻烦大家帮帮忙提供一下思路。

我附上一部分代码。

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) {

        }

    }

8条回答 默认 最新

相关推荐 更多相似问题