在java屏幕上显示鼠标坐标的问题 [英] problem displaying mouse coordinates on the screen in java
本文介绍了在java屏幕上显示鼠标坐标的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在我的JPanel中显示鼠标坐标(数学坐标),但是我将每个坐标放在另一个坐标上,可以找出原因。
这里是我的代码:
import java.awt。*;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.applet。*;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;
public class drawarea extends JPanel {
int n;
private point mouseCoords = null;
int UNIT = 20;
drawarea(){
super();
setBackground(Color.white);
addMouseMotionListener(new MouseInputAdapter(){
public void mouseMoved(MouseEvent e){
super.mouseMoved(e);
mouseCoords = new Point(e.getX (),e.getY());
repaint();
}
/ **
* @see java.awt.event.MouseListener#mouseExited (MouseEvent)
* /
public void mouseExited(MouseEvent e){
super.mouseExited(e);
mouseCoords = null;
repaint();
}
});
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
public void paint(Graphics g){
// draw axis x and y
g.setColor(Color.red);
g.drawLine(0,r.height / 2,r.width,r.height / 2);
g.drawLine(r.width / 2,0,r.width / 2,r.height);
Graphics2D g2 =(Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
dessinBoule(g2);
$ b $ private void dessinBoule(Graphics2D g){
//如果鼠标不在ara里面
//我想要坐标显示
if(mouseCoords == null){
return;
}
g.setColor(Color.BLACK);
int decPolice = 15;
g.drawString(x =+ getFormatedString(+ mouseCoords.x)
+,y =+ getFormatedString(+ mouseCoords.y),2,15 );
$ b $ private String getFormatedString(String s){
if(s.length()> 4){
return s.substring(0,3) ;
}
return s;
}
}
谢谢。
<您正在绘制图形区域,所以g.drawString(...)会在已经存在的地方绘制一个字符串。您必须首先清除其中的内容,通过以背景颜色绘制矩形,或者使用可以使用单独的paint(...)方法管理的单独组件。
I'm trying to display mouse coordinates (math coordinates) in my JPanel , but i get each coordinates on top of the other ,can' figure out why . here's my code :
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.applet.*;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;
public class drawarea extends JPanel {
int n;
private Point mouseCoords = null;
int UNIT = 20;
drawarea() {
super();
setBackground(Color.white);
addMouseMotionListener(new MouseInputAdapter() {
public void mouseMoved(MouseEvent e) {
super.mouseMoved(e);
mouseCoords = new Point(e.getX(), e.getY());
repaint();
}
/**
* @see java.awt.event.MouseListener#mouseExited(MouseEvent)
*/
public void mouseExited(MouseEvent e) {
super.mouseExited(e);
mouseCoords = null;
repaint();
}
});
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
public void paint(Graphics g) {
//draw axis x and y
g.setColor(Color.red);
g.drawLine(0, r.height / 2, r.width, r.height / 2);
g.drawLine(r.width / 2, 0, r.width / 2, r.height);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
dessinBoule(g2);
}
private void dessinBoule(Graphics2D g) {
// if mouse isnt inside the ara where
// i want the coordinates to bes displayed
if (mouseCoords == null) {
return;
}
g.setColor(Color.BLACK);
int decPolice = 15;
g.drawString("x = " + getFormatedString("" + mouseCoords.x)
+ " , y = " + getFormatedString("" + mouseCoords.y), 2, 15);
}
private String getFormatedString(String s) {
if (s.length() > 4) {
return s.substring(0, 3);
}
return s;
}
}
thanks.
解决方案
You're drawing on your graphics area, so g.drawString(...) draws a string on top of what is already there. You must either erase what is there first, by drawing a rectangle in the background colour, or use a separate component that you can manage with a separate paint(...) method.
这篇关于在java屏幕上显示鼠标坐标的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文