JAVA 怎么判断鼠标单击处是否在某一图形上
初次接触对图形操作的问题,实在不会了,请教。问题:对通过多线程产生的各个球分别进行操作,判断再次点击处是否在图形上,这个判断整不了,下面是代码,由于长度限制的问题省略了部...
初次接触对图形操作的问题,实在不会了,请教。
问题:对通过多线程产生的各个球分别进行操作,判断再次点击处是否在图形上,这个判断整不了,下面是代码,由于长度限制的问题省略了部分系统自动添加代码。
package mypaint;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JFrame;import javax.swing.JPanel;
public class MyPaint {
/**
* @param 在鼠标点击处启动多线程,产生一个球,下落。
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
class MyFrame extends JFrame implements MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Point p;
JPanel jp;
JPanel locate;
MyFrame() {
super("画图");
jp = new JPanel();
jp.setBounds(0, 0, 800, 600);
this.add(jp);
this.jp.addMouseListener(this);
// 设置窗体的位置和大小
this.setBounds(200, 50, 800, 600);
this.setResizable(false);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//怎么在这里加一个事件,让此程序可以实现:单击在某个球上时可以对这个球进行控制,如让其暂停,对图形的操作,
//这个位置的判断不会。。。。
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
p = e.getPoint();
Random r = new Random();
Ball ball = new Ball(p.x , p.y );
// Ball ball = new Ball(r.nextInt(600), 10);
ball.start();
}
class Ball extends Thread {
int start_x, start_y; int ball_size = (int)(Math.random()*30);
Ball(int i, int j) {
start_x = i-ball_size/2;
start_y = j-ball_size/2;
}
public void run() {
Graphics g = jp.getGraphics();
// int x = 0, y = 0;
// 计算画球时X轴,Y轴的最大坐标
int HuaBan_x = 800 - ball_size;
int HuaBan_y = 570 - ball_size;
// 球移动的增量
Random r1 = new Random();
Random r2 = new Random();
int x_increase = r1.nextInt(20)-10, y_increase = r2.nextInt(20)-10;
while (true) {
// 将上一次画的球擦掉
g.setColor(jp.getBackground());
g.fillOval(start_x, start_y, ball_size, ball_size);
// 画球
g.setColor(Color.blue);
start_x = start_x + x_increase;
start_y = start_y + y_increase;
g.fillOval(start_x, start_y, ball_size, ball_size);
// 判断球是否到达了边界,若到达了则转向
if (start_x <= 0 || start_x >= HuaBan_x)
x_increase = -x_increase;
if (start_y <= 0 || start_y >= HuaBan_y)
y_increase = -y_increase;
try {
// 休眠一段时间
Thread.sleep(30);
} catch (Exception e) {
}
}
}
}} 展开
问题:对通过多线程产生的各个球分别进行操作,判断再次点击处是否在图形上,这个判断整不了,下面是代码,由于长度限制的问题省略了部分系统自动添加代码。
package mypaint;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JFrame;import javax.swing.JPanel;
public class MyPaint {
/**
* @param 在鼠标点击处启动多线程,产生一个球,下落。
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
class MyFrame extends JFrame implements MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Point p;
JPanel jp;
JPanel locate;
MyFrame() {
super("画图");
jp = new JPanel();
jp.setBounds(0, 0, 800, 600);
this.add(jp);
this.jp.addMouseListener(this);
// 设置窗体的位置和大小
this.setBounds(200, 50, 800, 600);
this.setResizable(false);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//怎么在这里加一个事件,让此程序可以实现:单击在某个球上时可以对这个球进行控制,如让其暂停,对图形的操作,
//这个位置的判断不会。。。。
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
p = e.getPoint();
Random r = new Random();
Ball ball = new Ball(p.x , p.y );
// Ball ball = new Ball(r.nextInt(600), 10);
ball.start();
}
class Ball extends Thread {
int start_x, start_y; int ball_size = (int)(Math.random()*30);
Ball(int i, int j) {
start_x = i-ball_size/2;
start_y = j-ball_size/2;
}
public void run() {
Graphics g = jp.getGraphics();
// int x = 0, y = 0;
// 计算画球时X轴,Y轴的最大坐标
int HuaBan_x = 800 - ball_size;
int HuaBan_y = 570 - ball_size;
// 球移动的增量
Random r1 = new Random();
Random r2 = new Random();
int x_increase = r1.nextInt(20)-10, y_increase = r2.nextInt(20)-10;
while (true) {
// 将上一次画的球擦掉
g.setColor(jp.getBackground());
g.fillOval(start_x, start_y, ball_size, ball_size);
// 画球
g.setColor(Color.blue);
start_x = start_x + x_increase;
start_y = start_y + y_increase;
g.fillOval(start_x, start_y, ball_size, ball_size);
// 判断球是否到达了边界,若到达了则转向
if (start_x <= 0 || start_x >= HuaBan_x)
x_increase = -x_increase;
if (start_y <= 0 || start_y >= HuaBan_y)
y_increase = -y_increase;
try {
// 休眠一段时间
Thread.sleep(30);
} catch (Exception e) {
}
}
}
}} 展开
5个回答
展开全部
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFrame extends JFrame implements MouseListener {
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
private static final long serialVersionUID = 1L;
private Point p;
private JPanel jp;
private JPanel locate;
private ArrayList<Ball> ballList;
MyFrame() {
super("画图");
jp = new JPanel();
jp.setBounds(0, 0, 800, 600);
this.add(jp);
this.jp.addMouseListener(this);
// 设置窗体的位置和大小
this.setBounds(200, 50, 800, 600);
this.setResizable(false);
this.ballList = new ArrayList<Ball>();
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 怎么在这里加一个事件,让此程序可以实现:单击在某个球上时可以对这个球进行控制,如让其暂停,对图形的操作,
// 这个位置的判断不会。。。。
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
p = e.getPoint();
int x = p.x;
int y = p.y;
for (Ball b : ballList) {
if (b.isInArea(x, y)) {
b.changeActivity();
return;
}
}
Random r = new Random();
Ball ball = new Ball(x, y);
this.ballList.add(ball);
ball.start();
}
class Ball extends Thread {
private int start_x, start_y;
private int ball_size = (int) (Math.random() * 30 + 40);
private int isMove;
Ball(int i, int j) {
start_x = i - ball_size / 2;
start_y = j - ball_size / 2;
isMove = 1;
}
public void run() {
Graphics g = jp.getGraphics();
// int x = 0, y = 0;
// 计算画球时X轴,Y轴的最大坐标
int HuaBan_x = 800 - ball_size;
int HuaBan_y = 570 - ball_size;
// 球移动的增量
Random r1 = new Random();
Random r2 = new Random();
int x_increase = r1.nextInt(20) - 10, y_increase = r2.nextInt(20) - 10;
while (true) {
// 将上一次画的球擦掉
g.setColor(jp.getBackground());
g.fillOval(start_x, start_y, ball_size, ball_size);
// 画球
g.setColor(Color.blue);
if(isMove == 1){
start_x = start_x + x_increase;
start_y = start_y + y_increase;
}
g.fillOval(start_x, start_y, ball_size, ball_size);
// 判断球是否到达了边界,若到达了则转向
if (start_x <= 0 || start_x >= HuaBan_x)
x_increase = -x_increase;
if (start_y <= 0 || start_y >= HuaBan_y)
y_increase = -y_increase;
try {
// 休眠一段时间
Thread.sleep(200);
} catch (Exception e) {
}
}
}
public void changeActivity() {
this.isMove *= -1;
}
public boolean isInArea(int x, int y) {
if (x >= start_x && x <= start_x + ball_size && y >= start_y
&& y <= start_y + ball_size) {
return true;
}
return false;
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
简单的改了下。。也就是点到园就暂停或者活动
原来你设定的直径太小了。。直接点不到我给加大了
点在圆内也是简单的处理就是判断点是否在x和y左上角
边长为ball_size的正方形内。。严谨点是要计算圆面积的。
懒得算。。。。
其实你编程习惯很不好。。。我是按照你的思路给改的
但是如果我自己写我不会这样设置变量和方法
比如一个和简单的private和public的设置
方法的名字 类的名字变量的名字等等很多细节。。
建议你去看
java编程规范化的相关资料
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFrame extends JFrame implements MouseListener {
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
private static final long serialVersionUID = 1L;
private Point p;
private JPanel jp;
private JPanel locate;
private ArrayList<Ball> ballList;
MyFrame() {
super("画图");
jp = new JPanel();
jp.setBounds(0, 0, 800, 600);
this.add(jp);
this.jp.addMouseListener(this);
// 设置窗体的位置和大小
this.setBounds(200, 50, 800, 600);
this.setResizable(false);
this.ballList = new ArrayList<Ball>();
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 怎么在这里加一个事件,让此程序可以实现:单击在某个球上时可以对这个球进行控制,如让其暂停,对图形的操作,
// 这个位置的判断不会。。。。
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
p = e.getPoint();
int x = p.x;
int y = p.y;
for (Ball b : ballList) {
if (b.isInArea(x, y)) {
b.changeActivity();
return;
}
}
Random r = new Random();
Ball ball = new Ball(x, y);
this.ballList.add(ball);
ball.start();
}
class Ball extends Thread {
private int start_x, start_y;
private int ball_size = (int) (Math.random() * 30 + 40);
private int isMove;
Ball(int i, int j) {
start_x = i - ball_size / 2;
start_y = j - ball_size / 2;
isMove = 1;
}
public void run() {
Graphics g = jp.getGraphics();
// int x = 0, y = 0;
// 计算画球时X轴,Y轴的最大坐标
int HuaBan_x = 800 - ball_size;
int HuaBan_y = 570 - ball_size;
// 球移动的增量
Random r1 = new Random();
Random r2 = new Random();
int x_increase = r1.nextInt(20) - 10, y_increase = r2.nextInt(20) - 10;
while (true) {
// 将上一次画的球擦掉
g.setColor(jp.getBackground());
g.fillOval(start_x, start_y, ball_size, ball_size);
// 画球
g.setColor(Color.blue);
if(isMove == 1){
start_x = start_x + x_increase;
start_y = start_y + y_increase;
}
g.fillOval(start_x, start_y, ball_size, ball_size);
// 判断球是否到达了边界,若到达了则转向
if (start_x <= 0 || start_x >= HuaBan_x)
x_increase = -x_increase;
if (start_y <= 0 || start_y >= HuaBan_y)
y_increase = -y_increase;
try {
// 休眠一段时间
Thread.sleep(200);
} catch (Exception e) {
}
}
}
public void changeActivity() {
this.isMove *= -1;
}
public boolean isInArea(int x, int y) {
if (x >= start_x && x <= start_x + ball_size && y >= start_y
&& y <= start_y + ball_size) {
return true;
}
return false;
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
简单的改了下。。也就是点到园就暂停或者活动
原来你设定的直径太小了。。直接点不到我给加大了
点在圆内也是简单的处理就是判断点是否在x和y左上角
边长为ball_size的正方形内。。严谨点是要计算圆面积的。
懒得算。。。。
其实你编程习惯很不好。。。我是按照你的思路给改的
但是如果我自己写我不会这样设置变量和方法
比如一个和简单的private和public的设置
方法的名字 类的名字变量的名字等等很多细节。。
建议你去看
java编程规范化的相关资料
展开全部
运行了一下你的代码,再综合了一下大家的意见,对你的Ball类做了些改动,如下:
//由于原有的类不能直接增加鼠标监听,所以让他继承了Label;
//又因为java不支持多继承,为了保持线程而实现了Runnable接口
class Ball extends Label implements Runnable {
int start_x, start_y;
int ball_size = (int) (Math.random() * 30);
Ball(int i, int j) {
setBackground(Color.blue); //使用和圆球相同的蓝色
start_x = i - ball_size / 2;
start_y = j - ball_size / 2;
Thread loop =new Thread(this);
loop.start();
addMouseListener(new MouseAdapter() { //增加鼠标监听
public void mouseClicked(MouseEvent e) {
System.out.println("球被点击了");
}
});
}
public void run() {
Graphics g = jp.getGraphics();
// 计算画球时X轴,Y轴的最大坐标
int HuaBan_x = 800 - ball_size;
int HuaBan_y = 570 - ball_size;
// 球移动的增量
Random r1 = new Random();
Random r2 = new Random();
int x_increase = r1.nextInt(20) - 10, y_increase = r2.nextInt(20) - 10;
while (true) {
// 将上一次画的球擦掉
g.setColor(jp.getBackground());
g.fillOval(start_x, start_y, ball_size, ball_size);
// 画球
g.setColor(Color.blue);
start_x = start_x + x_increase;
start_y = start_y + y_increase;
g.fillOval(start_x, start_y, ball_size, ball_size);
setBounds(start_x, start_y, ball_size, ball_size); //随球更改label的位置
// 判断球是否到达了边界,若到达了则转向
if (start_x <= 0 || start_x >= HuaBan_x)
x_increase = -x_increase;
if (start_y <= 0 || start_y >= HuaBan_y)
y_increase = -y_increase;
try {
// 休眠一段时间
Thread.sleep(300); //为了方便点击测试,改成了300毫秒
} catch (Exception e) {
}
}
}
}
说明一下:运行时会随着蓝色的球出现一个蓝色的矩形,点击到矩形部分后,不会创建出新的Ball实例,而是执行System.out.println("球被点击了"); ,可以在这部分增加控制。
这份代码还不够完善,但希望可以起到抛砖引玉的作用。
//由于原有的类不能直接增加鼠标监听,所以让他继承了Label;
//又因为java不支持多继承,为了保持线程而实现了Runnable接口
class Ball extends Label implements Runnable {
int start_x, start_y;
int ball_size = (int) (Math.random() * 30);
Ball(int i, int j) {
setBackground(Color.blue); //使用和圆球相同的蓝色
start_x = i - ball_size / 2;
start_y = j - ball_size / 2;
Thread loop =new Thread(this);
loop.start();
addMouseListener(new MouseAdapter() { //增加鼠标监听
public void mouseClicked(MouseEvent e) {
System.out.println("球被点击了");
}
});
}
public void run() {
Graphics g = jp.getGraphics();
// 计算画球时X轴,Y轴的最大坐标
int HuaBan_x = 800 - ball_size;
int HuaBan_y = 570 - ball_size;
// 球移动的增量
Random r1 = new Random();
Random r2 = new Random();
int x_increase = r1.nextInt(20) - 10, y_increase = r2.nextInt(20) - 10;
while (true) {
// 将上一次画的球擦掉
g.setColor(jp.getBackground());
g.fillOval(start_x, start_y, ball_size, ball_size);
// 画球
g.setColor(Color.blue);
start_x = start_x + x_increase;
start_y = start_y + y_increase;
g.fillOval(start_x, start_y, ball_size, ball_size);
setBounds(start_x, start_y, ball_size, ball_size); //随球更改label的位置
// 判断球是否到达了边界,若到达了则转向
if (start_x <= 0 || start_x >= HuaBan_x)
x_increase = -x_increase;
if (start_y <= 0 || start_y >= HuaBan_y)
y_increase = -y_increase;
try {
// 休眠一段时间
Thread.sleep(300); //为了方便点击测试,改成了300毫秒
} catch (Exception e) {
}
}
}
}
说明一下:运行时会随着蓝色的球出现一个蓝色的矩形,点击到矩形部分后,不会创建出新的Ball实例,而是执行System.out.println("球被点击了"); ,可以在这部分增加控制。
这份代码还不够完善,但希望可以起到抛砖引玉的作用。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用分组技术 判断if..... else....
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
添加鼠标事件监听呗。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你可以为每一个图形添加鼠标监听,代码楼上的就可以的,你再完善一下。当鼠标单击图形的时候,把图形的速度设置为0 ,这样就实现了暂停
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询