「java编写坐标」java画坐标图

博主:adminadmin 2023-01-07 02:12:07 810

本篇文章给大家谈谈java编写坐标,以及java画坐标图对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java编写一个表示坐标点的类(Point),其中包含x和y两个坐标点的值,并且包含一个打印出该点坐标的方法

public class Point{

    private int x;

    private int y;

    

    public getX(){ return x; }

    

    public getY(){ return y; }

    

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

    

    public setY(int y){ this.y = y; }

    

    public Point(int x, int y){

        this.x = x;

        this.y = y;

    }

    

    public displayPoint (){

        //输出坐标

        System.out.println("x:"+x+",y:"+y);

    }

    public static distancePoint(Point p1, Point p2){

        //计算坐标距离

        int a = p1.getX()-p2.getX();

        int b = p1.getY()-p2.getY();

        return Math.sqrt(a*a+b*b);

    }

}

用java编写平面坐标类Point并测试

运行结果:

public class Pointtest {

public static void main(String[] args) {

Point point1 = new Point(0,0);

Point point2 = new Point(5,5);

Point point3 = new Point(5,5);

System.out.println(point1.equals(point2));

System.out.println(point2.equals(point3));

System.out.println(Point.distance(point1,point2));

System.out.println(Point.distance(point1,5,5));

System.out.println(Point.distance(0,0,5,5));

}

}

class Point{

private  int x;

private  int y;

public Point() {

super();

// TODO Auto-generated constructor stub

}

public Point(int x, int y) {

super();

this.x = x;

this.y = y;

}

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;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + x;

result = prime * result + y;

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Point other = (Point) obj;

if (x != other.x)

return false;

if (y != other.y)

return false;

return true;

}

public String toString() {

return "Point [x=" + x + ", y=" + y + "]";

}

public static double distance(int x, int y,int a,int b) {

return Math.sqrt((Math.pow((x-a), 2)+Math.pow((y-b), 2)));

}

public static double distance(Point p,int a,int b) {

return Math.sqrt((Math.pow((p.getX()-a), 2)+Math.pow((p.getY()-b), 2)));

}

public static double distance(Point a,Point b) {

return Math.sqrt((Math.pow((a.getX()-b.getX()), 2)+Math.pow((a.getY()-b.getY()), 2)));

}

}

用java语言编写输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。

import

java.util.Scanner;

public

class

TestObject

{

/**

*

@param

args

*/

public

static

void

main(String[]

args)

{

//

TODO

Auto-generated

method

stub

Scanner

in

=

new

Scanner(System.in);

System.out.println("请输入第一个坐标点:");

int

x1

=

in.nextInt();

int

y1

=

in.nextInt();

System.out.println("请输入第二个坐标点:");

int

x2

=

in.nextInt();

int

y2

=

in.nextInt();

int

distance

=

(int)

Math.sqrt(Math.abs((x1

-

x2)*(x1

-

x2))+Math.abs((y1

-

y2)*(y1

-

y2)));

System.out.println("两点间距离是:"+distance);

}

}

java中如何定义控件的坐标?

1. public void setLocation(Point p)\x0d\x0a\x0d\x0a 将组件移到新位置。通过点 p 来指定新位置的左上角。在父级坐标空间中给出点 p。\x0d\x0a参数:\x0d\x0a\x0d\x0a p - 定义新位置左上角的点,在此组件的父级坐标空间中给出\x0d\x0a\x0d\x0a2. public void setLocation(int x, int y) //重载的方法\x0d\x0a\x0d\x0a 将组件移到新位置。通过此组件父级坐标空间中的 x 和 y 参数来指定新位置的左上角。\x0d\x0a\x0d\x0a参数:\x0d\x0a x - 父级坐标空间中新位置左上角的 x 坐标\x0d\x0a y - 父级坐标空间中新位置左上角的 y 坐标\x0d\x0a\x0d\x0a3. public void setBounds(int x,\x0d\x0a int y,\x0d\x0a int width,\x0d\x0a int height)\x0d\x0a\x0d\x0a 移动组件并调整其大小。由 x 和 y 指定左上角的新位置,由 width \x0d\x0a和 height 指定新的大小。\x0d\x0a\x0d\x0a参数:\x0d\x0a x - 组件的新 x 坐标\x0d\x0a y - 组件的新 y 坐标\x0d\x0a width - 组件的新 width\x0d\x0a height - 组件的新 height

关于java编写坐标和java画坐标图的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。