Switch to full style
General Java code examples
Post a reply

display Coordinates of mouse in java

Tue Nov 11, 2008 9:03 pm

draw Coordinates of mouse in java
Code:
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 < 0 ||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(450, 100);
    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(0, 0, getWidth(), getHeight());
        g.setColor(Color.GRAY);
        drawGrid(g, 22);
        if (point != null) {
        g.setColor(getForeground());
        g.fillRect(point.x - 4, point.y - 4, 8, 8);
      }
    }
  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) {
    }
  }
}



Attachments
showCoo1.gif
java Coordinates display
showCoo1.gif (7.54 KiB) Viewed 13375 times

Post a reply
  Related Posts  to : display Coordinates of mouse in java
 Need Java program for display the CPU usage     -  
 Mouse position in C++     -  
 SEVEN SEGMENT DISPLAY     -  
 display list     -  
 hi i need ppt for space mouse.     -  
 Here is how to display any 2d array     -  
 Display images on jsp from servlet     -  
 Can I display same vertically on right side?     -  
 Get Mouse Position using JQuery     -  
 Display a different image for each day of the week     -  

Topic Tags

Java Events