用java随机画出两个圆,判断它们是否相交


import java.awt.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.TitledBorder;

/***
7. * 随机画出两个圆,判断它们是否相交
8. * @author Firklaag
9. * @ver 0.01
10. * 编写代码实现同一平面内两圆是否碰撞,其中:第一个圆圆心坐标为(x1,y1),
半径是r1,第二个圆圆心坐标为(x2,y2),
半径是r2,数据结构自定义。
11. */
public class CheckCircul extends JFrame {
//定义画布
private MyPanel myPanel = new MyPanel();

public CheckCircul() {
add(myPanel);
}

public static void main(String[] args) {
CheckCircul demo = new CheckCircul();
run(demo, 800, 700);
}
/*
* 运行辅助方法
*/
public static void run(final JFrame f, final int width, final int height) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(width, height);
f.setVisible(true);
}
});
}

}
/*
40. * 画布类
41. */
class MyPanel extends JPanel {
//定义标签
private JLabel label = new JLabel();
//定义随机数
private Random ran = new Random();
//定义两个圆
private Circul tom;
private Circul jerry;

/**
52. * 生成两个圆并增加标签到画布
53. */
public MyPanel() {
tom = new Circul(ran.nextInt(300), ran.nextInt(300), ran.nextInt(400));
jerry = new Circul(ran.nextInt(300), ran.nextInt(300), ran.nextInt(400));
this.setBorder(new TitledBorder("CheckCircul"));
add(label);
}

@Override
protected void paintComponent(Graphics g) {
//画出两个圆
Rectangle rec1 = tom.draw(g);
Rectangle rec2 = jerry.draw(g);
//判断其是否相交
if (rec1.intersects(rec2)) {
label.setText("相交");
} else {
label.setText("不相交");
}
}

}
/**
76. * 定义圆形类,类中不仅有圆的属性,还有画圆的方法
77. */
class Circul {
private int x;
private int y;
private int r;

public Circul(int x, int y, int r) {
this.x = x;
this.y = y;
this.r = r;
}

public Rectangle draw(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
//调用抗锯齿API
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawOval(x, y, r, r);
return new Rectangle(x, y, r, r);

}
public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}
public int getR() {
return r;
}

public void setR(int r) {
this.r = r;
}
}
实验内容: 一、定义一个名为Circle的类,位于shape包中。要求如下: (1) 该类需要描述在二维坐标系中的位置,心的横、纵坐标值均取整数;描述的半径,取双精度类型。 (2) 无参构造方法将心坐标设为坐标点,半径设为1.0;有参构造方法需要根据参数值设置心坐标和半径值。 (3) 提供一个计算面积的方法double getArea()。 (4) 提供一个判断当前对象与参数对象是否相交的方法(相交方式见下图): boolean isIntersected(Circle another) (5) 提供一个判断当前对象是否包含参数对象的方法(包含方式见下图): boolean contains(Circle another) (6) 提供一个判断当前对象与参数对象的大小关系的方法(判断依据是半径,半径之差的绝对值小于0.0001认为相等;返回负数表示当前对象小,0表示相等,正数表示当前对象) int compareTo(Circle another) (7) 提供返回当前的字符串的方法,格式为:[Circle(x,y)-raduis] String toString() 二、定义一个包含main方法的类TestCircle,位于shape包中。要求如下: (1) main方法中调用以下方法。 (2) 编写静态方法createArray,生成位置和大小均随机的10个对象,置于数组中。 (3) 编写静态方法sort对这10个对象进行升序排序,并输出。 (4) 编写静态方法computeArea计算这10个中互不相交和包含的的面积之和。 提交要求:打包为可以执行的JAR文档,其中要包含源程序文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值