Java 将圆移动到任意位置
我正在制作这个java小程序,它画了一个圆,当我用鼠标悬停在它上面时,我需要将圆移动到一个随机的地方。现在我的小程序在一个随机的地方画圆,当我用鼠标悬停在圆上时改变颜色,但是如何使它也移动到一个随机的地方呢?如有任何建议,将不胜感激Java 将圆移动到任意位置,java,applet,Java,Applet,我正在制作这个java小程序,它画了一个圆,当我用鼠标悬停在它上面时,我需要将圆移动到一个随机的地方。现在我的小程序在一个随机的地方画圆,当我用鼠标悬停在圆上时改变颜色,但是如何使它也移动到一个随机的地方呢?如有任何建议,将不胜感激 import java.awt.*; import java.applet.*; import java.awt.event.*; public class circle extends Applet implements MouseMotionListene
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class circle extends Applet implements MouseMotionListener
{
// The X-coordinate and Y-coordinate of the last Mouse Position.
int xpos;
int ypos;
//generate random place for circle (in coordinates from 0 to 400
int x=(int)(Math.random() * 401);
int y=(int)(Math.random() * 401);
int width;
int height;
// wll be true when the Mouse is in the circle
boolean active;
public void init()
{
width=40;
height=40;
// Add the MouseMotionListener to applet
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
if (active){g.setColor(Color.black);}
else {g.setColor(Color.blue);}
g.fillRoundRect(x, y, width, height, 200, 200);
// This will show the coordinates of the mouse
// at the place of the mouse.
g.drawString("("+xpos+","+ypos+")",xpos,ypos);
}
// This will be excuted whenever the mousemoves in the applet
public void mouseMoved(MouseEvent me)
{
xpos = me.getX();
ypos = me.getY();
// Check if the mouse is in the circle
if (xpos > x&& xpos < x+width && ypos > y
&& ypos < y+height)
active = true;
else
active = false;
//show the results of the motion
repaint();
}
public void mouseDragged(MouseEvent me)
{
}
}
import java.awt.*;
导入java.applet.*;
导入java.awt.event.*;
公共类circle扩展小程序实现MouseMotionListener
{
//最后一个鼠标位置的X坐标和Y坐标。
int XPO;
int ypos;
//为圆生成随机位置(坐标范围从0到400
int x=(int)(Math.random()*401);
int y=(int)(Math.random()*401);
整数宽度;
内部高度;
//当鼠标在圆圈内时,将为真
布尔主动;
公共void init()
{
宽度=40;
高度=40;
//将MouseMotionListener添加到小程序
addMouseMotionListener(此);
}
公共空间涂料(图g)
{
if(活动){g.setColor(Color.black);}
else{g.setColor(Color.blue);}
g、 fillRoundRect(x,y,宽度,高度,200200);
//这将显示鼠标的坐标
//在老鼠的地方。
g、 拉索(“(“+xpos+”,“+ypos+”),xpos,ypos);
}
//只要鼠标移动到小程序中,就会执行此操作
public void mouseMoved(MouseEvent me)
{
xpos=me.getX();
ypos=me.getY();
//检查鼠标是否在圆圈内
如果(xpos>x&&xposy
&&ypos
只需将
x=(int)(Math.random() * 401);
y=(int)(Math.random() * 401);
在您的绘制()方法中进入状态
public void paint(Graphics g)
{
if (active){
g.setColor(Color.black);
//here
x=(int)(Math.random() * 401);
y=(int)(Math.random() * 401);
}
...
鼠标悬停时“刷新”位置
编辑 正如@MadProgrammer在下面的注释paint()中所写的那样,方法不是放置应用程序逻辑的最佳位置。更改位置的最佳位置是将active标志设置为True的侦听器 与其将x,y…行粘贴到pain()方法中,不如将其粘贴到此处:
public void mouseMoved(MouseEvent me)
{
...
if (xpos > x&& xpos < x+width && ypos > y && ypos < y+height)
{
active = true;
//here
x=(int)(Math.random() * 401);
y=(int)(Math.random() * 401);
}
else
...
}
public void mouseMoved(MouseEvent me)
{
...
如果(xpos>x&&xposy和&ypos
只需将
x=(int)(Math.random() * 401);
y=(int)(Math.random() * 401);
在您的绘制()方法中进入状态
public void paint(Graphics g)
{
if (active){
g.setColor(Color.black);
//here
x=(int)(Math.random() * 401);
y=(int)(Math.random() * 401);
}
...
鼠标悬停时“刷新”位置
编辑 正如@MadProgrammer在下面的注释paint()中所写的那样,方法不是放置应用程序逻辑的最佳位置。更改位置的最佳位置是将active标志设置为True的侦听器 与其将x,y…行粘贴到pain()方法中,不如将其粘贴到此处:
public void mouseMoved(MouseEvent me)
{
...
if (xpos > x&& xpos < x+width && ypos > y && ypos < y+height)
{
active = true;
//here
x=(int)(Math.random() * 401);
y=(int)(Math.random() * 401);
}
else
...
}
public void mouseMoved(MouseEvent me)
{
...
如果(xpos>x&&xposy和&ypos
Simple:在设置XPO和YPO时,创建一个随机场并使用其nextInt(某个上限值)
方法。什么会触发移动到随机位置?鼠标指针在圆圈中移动(或非常接近它时,不会移动)。据我所知,我应该在if语句中的public void paint function中编写这些操作。简单:创建一个随机场,并在设置XPO和YPO时使用其nextInt(某些上限值)
方法。什么会触发移动到随机位置?当鼠标指针在圆圈中移动时(或者离它很近,不是吗)。据我所知,我应该在if语句中的public void paint function中编写这些操作。绘制的目的是绘制组件的当前状态,它不应该包含任何逻辑,因为可以出于任何原因调用绘制,这可能会产生意外的结果。很好,那么应该将其移动到零件当设置active flag(在侦听器方法中)时,根据OP的代码,用于生成位置的逻辑应该在鼠标侦听器中(如果您是这么说的话)绘制的目的是绘制组件的当前状态,它不应包含任何逻辑,因为可以出于任何原因调用绘制,这可能会产生意外的结果良好点-然后在设置活动标志时(在侦听器方法中)应将其移动到零件根据OP的代码,生成位置的逻辑应该在MouseListener
中(如果您是这么说的话)