两点到原点的代码java java点到点的距离

java 求高手帮助 在界面上给出两个或者多个点(坐标) 点击按钮 在每两个点之间出现一个点 求源代码 谢谢x给你代码,附件是运行截图 。
import java.awt.BorderLayout;
public class App extends JFrame implements ActionListener {
private JComboBox combox = null;
private Random random = new Random();
private MyPanel center = new MyPanel();
public App() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel label = new JLabel("point numbers :");
Integer[] items = { 2, 3, 4, 5, 6 };
combox = new JComboBox(items);
JButton button = new JButton("OK");
button.addActionListener(this);
panel.add(label, BorderLayout.WEST);
panel.add(combox, BorderLayout.CENTER);
panel.add(button, BorderLayout.EAST);
add(panel, BorderLayout.NORTH);
add(center, BorderLayout.CENTER);
setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
@Override
public void actionPerformed(ActionEvent e) {
int number = (Integer) combox.getSelectedItem();
Listlt;Pointgt; points = new ArrayListlt;Pointgt;();
for (int i = 0; i lt; number; i) {
Point point = new Point();
point.x = random.nextInt(400)80;
point.y = random.nextInt(260)50;
points.add(point);
}
center.points = points;
// center.repaint();
repaint();
}
}
class MyPanel extends JPanel {
public Listlt;Pointgt; points = null;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (points != null) {
paintPoints(g, points);
}
}
private void paintPoints(Graphics g, Listlt;Pointgt; points) {
if (points.size() == 1) {
return;
}
Point point = points.remove(0);
for (int i = 0; i lt; points.size(); i) {
Point tmp = points.get(i);
paintCenterPoint(g, point, tmp);
}
paintPoints(g, points);
}
【两点到原点的代码java java点到点的距离】private void paintCenterPoint(Graphics g, Point src, Point dest) {
// 在两个点上画一个直径20的圆点
g.fillOval(src.x - 10, src.y - 10, 20, 20);
g.fillOval(dest.x - 10, dest.y - 10, 20, 20);
// 画点连接
g.drawLine(src.x, src.y, dest.x, dest.y);
// 计算中间点位置
int x = (int) Math.floor(src.x(dest.x - src.x) / 2);
int y = (int) Math.floor(src.y(dest.y - src.y) / 2);
Color color = g.getColor();
g.setColor(Color.GREEN);
// 画两点中间的点
g.fillOval(x - 10, y - 10, 20, 20);
g.setColor(color);
}
}
这道Java题怎么做?Java源代码两点到原点的代码java:
public class Test {
public static void main(String[] args) {
Point p1 = new Point(4, 5);
System.out.printf("点p坐标为(%f,%f)\n", p1.getX(), p1.getY());
p1.setX(3);
p1.setY(4);
System.out.printf("重置后点p坐标为(%f,%f)\n", p1.getX(), p1.getY());
System.out.printf("点(%f, %f)到原点两点到原点的代码java的距离两点到原点的代码java的平方为%f\n", p1.getX(), p1.getY(),
p1.distance());
Point p2 = new Point(1, 2);
System.out.printf("点(%f,%f)到点(%f,%f)的距离的平方为%f\n", p1.getX(),
p1.getY(), p2.getX(), p2.getY(), p1.distance(p2));
}
}
class Point {
protected double x;
protected double y;
public Point(){
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public void setX(double x) {
this.x = x;
}
public double getX() {
return this.x;
}
public void setY(double y) {
this.y = y;
}
public double getY() {
return this.y;
}
public double distance() {
return Math.pow(x, 2)Math.pow(y, 2);
}
public double distance(Point p) {
return Math.pow(this.x - p.x, 2)Math.pow(this.y - p.y, 2);
}
}
运行测试:
java怎么比较2个对象?你这个compareTo方法是干啥的?思路不清啊....
你应该写的是一个点的对象(Position)..他要和别一个点(Position
)去比较.所以至少你的Position
类中的compareTo这个方法里要获得别一个点的对象.
也就是说应该在实现类中有这样的形式
点1对象.compareTo(点2对象);//说白了就是你的实现类里这行
System.out.println("两点到原点的距离之差为" compareTo());
应该改成
System.out.println("两点到原点的距离之差为" a.mpareTo(b);
在计较的时候.也就是你的compareTo()这个方法里..就应该是对象1的距离-对象2的举例
也就是类似这样的.
Math.sqrt(a.getx()*a.getx()* a.gety()*a.gety())-Math.sqrt(b.getx()*b.getx() b.gety()*b.gety();//
用java采用面向对象思想设计求两点间的距离,求代码import java.util.Scanner;
public class Demo
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Point p1,p2;
System.out.println("请输入第1个点的x、y坐标:");
p1=new Point(sc.nextDouble(),sc.nextDouble());
System.out.println("请输入第2个点的x、y坐标:");
p2=new Point(sc.nextDouble(),sc.nextDouble());
System.out.println("点" p1 "与点" p2 "的距离是" p1.distance(p2));
}
}
class Point
{
Point(double x,double y)
{
this.x=x;
this.y=y;
}
public String toString()
{
return "(" x "," y ")";
}
double distance(Point p)
{
return Math.sqrt(Math.pow(this.x-p.x,2) Math.pow(this.y-p.y,2));
}
private double x,y;
}
请问java如何用接口实现比较两个点到原点的距离的程序//接口
public interface CompareLength {
public int compare(Point p1,Point p2);
}
//接口实现
public class CompareLengthimpl implements CompareLength {
public static void main(String[] args) {
CompareLength c=new CompareLengthimpl();
Point p1=new Point(1,1);
Point p2=new Point(2,2);
System.out.print(c.compare(p1, p2));
}
//p1大于p2 返回1 p1等于p2返回0 p1小于p2返回-1
public int compare(Point p1,Point p2){
if((p1.getX()*p1.getX() p1.getY()*p1.getY())(p2.getX()*p2.getX() p2.getY()*p2.getY()))
return 1;
else if((p1.getX()*p1.getX() p1.getY()*p1.getY())==(p2.getX()*p2.getX() p2.getY()*p2.getY()))
return 0;
else
return -1;
}
}
//坐标点对象
public class Point {
int x;
int y;
public Point(int x,int y){
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;
}
}
不知道是不是这样 。
用java计算该点到原点的距离 , public float pto0(){ }类型直接用double了,
public Double pto0(Double x,Double y){ //参数是点的坐标
returnMath.sqrt(Math.pow(x, 2) Math.pow(y, 2));
}
关于两点到原点的代码java和java点到点的距离的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读