Total members 11895 |It is currently Tue Dec 03, 2024 5:44 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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:
File comment: java Coordinates display
showCoo1.gif
showCoo1.gif [ 7.54 KiB | Viewed 13398 times ]

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada
Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  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 a different image for each day of the week     -  
 How can i display these items in Listview in C#?     -  
 Display images on jsp from servlet     -  
 Can I display same vertically on right side?     -  



Topic Tags

Java Events






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com