使用平移校准坐标系后鼠标坐标停止工作-java
Mouse Coordinates stop working after using translation to recallibrate the coordination system-java
我正在开发一个坐标系,点 (0,0) 将位于面板的中心,但在转换默认 (0,0) 坐标后,鼠标坐标停止 displaying.Without 这一行代码 "ga.translate(175.0, 125.0)",程序运行正常。我该如何解决这个问题?谢谢。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
public class Main extends JPanel implements MouseMotionListener {
public JLabel label;
//@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D ga = (Graphics2D) g;
ga.translate(175.0, 125.0); //the mouse coordinates stop displaying with this code
ga.drawLine(0, 0, 100, 100);
}
public static void main(String[] args) {
Main m = new Main();
GraphicsDraw D = new GraphicsDraw();
JFrame frame = new JFrame();
frame.setTitle("MouseCoordinates111");
frame.setSize(400, 400);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
Container contentPane = frame.getContentPane();
contentPane.add(m);
frame.setVisible(true);
}
public Main() {
setSize(400, 400);
label = new JLabel("No Mouse Event Captured", JLabel.CENTER);
add(label);
//super();
addMouseMotionListener(this);
}
@Override
public void mouseMoved(MouseEvent e) {
//System.out.println(e.getX() + " / " + e.getY());
label.setText("Mouse Cursor Coordinates => X:" + e.getX() + " |Y:" + e.getY());
}
@Override
public void mouseDragged(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
}
}
当您操作 Graphics
对象的(某些)值(如翻译、转换)时,您应该始终将其恢复到其原始状态。
一个简单的方法是创建一个临时 Graphics
对象来处理您的绘画:
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
//Graphics2D ga = (Graphics2D) g;
Graphics2D ga = (Graphics2D)g.create();
ga.translate(175.0, 125.0); //the mouse coordinates stop displaying with this code
ga.drawLine(0, 0, 100, 100);
ga.dispose();
}
我正在开发一个坐标系,点 (0,0) 将位于面板的中心,但在转换默认 (0,0) 坐标后,鼠标坐标停止 displaying.Without 这一行代码 "ga.translate(175.0, 125.0)",程序运行正常。我该如何解决这个问题?谢谢。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
public class Main extends JPanel implements MouseMotionListener {
public JLabel label;
//@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D ga = (Graphics2D) g;
ga.translate(175.0, 125.0); //the mouse coordinates stop displaying with this code
ga.drawLine(0, 0, 100, 100);
}
public static void main(String[] args) {
Main m = new Main();
GraphicsDraw D = new GraphicsDraw();
JFrame frame = new JFrame();
frame.setTitle("MouseCoordinates111");
frame.setSize(400, 400);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
Container contentPane = frame.getContentPane();
contentPane.add(m);
frame.setVisible(true);
}
public Main() {
setSize(400, 400);
label = new JLabel("No Mouse Event Captured", JLabel.CENTER);
add(label);
//super();
addMouseMotionListener(this);
}
@Override
public void mouseMoved(MouseEvent e) {
//System.out.println(e.getX() + " / " + e.getY());
label.setText("Mouse Cursor Coordinates => X:" + e.getX() + " |Y:" + e.getY());
}
@Override
public void mouseDragged(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
}
}
当您操作 Graphics
对象的(某些)值(如翻译、转换)时,您应该始终将其恢复到其原始状态。
一个简单的方法是创建一个临时 Graphics
对象来处理您的绘画:
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
//Graphics2D ga = (Graphics2D) g;
Graphics2D ga = (Graphics2D)g.create();
ga.translate(175.0, 125.0); //the mouse coordinates stop displaying with this code
ga.drawLine(0, 0, 100, 100);
ga.dispose();
}