導航:首頁 > 編程語言 > java坐標系

java坐標系

發布時間:2022-12-06 16:30:07

java求坐標系中任意兩點的距離取出最短距離

Point p1 = new Point(x1,y1);
Point p2 = new Point(x2,y2);
double gap = Math.sqrt(Math.pow(p1.y-p2.y,2) + Math.pow(p1.x-p2.x,2));

② 用JAVA用Graphics2D來繪制一個坐標系,帶刻度的那種

public class test8888 extends JPanel
{
Polygon po = new Polygon();
Font fn = new Font("宋體", Font.BOLD, 22);
Font fn2 = new Font("宋體", Font.BOLD, 20);
int x = 100;
int y = 100;
int[] pox ={ 90, 100, 100 };
int[] poy ={ 110, 90, 100 };
int[] poxx ={ 100, 100, 110 };
int[] poyy ={ 90, 90, 110 };

int[] poxB = {687,697,707};
int[] poyB = {690,700,700};
int[] poxBB = {687,697,707};
int[] poyBB = {710,700,700};

public test8888()
{
setSize(900, 900);

}

public void paint(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
g2d.fillRect(99, 100, 2, 600);
g2d.fillRect(99, 700, 600, 2);
g2d.fillRect(100, 685, 15, 15);
for (int i = 0; i < 37; i++)
{
g2d.drawLine(100 + i * 15, 600 + y, y + i * 15, y + 45);
g2d.drawLine(100, 600 + y - i * 15, y + 555, y + 600 - i * 15);
g2d.drawString("0", x - 20, 720);
if (i % 2 == 0 && i / 2 != 0)
{
g2d.drawString(String.valueOf(i / 2), x - 20, 705 - i / 2 * 30);
g2d.drawString(String.valueOf(i / 2), x - 5 + i / 2 * 30, 720);
}
}
g2d.setFont(fn2);
g2d.setColor(Color.white);
g2d.drawString("A", 102, 700);
g2d.setFont(fn);
g2d.setColor(Color.black);
g2d.drawString("Y", 80, 140);
g2d.drawString("X", 670, 720);

g2d.fillPolygon(pox,poy,3);
g2d.fillPolygon(poxx,poyy,3);

g2d.fillPolygon(poxB,poyB,3);
g2d.fillPolygon(poxBB,poyBB,3);
g2d.dispose();

}

public static void main(String[] args)
{
JFrame jf = new JFrame();
jf.setSize(900, 900);
jf.setVisible(true);
jf.setDefaultCloseOperation(3);
jf.getContentPane().add(new test8888());
}
}

③ JAVA直角坐標系中,兩點的中點及長度距離。

public class Point {

private double x;//橫坐標x
private double y;//縱坐標y

public Point() {//默認構造為(0,0)
this(0.0D, 0.0D);
}

public Point(double xCoordinate, double yCoordinate){//帶參數構造
this.x = xCoordinate;
this.y = yCoordinate;
}

public static void main(String[] args) {

Point p1 = new Point(1, 1);//點1
Point p2 = new Point(2, 3);//點2

System.out.println("Point 1 is: " + p1.toString());//輸出點1坐標
System.out.println("Point 2 is: " + p2.toString());//輸出點2坐標

System.out.println("Midpoint of point 1 and point 2 is: " + p1.getMidpoint(p2).toString());//中點坐標
System.out.println("Distance between point 1 and point 2 is: " + p1.getDistance(p2));//2點之間距離

}

// x = (x1 + x2)/ 2 , y = (y1 +y2)/2
public Point getMidpoint(Point p1){
Point midPoint = new Point();
midPoint.x = (this.getX() + p1.getX()) / 2;
midPoint.y = (this.getY() + p1.getY()) /2;

return midPoint;
}

//∣AB∣=√[(X1- X2)^2+(Y1- Y2)^2]= √(1+k2) ∣X1 -X2∣
public double getDistance(Point p1){
double xSqure = Math.pow(this.x - p1.getX(), 2);
double ySqure = Math.pow(this.y - p1.getY(), 2);

return Math.sqrt(xSqure + ySqure);
}

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 String toString(){//string輸出
return "(x, y): (" + this.x + ", " + this.y + ")";
}
}

-----------程序運行結果
Point 1 is: (x, y): (1.0, 1.0)
Point 2 is: (x, y): (2.0, 3.0)
Midpoint of point 1 and point 2 is: (x, y): (1.5, 2.0)
Distance between point 1 and point 2 is: 2.23606797749979

④ JAVA編程在坐標系中的運算

public class Perimeter {

public static void main(String args[]) {
// 三個圓的坐標
int x1=1;
int y1=1;
int x2=2;
int y2=2;
int x3=3;
int y3=4;
// Math.sqrt是求平方根,trianglePerimeter就是三角形的周長
double trianglePerimeter = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
+Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2))
+Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
// 假設半徑是5.0
double radius = 5.0;
// 圓的周長跟坐標沒關系,Math.PI就是數學中的圓周率常數3.1415926。。。
double circularPerimeter = Math.PI*2*radius;
}
}

⑤ java實現對應的坐標系轉換,從wgs84坐標系轉換成墨卡托坐標

若是獨立坐標系,無轉換參數是無法取得wgs84坐標的。
若是wgs84的坐標的空間直角坐標的形式,你可以從網上coord轉換,圖標是一個小笑臉,坐標轉換應當可以搜到。
補充:既然是兩組10位數字,那肯定就不是經緯度咯,建議你先了解一下坐標系統。
我國目前常用的坐標系有北京54坐標系,西安80坐標系以及WGS84。

⑥ 如何用java畫一個坐標系,帶x,y軸

你是要畫什麼圖形?
我這有個畫圓的代碼:
package com.java;

import javax.swing.*;
import java.awt.*;

public class demo9 extends JFrame{
Mypanel mypanel=null;
public static void main(String args[]){

demo9 a = new demo9();

}
public demo9(){
mypanel = new Mypanel();
this.add(mypanel);
this.setSize(400,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

class Mypanel extends JPanel{
public void paint(Graphics g){
g.drawOval(30, 30, 50, 50);//30,30是代表圓心的位置。50,50是半徑。(如果你把50,50改成2個不相等的就是話橢圓),具體畫別的圖形你可以參照Graphics類,其中用的多的還有drawLine(畫直線)和drawRect(畫矩形)的方法
}
}
}

補充:首先你要理解drawLine(x1,x2,y1,y2);中是畫坐標坐標(x1,y1)到(x2,y2)的點~。那麼你畫幾個線段的組合你就可以多畫幾次。就拿你說畫1<x<=3是Y=1;1<X<=6,Y=5的兩條直線你就可以看做是畫點(1,1)到點(3,1)的直線。你就drawline(1,3,1,1);第二條就drawLine(1,5,6,5)就可以了~~還有就是JAVA的畫圖板是以畫板的左上角為起點的,向電腦屏幕,x是右邊增加,Y是往下面增加。。畫直線的單位是像素為單位的,(假如你電腦顯示屏解析度是800*600的話,那麼你X軸就有800個像素,Y就有600個)

⑦ java 建立直角坐標系

//不能用的話肯定是坐標不對
//把mouseMoved方法里的注釋去掉重新獲取
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class MouseHelp extends javax.swing.JPanel implements MouseMotionListener {
private JButton textButton;
Robot robot;
/**
* Auto-generated main method to display this
* JPanel inside a new JFrame.
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MouseHelp());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

public MouseHelp() {
super();
initGUI();
}

private void initGUI() {

try {
robot=new Robot();
addMouseMotionListener(this);
setPreferredSize(new Dimension(400, 300));
this.setLayout(null);
{
textButton = new JButton();
this.add(textButton);
textButton.setText("\u8fd0 \u884c");
textButton.setBounds(136, 72, 127, 22);
textButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
robot.mouseMove(30,745);
try {
Thread.sleep(1000);
robot.mousePress(MouseEvent.BUTTON1_MASK);
robot.mouseRelease(MouseEvent.BUTTON1_MASK);
Thread.sleep(1000);
robot.mouseMove(150,481);
robot.mousePress(MouseEvent.BUTTON1_MASK);
robot.mouseRelease(MouseEvent.BUTTON1_MASK);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}});
}
} catch (Exception e) {
e.printStackTrace();
}
}

public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
//從這里獲取滑鼠的全局坐標
//Point mousepoint = MouseInfo.getPointerInfo().getLocation();
//System.out.println(mousepoint.x+"\t"+mousepoint.y);
}

}

⑧ 在java中在上下文的坐標系統什麼意思

計算機屏幕擁有坐標系統,它被用來定義窗口的位置和尺寸。同樣,Java中的可視化組件都有自己的一個坐標系,即以組件的左上角為原點,向下和向右分別為Y軸和X軸的正方向。我們可以將一個點求出其在不同坐標系下的值。通過下面的方法即可達到:SwingUtilities.convertPoint方法。在Component類中還有一個方法:contains

public boolean contains(Point p)

檢查組件是否「包含」指定的點,其中該點的 x 和 y 坐標是相對於此組件的坐標系定義的值得注意的是所制定包含的點是相對於該組件的坐標系中定義的點。假若一個JButton是位於JFrame中,視覺上在JButton中的一個點,但是其值是相對於JFrame坐標系的,因此在數值上若將該點放到JButton坐標系中,則他可能不位於JButton中了。

右鍵菜單JPopupMenu使用要領:

首先定義一個JPopupMenu對象pop,然後添加需要的JMenuItem到pop中。

isPopupTrigger

public boolean isPopupTrigger(MouseEvent e)

如果 JPopupMenu 的當前安裝 UI 將 MouseEvent 視為彈出菜單觸發器,則返回 true。

返回: 如果滑鼠事件為彈出菜單觸發器,則返回 true

show

public void show(Component invoker, int x, int y)

在組件調用者的坐標空間中的位置 X、Y 顯示彈出菜單。

參數: invoker - 彈出菜單在其空間中顯示的組件 x - 用於顯示彈出菜單的調用者的坐標空間中的 X 坐標 y - 用於顯示彈出菜單的調用者的坐標空間中的 Y 坐標

需要注意的是:哪個組件添加了該JPopupMenu,該pop就在哪個組件的坐標系中的范圍上顯示。假如你在一個JButton上添加了pop那麼若pop比JButton要大的話,pop的一些菜單項可能顯示不出來。一般我們將pop會添加到頂層的容器中。若要右鍵其中的組件就彈出菜單時,則可以採用上面所說的方法:在組件中添加一個滑鼠監聽器,一般右鍵菜單是右鍵釋放的時候觸發,因此在mouseReleased方法中添加處理事件的程序。首先得到在組件坐標系下的滑鼠點擊的坐標點,再求出該坐標值在容器坐標系下對應的坐標點。並在容器中的該位置顯示右鍵菜單即可

⑨ java定義一個點類Point, 具備坐標系中的橫坐標x, 和縱坐標y, 並實現如下功能:

Point類


publicclassPoint{
privatefloatx;
privatefloaty;
publicPoint(floatx,floaty){
this.x=x;
this.y=y;
}

publicfloatgetX(){
returnx;
}

publicvoidsetX(floatx){
this.x=x;
}

publicfloatgetY(){
returny;
}

publicvoidsetY(floaty){
this.y=y;
}

publicfloatdistanceToOrigin(){
return(float)Math.sqrt(Math.pow(this.x,2)+Math.pow(this.y,2));
}

publicfloatdistanceToOther(intx,inty){
return(float)Math.sqrt(Math.pow(this.x-x,2)+Math.pow(this.y-y,2));
}

publicfloatdistanceToOther(Pointpoint){
return(float)Math.sqrt(Math.pow(this.x-point.x,2)+Math.pow(this.y-point.y,2));
}
}

測試類

publicclassTestPoint{
publicstaticvoidmain(String[]args){
Pointp1=newPoint(3,5);
Pointp2=newPoint(7,8);
System.out.println(p1.distanceToOrigin());
System.out.println(p2.distanceToOrigin());
System.out.println(p1.distanceToOther(20,30));
System.out.println(p2.distanceToOther(20,30));
System.out.println(p1.distanceToOther(p2));
}
}
閱讀全文

與java坐標系相關的資料

熱點內容
fortran把文件放入文件夾 瀏覽:707
程序員1年經驗不敢投簡歷 瀏覽:481
如何看電腦的源碼 瀏覽:896
找工作app軟體哪個好 瀏覽:96
信息管理網站源碼 瀏覽:439
小說app哪個好免費 瀏覽:224
域名在線加密 瀏覽:146
軟體編程西安交大 瀏覽:453
是不是串貨的奶粉查不到溯源碼的 瀏覽:825
北京dns伺服器雲主機 瀏覽:221
openldaplinux安裝 瀏覽:23
java取月的最後一天 瀏覽:10
騰訊雲伺服器多久退款 瀏覽:949
微信廣告植入系統源碼 瀏覽:922
一年級語文上冊pdf 瀏覽:315
好久不見app干什麼用的 瀏覽:143
壓縮包解壓碼對方可以更改嗎 瀏覽:256
pdf電子書製作軟體 瀏覽:888
數控三通編程 瀏覽:300
linux多終端 瀏覽:811