Coordinate Demo : Transform « 2D Graphics GUI « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 2D Graphics GUI » TransformScreenshots 
Coordinate Demo
Coordinate Demo

/*
 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * -Redistribution of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 *
 * -Redistribution in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */
/* CoordinatesDemo.java is a 1.4 application that requires no other files. */

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.event.MouseInputListener;

/*
 * This displays a framed area. As the user moves the cursor over the area, a
 * label displays the cursor's location. When the user clicks, the area displays
 * a 7x7 dot at the click location.
 */
public class CoordinatesDemo {
  private JLabel label;

  private Point clickPoint, cursorPoint;

  private void buildUI(Container container) {
    container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));

    CoordinateArea coordinateArea = new CoordinateArea(this);
    container.add(coordinateArea);

    label = new JLabel();
    resetLabel();
    container.add(label);

    //Align the left edges of the components.
    coordinateArea.setAlignmentX(Component.LEFT_ALIGNMENT);
    label.setAlignmentX(Component.LEFT_ALIGNMENT)//redundant
  }

  public void updateCursorLocation(int x, int y) {
    if (x < || y < 0) {
      cursorPoint = null;
      updateLabel();
      return;
    }

    if (cursorPoint == null) {
      cursorPoint = new Point();
    }

    cursorPoint.x = x;
    cursorPoint.y = y;
    updateLabel();
  }

  public void updateClickPoint(Point p) {
    clickPoint = p;
    updateLabel();
  }

  public void resetLabel() {
    cursorPoint = null;
    updateLabel();
  }

  protected void updateLabel() {
    String text = "";

    if ((clickPoint == null&& (cursorPoint == null)) {
      text = "Click or move the cursor within the framed area.";
    else {

      if (clickPoint != null) {
        text += "The last click was at (" + clickPoint.x + ", "
            + clickPoint.y + "). ";
      }

      if (cursorPoint != null) {
        text += "The cursor is at (" + cursorPoint.x + ", "
            + cursorPoint.y + "). ";
      }
    }

    label.setText(text);
  }

  /**
   * Create the GUI and show it. For thread safety, this method should be
   * invoked from the event-dispatching thread.
   */
  private static void createAndShowGUI() {
    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);

    //Create and set up the window.
    JFrame frame = new JFrame("CoordinatesDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    CoordinatesDemo controller = new CoordinatesDemo();
    controller.buildUI(frame.getContentPane());

    //Display the window.
    frame.pack();
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }

  public static class CoordinateArea extends JComponent implements
      MouseInputListener {
    Point point = null;

    CoordinatesDemo controller;

    Dimension preferredSize = new Dimension(40075);

    Color gridColor;

    public CoordinateArea(CoordinatesDemo controller) {
      this.controller = controller;

      //Add a border of 5 pixels at the left and bottom,
      //and 1 pixel at the top and right.
      setBorder(BorderFactory.createMatteBorder(1551, Color.RED));

      addMouseListener(this);
      addMouseMotionListener(this);
      setBackground(Color.WHITE);
      setOpaque(true);
    }

    public Dimension getPreferredSize() {
      return preferredSize;
    }

    protected void paintComponent(Graphics g) {
      //Paint background if we're opaque.
      if (isOpaque()) {
        g.setColor(getBackground());
        g.fillRect(00, getWidth(), getHeight());
      }

      //Paint 20x20 grid.
      g.setColor(Color.GRAY);
      drawGrid(g, 20);

      //If user has chosen a point, paint a small dot on top.
      if (point != null) {
        g.setColor(getForeground());
        g.fillRect(point.x - 3, point.y - 377);
      }
    }

    //Draws a 20x20 grid using the current color.
    private void drawGrid(Graphics g, int gridSpace) {
      Insets insets = getInsets();
      int firstX = insets.left;
      int firstY = insets.top;
      int lastX = getWidth() - insets.right;
      int lastY = getHeight() - insets.bottom;

      //Draw vertical lines.
      int x = firstX;
      while (x < lastX) {
        g.drawLine(x, firstY, x, lastY);
        x += gridSpace;
      }

      //Draw horizontal lines.
      int y = firstY;
      while (y < lastY) {
        g.drawLine(firstX, y, lastX, y);
        y += gridSpace;
      }
    }

    //Methods required by the MouseInputListener interface.
    public void mouseClicked(MouseEvent e) {
      int x = e.getX();
      int y = e.getY();
      if (point == null) {
        point = new Point(x, y);
      else {
        point.x = x;
        point.y = y;
      }
      controller.updateClickPoint(point);
      repaint();
    }

    public void mouseMoved(MouseEvent e) {
      controller.updateCursorLocation(e.getX(), e.getY());
    }

    public void mouseExited(MouseEvent e) {
      controller.resetLabel();
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
    }
  }
}

           
       
Related examples in the same category
1. Transform DemoTransform Demo
2. Transform ShearTransform Shear
3. Transform ScaleTransform Scale
4. Transform Rotation Translation Transform Rotation Translation
5. Transforme Rotation demoTransforme Rotation demo
6. Transform Translation and RotationTransform Translation and Rotation
7. Transform Translated RotationTransform Translated Rotation
8. Transform TranslationTransform Translation
9. Line transformation, rotation, shear,scale Line transformation, rotation, shear,scale
10. AffineTransform demoAffineTransform demo
11. Transform TestTransform Test
w__ww.___ja_va_2__s_.c__o___m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.