java输入点的坐标,输出点在什么位置

java输入点的坐标,输出点在什么位置,第1张

在Java中,。具体来说,我们需要利用数学中的坐标系知识,将点的坐标转化为具体的位置信息。

首先,我们需要了解二维坐标系的基本知识。二维坐标系由两条垂直的轴组成,分别为x轴和y轴。在这个坐标系中,每个点都可以用一个有序对(x,y)来表示Java可以通过输入点的坐标来判断该点的位置。在二维平面上,坐标系通常以x轴和y轴为基准。如果一个点的x坐标为正且y坐标为正,那么该点位于第一象限。如果x坐标为负且y坐标为正,那么该点位于第二象限。如果x坐标为负且y坐标为负,那么该点位于第三象限。如果x坐标为正且y坐标为负,那么该点位于第四象限。如果x坐标为0且y坐标为0,那么该点位于坐标原点。因此,通过输入点的坐标,可以轻松地判断该点的位置。其中x表示点在x轴上的位置,y表示点在y轴上的位置。

接下来,我们可以根据点的坐标位置,将其分为以下几种情况:

1 如果点的x坐标和y坐标均为正数,那么它位于第一象限。在这个象限中,x和y坐标均为正数,因此这个点位于坐标系的右上方。

2 如果点的x坐标为负数,而y坐标为正数,那么它位于第二象限。在这个象限中,x坐标为负数,而y坐标为正数,因此这个点位于坐标系的左上方。

3 如果点的x坐标和y坐标均为负数,那么它位于第三象限。在这个象限中,x和y坐标均为负数,因此这个点位于坐标系的左下方。

4 如果点的x坐标为正数,而y坐标为负数,那么它位于第四象限。在这个象限中,x坐标为正数,而y坐标为负数,因此这个点位于坐标系的右下方。

总之,通过输入点的坐标,我们可以轻松地判断它在二维坐标系中的具体位置。这种方法可以广泛应用于数学、计算机图形学等领域。

//提示:坐标依次打印在命令符窗口

//提示:坐标依次打印在命令符窗口

//提示:坐标依次打印在命令符窗口

//不就是监听鼠标事件吗?

import javaxswing;

import javaawt;

import javaawtevent;

import javaxswingborderBorder;

/

我想建立个界面,可以加载本机中。

加载后可以通过鼠标点击获得上任意点坐标。

提问者: sunny929929 - 试用期 一级

/

public class MyPicture extends JFrame implements MouseListener{

private JLabel tipLabel;

/

main()

/

public static void main(String[] args){

MyPicture frame = new MyPicture();

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

framesetVisible(true);

}

/

constructor

/

public MyPicture(){

setSize(800, 600);//根据要求调整大小

setLocation(100,100);

setTitle("获得上任意点坐标");

setResizable(false);

Container con=getContentPane();

ImageIcon bgIcon=new ImageIcon("bgpicjpg");//注意的路径

ImagePanel backpicPanel=new ImagePanel(bgIcon);

backpicPaneladdMouseListener(this);

conadd(backpicPanel,BorderLayoutCENTER);

tipLabel=new JLabel("--------------------提示:坐标依次打印在屏幕上!--------------------");

conadd(tipLabel,BorderLayoutSOUTH);

}

/

/

public void mousePressed(MouseEvent e){

int x=egetX();

int y=egetY();

String message="("+x+","+y+")";

tipLabelsetText(message);

Systemoutprintln(message);

}

public void mouseReleased(MouseEvent e){

}

public void mouseEntered(MouseEvent e){

}

public void mouseExited(MouseEvent e){

}

public void mouseClicked(MouseEvent e){

}

}

/

类ImagePanel,用于添加背景

/

class ImagePanel extends JPanel{

private Image img;

public ImagePanel (ImageIcon imageIcon){

img=imageIcongetImage();

}

public void paintComponent(Graphics g){

superpaintComponent(g);

gdrawImage(img,0,0,this);

}

}

//你运行一下,看在你的电脑上能不能用。

//不能用的话肯定是坐标不对

//把mouseMoved方法里的注释去掉重新获取

import javaawtDimension;

import javaawtMouseInfo;

import javaawtPoint;

import javaawtRobot;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaawteventMouseEvent;

import javaawteventMouseMotionListener;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingWindowConstants;

public class MouseHelp extends javaxswingJPanel 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();

framegetContentPane()add(new MouseHelp());

framesetDefaultCloseOperation(WindowConstantsDISPOSE_ON_CLOSE);

framepack();

framesetVisible(true);

}

public MouseHelp() {

super();

initGUI();

}

private void initGUI() {

try {

robot=new Robot();

addMouseMotionListener(this);

setPreferredSize(new Dimension(400, 300));

thissetLayout(null);

{

textButton = new JButton();

thisadd(textButton);

textButtonsetText("\u8fd0 \u884c");

textButtonsetBounds(136, 72, 127, 22);

textButtonaddActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

robotmouseMove(30,745);

try {

Threadsleep(1000);

robotmousePress(MouseEventBUTTON1_MASK);

robotmouseRelease(MouseEventBUTTON1_MASK);

Threadsleep(1000);

robotmouseMove(150,481);

robotmousePress(MouseEventBUTTON1_MASK);

robotmouseRelease(MouseEventBUTTON1_MASK);

} catch (InterruptedException e1) {

// TODO Auto-generated catch block

e1printStackTrace();

}

}});

}

} catch (Exception e) {

eprintStackTrace();

}

}

public void mouseDragged(MouseEvent e) {

// TODO Auto-generated method stub

}

public void mouseMoved(MouseEvent e) {

// TODO Auto-generated method stub

//从这里获取鼠标的全局坐标

//Point mousepoint = MouseInfogetPointerInfo()getLocation();

//Systemoutprintln(mousepointx+"\t"+mousepointy);

}

}

JLabelsetText(String text)可以设置标签的显示文本内容。

MouseEvent(鼠标事件)有两个方法:getX();getY();可以获得mouse的坐标。

定义一个类,implements mouseListener,mouseMotionListener就能ok了

如:

import javaawt;

import javaawtevent;

import javaxswing;

public class Mousetracker extends JFrame implements MouseListener,MouseMotionListener //实现两个鼠标事件接口 ,

{

private JLabel statusBar; //定义一个标签

public MouseTracker() //构造方法

{

super("Demonstrating mouse event"); //调用父窗口构造方法

statusBar=new JLabel();

getContentPane()add(statusBar,BorderLayoutSOUTH); //获得上下文面板,并添加组件

addMouseListener(this); //绑定事件监听器,因为本身实现了监听器接口,因此用this,即本身

addMouseMotionListener(this);

setSize(275,100);

setVisible(true);

} //以下是实现鼠标的各种事件,如按下,d起,拖拽等

public void mouseClicked(MouseEvent event)

{

statuxBarsetText("clicked at ["+eventgetX()+

","+eventgetY()+"]");

}

public void mousePressed(MouseEvent event)

{

statusBarsetText("Pressed at["+eventgetX()+

","+eventgetY()+"]");

}

public void mouseReleased(Mouseevent event)

{

statusBarsetText("Relessed at["+eventgetX()+

","+eventgetY()+"]");

}

public void mouseDragged(Mouseevent event)

{

statusBarsetText("Dragged at["+eventgetX()+

","+eventgetY()+"]");

}

public void mouseMoved(Mouseevent event)

{

statusBarsetText("Moved at["+eventgetX()+

","+eventgetY()+"]");

}

public static void main(String args[])

{

MouseTracker application=new MouseTracker();

applicationsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE); //设置用户在此窗体上发起 "close" 时默认执行的 *** 作

}

}

package comwztest1;

import javaawt;

import javaawtevent;

import javaxswing;

public class Painter extends JFrame {

private int pointCount = 0;

// array of 1000 javaawtPoint references

private Point points[] = new Point[ 1000 ];

// set up GUI and register mouse event handler

public Painter()

{

super( "A simple paint program" );

// create a label and place it in SOUTH of BorderLayout

getContentPane()add( new JLabel( "Drag the mouse to draw" ),

BorderLayoutSOUTH );

addMouseMotionListener(

new MouseMotionAdapter() { // anonymous inner class

// store drag coordinates and repaint

public void mouseDragged( MouseEvent event )

{

if ( pointCount < pointslength ) {

points[ pointCount ] = eventgetPoint();

++pointCount;

repaint();

}

}

} // end anonymous inner class

); // end call to addMouseMotionListener

setSize( 300, 150 );

setVisible( true );

} // end Painter constructor

// draw oval in a 4-by-4 bounding box at specified location on window

public void paint( Graphics g )

{

superpaint( g ); // clears drawing area

for ( int i = 0; i < pointslength && points[ i ] != null; i++ )

gfillOval( points[ i ]x, points[ i ]y, 4, 4 );

}

public static void main( String args[] )

{

Painter application = new Painter();

applicationsetDefaultCloseOperation( JFrameEXIT_ON_CLOSE );

}

} // end class Painter

Java里面有个ToolKit的现成的截屏的东西,截出来放在BufferImage里想咋处理都行哇,不过这取到的可能是全屏,在缓冲区里可以以字节指定位置么,不过算法要自己写,应该也不会太难的,就是从一个大的字节缓冲区中,一小段一小段的根据偏移点和结束点取一行一行的字节,一个矩形区域可以由左上角一点到右下角一点决定,把这些字节拼成一个小的图像。要是不想自己写算法的话,网上一大堆呢,不过都是C代码,自己照猫画虎地写呗!

以上就是关于java输入点的坐标,输出点在什么位置全部的内容,包括:java输入点的坐标,输出点在什么位置、java 如何载入本机图片并实现点击获得任意点坐标、java获取鼠标坐标等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9536270.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存