JAVA获取雪片的X,Y坐标值

JAVA获取雪片的X,Y坐标值,第1张

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值。

简单来说,你在屏幕上画一个直角坐标系,原点就是你屏幕的中心点的话,雪花总会落在坐标系的第一象限区域内。

JLabelsetText(String text)可以设置标签的显示文本内容。

MouseEvent(鼠标事件)有两个方法:getX();getY();可以获得mouse的坐标。

定义一个类,implements mouseListener,mouseMotionListener就能ok了

如:

import javaawt;

import javaawtevent;

import javaxswing;

public class Mousetracker extends JFrame implements MouseListener,MouseMotionListener //实现两个鼠标事件接口 ,

{

private JLabel statusBar; //定义一个标签

public MouseTracker() //构造方法

{

super("Demonstrating mouse event"); //调用父窗口构造方法

statusBar=new JLabel();

getContentPane()add(statusBar,BorderLayoutSOUTH); //获得上下文面板,并添加组件

addMouseListener(this); //绑定事件监听器,因为本身实现了监听器接口,因此用this,即本身

addMouseMotionListener(this);

setSize(275,100);

setVisible(true);

} //以下是实现鼠标的各种事件,如按下,d起,拖拽等

public void mouseClicked(MouseEvent event)

{

statuxBarsetText("clicked at ["+eventgetX()+

","+eventgetY()+"]");

}

public void mousePressed(MouseEvent event)

{

statusBarsetText("Pressed at["+eventgetX()+

","+eventgetY()+"]");

}

public void mouseReleased(Mouseevent event)

{

statusBarsetText("Relessed at["+eventgetX()+

","+eventgetY()+"]");

}

public void mouseDragged(Mouseevent event)

{

statusBarsetText("Dragged at["+eventgetX()+

","+eventgetY()+"]");

}

public void mouseMoved(Mouseevent event)

{

statusBarsetText("Moved at["+eventgetX()+

","+eventgetY()+"]");

}

public static void main(String args[])

{

MouseTracker application=new MouseTracker();

applicationsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE); //设置用户在此窗体上发起 "close" 时默认执行的 *** 作

}

}

//你运行一下,看在你的电脑上能不能用。

//不能用的话肯定是坐标不对

//把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);

}

}

public class PointTest {

public static void main(String[] args) {

for (int i = 0; i < 8; i++) {

Systemoutprintln("K(" + i + ")_X =" + MakeK()[i][0] + "-------K("

+ i + ")_Y =" + MakeK()[i][1]);

}

}

public static double Apoingx(double c, double b) {

double a = Mathsqrt(Mathpow(c, 2) - Mathpow(b, 2));

return a;

}

public static double[][] MakeK() {

double[][] K = new double[8][2];

for (int i = 1; i < 9; i++) {

K[i - 1][0] = i;

K[i - 1][1] = Apoingx(9, i);

}

return K;

}

}

/////或者基于对象有方式也行

import javautilArrayList;

import javautilList;

public class PointTest {

public static void main(String[] args) {

int i=0;

for (PointK k : MakeK()) {

Systemoutprintln("x"+i+"  "+kgetX()+"---------y"+i+"  "+kgetY());

i++;

}

}

public static double Apoingx(double c, double b) {

double a = Mathsqrt(Mathpow(c, 2) - Mathpow(b, 2));

return a;

}

public static List<PointK> MakeK() {

List<PointK> list=new ArrayList<>();

for (int i = 1; i < 9; i++) {

listadd(new PointK(i, Apoingx(9,i)));

}

return list;

}

}

class PointK {

double x;

double y;

public PointK(double x,double y) {

thisx=x;

thisy=y;

}

public double getX() {

return x;

}

public double getY() {

return y;

}

}

以上就是关于JAVA获取雪片的X,Y坐标值全部的内容,包括:JAVA获取雪片的X,Y坐标值、java如何获得当前鼠标的坐标、java获取鼠标坐标等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9787876.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存