Co-ordinates
从
TOP LEFT SIDE
屏幕的此处开始,随着的增加
X
,您将向前进
RIGHTSIDE
,而随着的增加
Y
,您将向前进
DOWNWARDS
。这是一个小的示例程序,您可以通过单击它的任意位置更好地理解它。
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class DrawingExample{ private int x; private int y; private String text; private Drawingbase canvas; private void displayGUI() { Jframe frame = new Jframe("Drawing Example"); frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); canvas = new Drawingbase(); canvas.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { text = "X : " + me.getX() + " Y : " + me.getY(); x = me.getX(); y = me.getY(); canvas.setValues(text, x, y); } }); frame.setContentPane(canvas); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new DrawingExample().displayGUI(); } }); }}class Drawingbase extends JPanel{ private String clickedAt = ""; private int x = 0; private int y = 0; public void setValues(String text, int x, int y) { clickedAt = text; this.x = x; this.y = y; repaint(); } public Dimension getPreferredSize() { return (new Dimension(500, 400)); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString(clickedAt, x, y); }}