重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
靠,楼上的回答那么长啊,只要一个函数,就是
站在用户的角度思考问题,与客户深入沟通,找到八公山网站设计与八公山网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都做网站、成都网站建设、企业官网、英文网站、手机端网站、网站推广、国际域名空间、虚拟空间、企业邮箱。业务覆盖八公山地区。
drawOval(int x,int y,int w,int h); 这是是画椭圆形的函数,但是它也可以画圆形。
比如 drawOval(100,100,50,50); 就在坐标50,50画一个直径100的圆,只要把,最后的2个参数设成一样就是一个圆。要画直径200的话,就把最后2个参数设成200,200 一切OK了
包java.awt.Graphics
中的drawOval(int x,int y,int width,int height)方法可以画圆。
x,y是坐标,圆心位置
width,height是圆的宽度和长度,他们相等的时候就是圆,不等就是椭圆。值就是半径
public class Exam
{
public static void main(String[] args)
{
Circle c=new Circle(3,4,5);
System.out.printf("圆心:(%f,%f),半径:%f,面积:%f",c.x,c.y,c.r,c.countArea());
}
}
class Circle
{
public Circle()
{
this(0,0,0);
}
public Circle(double x,double y,double r)
{
this.x=x;
this.y=y;
this.r=r;
}
public double countArea()
{
return Math.PI*r*r;
}
/*private*/public double x,y,r;
}
这只是处理产生同心圆的方法,你只写了方法却没有调用。就好比你吃饭,筷子是用来夹菜的,那么筷子就是这个处理方法,但是你没有手,你说怎么夹菜?
public class Cycle {
private double x = 0;//圆心横坐标
private double y = 0;//圆心纵坐标
private double r = 0;//圆心半径
public static void main(String[] args) {
String relation = "";
Cycle c = new Cycle(0,0,1);
//相交 外切 内切 相离
Cycle c_xiangJiao = new Cycle(3,4,5);
Cycle c_waiQie = new Cycle(3,4,4);
Cycle c_neiQie = new Cycle(3,4,6);
Cycle c_xiangLi = new Cycle(3,4,2);
relation = c.relationWithOtherCycle(c_xiangJiao);
System.out.println("c c_xiangJiao relationShip :"+relation);
relation = c.relationWithOtherCycle(c_xiangLi);
System.out.println("c c_xiangLi relationShip :"+relation);
relation = c.relationWithOtherCycle(c_neiQie);
System.out.println("c c_neiQie relationShip :"+relation);
relation = c.relationWithOtherCycle(c_waiQie);
System.out.println("c c_waiQie relationShip :"+relation);
}
public Cycle(double x, double y, double r) {
this.r = r;
this.x = x;
this.y = y;
}
public Cycle() {
}
public String relationWithOtherCycle(Cycle c){
String relation = ""; //相交 外切 内切 相离
double length = 0;// (x-x1)*(x-x1)+(y-y1)*(y-y1) 开平方
length = Math.sqrt((this.x-c.getX())*(this.x-c.getX())+(this.y-c.getY())*(this.y-c.getY()));
//System.out.println("length : "+length);
if(length(this.r+c.getR())){
relation = "相离";
}else if (length==(this.r+c.getR())){
relation = "外切";
}else if (length==Math.abs(this.r-c.getR())){
relation = "内切";
}else if (lengthMath.abs(this.r+c.getR())lengthMath.abs(this.r-c.getR())){
relation = "相交";
}
return relation;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
}
//把测试程序写在Cycle的main方法里了
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class MyCanvas extends Canvas
{
int x,y,r,n;
int x0,y0;
MyCanvas()
{
setSize(圆心位置,圆心位置);
setBackground(Color.red);
}
public void setX(int x)
{
this.x=x;
}
public void setY(int y)
{
this.y=y;
}
public void setR(int r)
{
this.r=r;
}
public void setN(int n)
{
this.n=n;
}
public void paint(Graphics g1)
{
for(int i=0;i=360;i=i+360/n)
{
x0 = (int)(x+r*Math.cos(i));
y0 = (int)(y+r*Math.sin(i));
g1.drawString("*",x0,y0);}
}
}
public class e1 extends Applet implements ActionListener
{
MyCanvas canvas;
TextField inputR,inputX,inputY,inputN;
Label label1,label2,label3;
Button b1,b2;
public void init()
{
canvas = new MyCanvas();
inputR = new TextField(6);
inputX = new TextField(6);
inputY = new TextField(6);
inputN = new TextField(6);
b1 = new Button("确定");
b1.addActionListener(this);
label1 = new Label("输入位置坐标:");
label2 = new Label("输入半径:");
label3 = new Label("输入要打印的*数:");
add(label1);
add(inputX);
add(inputY);
add(label2);
add(inputR);
add(label3);
add(inputN);
add(b1);
add(canvas);
}
public void actionPerformed(ActionEvent e)
{
int x=0,y=0,n=0,r=0;
try
{
x=Integer.valueOf(inputX.getText()).intValue();
y=Integer.valueOf(inputY.getText()).intValue();
n=Integer.valueOf(inputN.getText()).intValue();
r=Integer.valueOf(inputR.getText()).intValue();
canvas.setX(x);
canvas.setY(y);
canvas.setR(r);
canvas.setN(n);
canvas.repaint();
}
catch(NumberFormatException ee)
{
x = 0;
y = 0;
r = 0;
n = 0;
}
}
}