Show Coordinates

In this section, we are going to display a frame where we have draw the grid.

Show Coordinates

In this section, we are going to display a frame where we have draw the grid.

Show Coordinates

Show Coordinates

     

In this section, we are going to display a frame where we have draw the grid. As the user move or click the cursor with the mouse over the grid, a label displays the location of cursor,  i.e., shows coordinates with respect to x-axis and y-axis.

The class Point  shows a location with respect to x-axis and y-axis. The BoxLayout.PAGE_AXIS displays the components in the direction that lines flow across a page. It displays the location of cursor at the end of frame. The Component.RIGHT_ALIGNMENT displays the location of cursor at the right side.

The label.setText(msg) sets the message. Then set up the window and set up the content pane. The f.show() displays the frame.

 A collection of utility methods have been defined by the class SwingUtilities. The method invokeLater() executes the run() on the AWT event dispatching thread and all the AWT events have been processed. 

The method drawGrid() draws a 22 x 22 grid. The class MouseEvent is used to perform  mouse events (click, enter, exit) and mouse motion events (moves and drags).

Following code draws the vertical lines of the grid:

int x = X1;
while (x < X2) {
g.drawLine(x, Y1, x, Y2);
x += grid;
}

Following code draws the horizontal lines  of the grid:

int y = Y1;
while (y < Y2) {
g.drawLine(X1, y, X2, y);
y += grid;
}

Here is the code of ShowCoordinates.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import javax.swing.event.MouseInputListener;

public class ShowCoordinates{
  private JLabel label;
  private Point point1, point2;
  private void build(Container cont) {
  cont.setLayout(new BoxLayout(cont, BoxLayout.PAGE_AXIS));
  CoordinateExample coordinate = new CoordinateExample(this);
  cont.add(coordinate);

  label = new JLabel();
  resetLabel();
  cont.add(label);
  coordinate.setAlignmentX(Component.RIGHT_ALIGNMENT);
  label.setAlignmentX(Component.RIGHT_ALIGNMENT);
  }
  public void update(int x, int y) {
  if (x < ||y < 0) {
  point2 = null;
  updateLabel();
  return;
  }
  if (point2 == null) {
  point2 = new Point();
  }
  point2.x = x;
  point2.y = y;
  updateLabel();
  }
  public void updatePoint1(Point pt) {
  point1 = pt;
  updateLabel();
  }
  public void resetLabel() {
  point2 = null;
  updateLabel();
  }
  protected void updateLabel() {
  String msg = "";
  if ((point1 == null) && (point2 == null)) {
  msg = "You can move the cursor within the framed region.";
  else {
  if (point2 != null) {
  msg += "The coordinates are (" + point2.x + ", "
  + point2.y + "). ";
  }
  }
  label.setText(msg);
  }
  private static void create() {
  JFrame f = new JFrame("Show Coordinates");
  ShowCoordinates showCoo = new ShowCoordinates();
  showCoo.build(f.getContentPane());
  f.pack();
  f.show();
  }
  public static void main(String[] args) {
 javax.swing.SwingUtilities.invokeLater(new Runnable() {
  public void run() {
  create();
  }
  });
  }
  public static class CoordinateExample extends JComponent implements
  MouseInputListener {
  Point point = null;
  ShowCoordinates showCoo;
  Dimension dim = new Dimension(450100);
  Color color;

  public CoordinateExample(ShowCoordinates showCoo) {
  this.showCoo = showCoo;
  addMouseListener(this);
  addMouseMotionListener(this);
  setBackground(Color.WHITE);
  setOpaque(true);
  }
 public Dimension getPreferredSize() {
  return dim;
  }
 protected void paintComponent(Graphics g) {
  g.setColor(getBackground());
  g.fillRect(00, getWidth(), getHeight());
  g.setColor(Color.GRAY);
  drawGrid(g, 22);
  if (point != null) {
  g.setColor(getForeground());
  g.fillRect(point.x - 4, point.y - 488);
  }
  }
  private void drawGrid(Graphics g, int grid) {
  Insets insets = getInsets();
  int X1 = insets.left;
  int Y1 = insets.top;
  int X2 = getWidth() - insets.right;
  int Y2 = getHeight() - insets.bottom;
  int x = X1;
  while (x < X2) {
  g.drawLine(x, Y1, x, Y2);
  x += grid;
  }
  int y = Y1;
  while (y < Y2) {
  g.drawLine(X1, y, X2, y);
  y += grid;
  }
  }
  public void mouseClicked(MouseEvent event) {
  int x = event.getX();
  int y = event.getY();
  if (point == null) {
  point = new Point(x, y);
  else {
  point.x = x;
  point.y = y;
  }
  showCoo.updatePoint1(point);
  repaint();
  }
  public void mouseMoved(MouseEvent event) {
  showCoo.update(event.getX(), event.getY());
  }
  public void mouseExited(MouseEvent event) {
  showCoo.resetLabel();
  }
  public void mouseReleased(MouseEvent event) {
  }
  public void mouseEntered(MouseEvent event) {
  }
  public void mousePressed(MouseEvent event) {
  }
  public void mouseDragged(MouseEvent event) {
  }
  }
}

If you haven't click or move the cursor over the grid, output will be displayed as:

If you have click or move the cursor over the grid, output will be displayed as:

Download Source Code