「java坐标类型」java中坐标怎么表示
本篇文章给大家谈谈java坐标类型,以及java中坐标怎么表示对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java中如何定义控件的坐标?
- 2、java编写一个表示坐标点的类(Point),其中包含x和y两个坐标点的值,并且包含一个打印出该点坐标的方法
- 3、用java编写平面坐标类Point并测试
- 4、java中怎么返回一个坐标?
- 5、Java要求设计一个“坐标点”类(Point)
- 6、java中表示能表示坐标的变量的类型是什么??高手!!急!!!
java中如何定义控件的坐标?
1. public void setLocation(Point p)
将组件移到新位置。通过点 p 来指定新位置的左上角。在父级坐标空间中给出点 p。
参数:
p - 定义新位置左上角的点,在此组件的父级坐标空间中给出
2. public void setLocation(int x, int y) //重载的方法
将组件移到新位置。通过此组件父级坐标空间中的 x 和 y 参数来指定新位置的左上角。
参数:
x - 父级坐标空间中新位置左上角的 x 坐标
y - 父级坐标空间中新位置左上角的 y 坐标
3. public void setBounds(int x,
int y,
int width,
int height)
移动组件并调整其大小。由 x 和 y 指定左上角的新位置,由 width
和 height 指定新的大小。
参数:
x - 组件的新 x 坐标
y - 组件的新 y 坐标
width - 组件的新 width
height - 组件的新 height
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中怎么返回一个坐标?
java.awt.Point类 封装了x和y值的
getPoint返回一般都是返回Point对象 而不是double对象
Java要求设计一个“坐标点”类(Point)
public class Point {
double x1,x2,y1,y2;
double d,x3,y3;
Point(double x1,double y1,double x2,double y2){//构造方法
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
void TestPoint(){//求距离和中点坐标
x3 = (x1 + x2)/2;
y3 = (y1 + y2)/2;
d = Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
System.out.println("(" + x3 + ","+ y3 + ")");
System.out.println(d);
}
public static void main(String[] args) {
Point p = new Point(1,1,2,2);
p.TestPoint();
}
}
java中表示能表示坐标的变量的类型是什么??高手!!急!!!
表示坐标的 可以用 double,float 类型
但是用 double 要比 float 好
关于java坐标类型和java中坐标怎么表示的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-27,除非注明,否则均为
原创文章,转载请注明出处。