gpt4 book ai didi

java - 当鼠标移动而不是拖动时,如何在 Canvas 上移动二维图形

转载 作者:行者123 更新时间:2023-12-02 06:18:23 25 4
gpt4 key购买 nike

请我正在解决一个在java中绘制二维图形的问题,但我希望当鼠标移动而不是拖动时移动图形。你知道,当鼠标在 Canvas 上移动时,图形也会移动也。我该怎么做?

  import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;

public class Pointer2QuadCurve extends javax.swing.JFrame {

private javax.swing.JPanel jPanel1;
double x1;
double y1;

public Pointer2QuadCurve() {
initComponents();
jPanel1.addMouseListener(new MouseListener() {

@Override
public void mouseClicked(MouseEvent e) {

}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {
int[] pixels = new int[16 * 16];
Image image = Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(16, 16, pixels, 0, 16));
Cursor transparentCursor
= Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0, 0), "invisibleCursor");
//paintWithPointer(getGraphics());
setCursor(transparentCursor);
}

@Override
public void mouseExited(MouseEvent e) {

}
});
jPanel1.addMouseMotionListener(new MouseMotionListener() {

@Override
public void mouseDragged(MouseEvent e) {

}

@Override
public void mouseMoved(MouseEvent e) {
//Point p = getMousePosition();
x1 = e.getX();
y1 = e.getY();
jPanel1.repaint();
}
});
}



private void initComponents() {

jPanel1 = new javax.swing.JPanel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
}


public static void main(String args[]) {

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 ex) {
java.util.logging.Logger.getLogger(Pointer2QuadCurve.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Pointer2QuadCurve.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Pointer2QuadCurve.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Pointer2QuadCurve.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Pointer2QuadCurve().setVisible(true);
}
});
}


public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
// draw Rectangle2D.Double
g2d.draw(new Rectangle2D.Double(x1, y1,
Math.random() + 10,
Math.random() + 15));
}

}

最佳答案

  • MouseMotionListener 添加到自定义绘制组件。
  • 监听mouseMoved事件。
  • MouseEvent获取Point
  • 处绘制形状。

关于java - 当鼠标移动而不是拖动时,如何在 Canvas 上移动二维图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21249252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com