JAVA怎么裁剪拖拽到画布上的图片
课程设计
1
我做了一个GUI, 会在下面放图。左上的panel可以用来生成已有数据的图像,右边的panel生成一些pattern,左下角的panel是画布。 )
简单来说,我可以把右边的pattern拖拽到左下的画布上来,然后在画图的其他位置,我可以自己接着画图。
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) {
}
}
-
首先解决这个问题,你需要对想控制的物体添加鼠标事件。 鼠标事件中有鼠标按下事件和鼠标放开事件以及鼠标点击事件。 那么你需要做的是选择没用的片段进行删除。 那么你可以由两个静态变量来记录两次点击。第一次点击的时候如果两个变量都是空或者一个自定义值, 那么就记录在第一个变量上,第二次记录第二个变量。 两次之后,你就拿到了需要删除片段的一些数据。 你的图像应该保存有相关数据。如果没有,那么你需要对图片进行分段,将图片数据化。 使用获得的数据,在图片相应部分进行操作获得新的图片数据。 使用新的数据生成新图片!然后进行替换。 过程中最重要的部分为:图片转换为数据,数据转换为图片,获取操作信息数据。
-
你的问题应该是,画布内容拼接。我只提供思路,不提供代码 1.你在被拖拽的画布记录画布里面的内容。 2.定位到你要拖的画布上的位置,此时要进行内容的位置同步叠加。 3.在新画布上重绘画。
发表回复