
//你运行一下,看在你的电脑上能不能用。
//把mouseMoved方法里的注释去掉重新获取
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);
}
}
//提示:坐标依次打印在命令符窗口
//提示:坐标依次打印在命令符窗口
//提示:坐标依次打印在命令符窗口
//不就是监听鼠标事件吗?
import javaxswing;
import javaawt;
import javaawtevent;
import javaxswingborderBorder;
/
我想建立个界面,可以加载本机中。
加载后可以通过鼠标点击获得上任意点坐标。
提问者: sunny929929 - 试用期 一级
/
public class MyPicture extends JFrame implements MouseListener{
private JLabel tipLabel;
/
main()
/
public static void main(String[] args){
MyPicture frame = new MyPicture();
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framesetVisible(true);
}
/
constructor
/
public MyPicture(){
setSize(800, 600);//根据要求调整大小
setLocation(100,100);
setTitle("获得上任意点坐标");
setResizable(false);
Container con=getContentPane();
ImageIcon bgIcon=new ImageIcon("bgpicjpg");//注意的路径
ImagePanel backpicPanel=new ImagePanel(bgIcon);
backpicPaneladdMouseListener(this);
conadd(backpicPanel,BorderLayoutCENTER);
tipLabel=new JLabel("--------------------提示:坐标依次打印在屏幕上!--------------------");
conadd(tipLabel,BorderLayoutSOUTH);
}
/
/
public void mousePressed(MouseEvent e){
int x=egetX();
int y=egetY();
String message="("+x+","+y+")";
tipLabelsetText(message);
Systemoutprintln(message);
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
}
}
/
类ImagePanel,用于添加背景
/
class ImagePanel extends JPanel{
private Image img;
public ImagePanel (ImageIcon imageIcon){
img=imageIcongetImage();
}
public void paintComponent(Graphics g){
superpaintComponent(g);
gdrawImage(img,0,0,this);
}
}
可否
package io;
import javaio; //文本格式
public class FileOutputShortcut {
public static void main(String[] args)
throws IOException {
BufferedReader in = new BufferedReader(
new StringReader(
BufferedInputFileread(""FileOutputShortcutjava")));
String s;
Point po[]=new Point[Integerparseint(inreadLine())];
int i=0;
while((s = inreadLine()) != null )
{
po[i]x=ssplite(" ")[0];po[i++]x=ssplite(" ")[1];
}
outclose();
// Show the stored file:
}
} / (Execute to see output) ///:~
dswidth/2应该是画布或者屏幕的一半。
radomnextInt()是取得一个随机整数
randomnextInt()%(dswidth/2)你可以理解为求的是“以半屏宽度(或高度)为偏移最大量,随机偏移一个量”,至于后面的+dswidth/2,表示在计算的偏移基础上再向正方向偏移半屏幕宽度(或高度)。
程序计算的最终结果就要看randomnextInt()的值了。不管这个值多大,整除(dswidth/2)后都不会超过(dswidth/2),当然,随机数越离散,得出的雪花越好看。你可以想象一下,dswidth和dsheight均为200像素的话,根据公式,半屏的大小(dswidth/2)就应该是100像素。randomnextInt()假如等于10,10%100=10,再加上(dswidth/2),就是110,同理算出y值。
简单来说,你在屏幕上画一个直角坐标系,原点就是你屏幕的中心点的话,雪花总会落在坐标系的第一象限区域内。
Point getLocation()
获得组件的位置,形式是指定组件左上角的一个点。
其返回值是一个Point对象,包含着X坐标和Y坐标
接下来可以使用Point的成员
double getX() 以双精度型返回点的 X 坐标。
double getY() 以双精度型返回点的 Y 坐标。
例如:有组件JButton:btnTest
Point p = btnTestgetLocation();
double x = pgetX();
double y = pgetY();
这样可以得到按钮的x坐标和y坐标
写过一个类似的,我当时的思路是这样的
动物棋子,应该设计成为一个类 ,并且有自定义的属性(,位置信息)啊,位置可以是一个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;
}
以上就是关于java获取鼠标坐标全部的内容,包括:java获取鼠标坐标、用java怎么获得一张图片上的一个文字的坐标点 求高手解答、java问题 怎样从文件读取一个点坐标等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)