Total members 10261 | Gratitudes |It is currently Tue May 22, 2012 4:52 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: display Coordinates of mouse in java
PostPosted: Tue Nov 11, 2008 9:03 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

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 5593 times ]

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )
TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 video chat application in java
 navigating to database using java
 java mobile apps
 need help in java
 Read your gmail using Java code
 Java Programing to communicating port parallel
 Java Chat
 Steganography in java
 java code for listing folder contents from remote folder
 java

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 3 guests



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team