
package comwztest1;
import javaawt;
import javaawtevent;
import javaxswing;
public class Painter extends JFrame {
private int pointCount = 0;
// array of 1000 javaawtPoint references
private Point points[] = new Point[ 1000 ];
// set up GUI and register mouse event handler
public Painter()
{
super( "A simple paint program" );
// create a label and place it in SOUTH of BorderLayout
getContentPane()add( new JLabel( "Drag the mouse to draw" ),
BorderLayoutSOUTH );
addMouseMotionListener(
new MouseMotionAdapter() { // anonymous inner class
// store drag coordinates and repaint
public void mouseDragged( MouseEvent event )
{
if ( pointCount < pointslength ) {
points[ pointCount ] = eventgetPoint();
++pointCount;
repaint();
}
}
} // end anonymous inner class
); // end call to addMouseMotionListener
setSize( 300, 150 );
setVisible( true );
} // end Painter constructor
// draw oval in a 4-by-4 bounding box at specified location on window
public void paint( Graphics g )
{
superpaint( g ); // clears drawing area
for ( int i = 0; i < pointslength && points[ i ] != null; i++ )
gfillOval( points[ i ]x, points[ i ]y, 4, 4 );
}
public static void main( String args[] )
{
Painter application = new Painter();
applicationsetDefaultCloseOperation( JFrameEXIT_ON_CLOSE );
}
} // end class Painter
import javaawt;
import javaawtevent;
public class Test {
public static void main(String args[]) {
new myFrame();
}
}
class myFrame extends Frame {
TextField tf1,tf2;
public myFrame(){
tf1 = new TextField(5);
tf2 = new TextField(5);
add(tf1);
add(tf2);
setLayout(new FlowLayout());
setBounds(100,100,400,300);
addMouseMotionListener(new MouseMotionAdapter(){//鼠标移动事件的侦听器
public void mouseMoved(MouseEvent e) {//鼠标按键在组件上移动(无按键按下)时调用。
tf1setText(egetX()+"" );
tf2setText(egetY()+"" );
}
});
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
Systemexit(0);
}
});
setVisible(true);
}
}
写过一个类似的,我当时的思路是这样的
动物棋子,应该设计成为一个类 ,并且有自定义的属性(,位置信息)啊,位置可以是一个Point(x,y)或者两个数字i, j ,代表是在二维地图数组中的位置 当移动棋时,修改属性为新的位置 并在新位置绘制
public class Chess extends JButton{//其他属性忽略,主要属性如下
int value;//棋子类型, 根据类型可以显示对应的
int i;//横坐标 也可以封装成一个Point类,类里有x,y坐标
int j;//纵坐标
public Chess(int value, int i, int j) {
thisvalue = value;
thisi = i;
thisj = j;
}
那些东西都是系统给实现的,不用你去管。比方说光标的移动,文本的删除,粘贴复制之类的东西已经实现好了,你直接用就是了~~
鼠标位置在右下角的状态栏里面有!~~
直接按del键就删了啊!~~
那些东西都是系统给实现的,不用你去管。比方说光标的移动,文本的删除,粘贴复制之类的东西已经实现好了,你直接用就是了~~
代码如下:
import javaawtDimension;
import javaawtMouseInfo;
import javaawtPoint;
import javaawtRobot;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaawteventMouseEvent;
import javaawteventMouseMotionListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingWindowConstants;
public class MouseHelp extends javaxswingJPanel implements MouseMotionListener {
private JButton textButton;
Robot robot;
/
Auto-generated main method to display this
JPanel inside a new JFrame
/
public static void main(String[] args) {
JFrame frame = new JFrame();
framegetContentPane()add(new MouseHelp());
framesetDefaultCloseOperation(WindowConstantsDISPOSE_ON_CLOSE);
framepack();
framesetVisible(true);
}
public MouseHelp() {
super();
initGUI();
}
private void initGUI() {
try {
robot=new Robot();
addMouseMotionListener(this);
setPreferredSize(new Dimension(400, 300));
thissetLayout(null);
{
textButton = new JButton();
thisadd(textButton);
textButtonsetText("\u8fd0 \u884c");
textButtonsetBounds(136, 72, 127, 22);
textButtonaddActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
robotmouseMove(30,745);
try {
Threadsleep(1000);
robotmousePress(MouseEventBUTTON1_MASK);
robotmouseRelease(MouseEventBUTTON1_MASK);
Threadsleep(1000);
robotmouseMove(150,481);
robotmousePress(MouseEventBUTTON1_MASK);
robotmouseRelease(MouseEventBUTTON1_MASK);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1printStackTrace();
}
}});
}
} catch (Exception e) {
eprintStackTrace();
}
}
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
//从这里获取鼠标的全局坐标
Point mousepoint = MouseInfogetPointerInfo()getLocation();
Systemoutprintln(mousepointx+"\t"+mousepointy);
}
}
以上就是关于求用ECLIPSE java编一个鼠标获取坐标代码!全部的内容,包括:求用ECLIPSE java编一个鼠标获取坐标代码!、Java怎么作一个记录鼠标坐标的程序、JAVA游戏鼠标监听获取坐标等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)