Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-24-2008, 12:16 AM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Mouse drag and drop to draw
Code:
import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.awt.Container; import java.awt.Cursor; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.Rectangle; import java.awt.Shape; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.TitledBorder; public class Ellipse extends JFrame { DrawingCanvas canvas; JLabel location; public Ellipse() { super(); Container container = getContentPane(); canvas = new DrawingCanvas(); container.add(canvas); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(1, 2)); panel.add(new JLabel("x,y: ", JLabel.RIGHT)); location = new JLabel(""); panel.add(location); container.add(panel, BorderLayout.SOUTH); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setSize(600,300); setVisible(true); } public static void main(String arg[]) { new Ellipse(); } class DrawingCanvas extends JPanel { double x, y, w, h; int x1, y1, x2, y2; Ellipse2D ellipse; Ellipse2D selectedShape; Rectangle2D boundingRec; Cursor curCursor; public DrawingCanvas() { x = 20; y = 20; w = 100; h = 75; setBackground(Color.white); addMouseListener(new MyMouseListener()); addMouseMotionListener(new MyMouseMotionListener()); } public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; ellipse = new Ellipse2D.Double(x, y, w, h); g2D.draw(ellipse); if (boundingRec != null) { drawHighlightSquares(g2D, boundingRec); } if (curCursor != null) setCursor(curCursor); } public void drawHighlightSquares(Graphics2D g2D, Rectangle2D r) { double x = r.getX(); double y = r.getY(); double w = r.getWidth(); double h = r.getHeight(); g2D.setColor(Color.black); g2D.fill(new Rectangle.Double(x - 3.0, y - 3.0, 6.0, 6.0)); g2D .fill(new Rectangle.Double(x + w * 0.5 - 3.0, y - 3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x + w - 3.0, y - 3.0, 6.0, 6.0)); g2D .fill(new Rectangle.Double(x - 3.0, y + h * 0.5 - 3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x + w - 3.0, y + h * 0.5 - 3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x - 3.0, y + h - 3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x + w * 0.5 - 3.0, y + h - 3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x + w - 3.0, y + h - 3.0, 6.0, 6.0)); } class MyMouseListener extends MouseAdapter { public void mousePressed(MouseEvent e) { if (ellipse.contains(e.getX(), e.getY())) { selectedShape = ellipse; if (boundingRec != null) boundingRec = ellipse.getBounds2D(); displayParameters(selectedShape); } else { boundingRec = null; location.setText(""); } canvas.repaint(); x1 = e.getX(); y1 = e.getY(); } public void mouseReleased(MouseEvent e) { if (ellipse.contains(e.getX(), e.getY())) { boundingRec = ellipse.getBounds2D(); selectedShape = ellipse; displayParameters(selectedShape); } canvas.repaint(); } public void mouseClicked(MouseEvent e) { if (ellipse.contains(e.getX(), e.getY())) { selectedShape = ellipse; boundingRec = ellipse.getBounds2D(); displayParameters(selectedShape); } else { if (boundingRec != null) boundingRec = null; location.setText(""); } canvas.repaint(); } } class MyMouseMotionListener extends MouseMotionAdapter { public void mouseDragged(MouseEvent e) { if (ellipse.contains(e.getX(), e.getY())) { boundingRec = null; selectedShape = ellipse; x2 = e.getX(); y2 = e.getY(); x = x + x2 - x1; y = y + y2 - y1; x1 = x2; y1 = y2; } if (selectedShape != null) displayParameters(selectedShape); canvas.repaint(); } public void mouseMoved(MouseEvent e) { if (ellipse != null) { if (ellipse.contains(e.getX(), e.getY())) { curCursor = Cursor .getPredefinedCursor(Cursor.HAND_CURSOR); } else { curCursor = Cursor.getDefaultCursor(); } } canvas.repaint(); } } public void displayParameters(Shape shape) { double x = selectedShape.getX(); double y = selectedShape.getY(); double w = selectedShape.getWidth(); double h = selectedShape.getHeight(); String locString = "(" + Double.toString(x) + "," + Double.toString(y) + ")"; String sizeString = "(" + Double.toString(w) + "," + Double.toString(h) + ")"; location.setText(locString); } } }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Undo Folder Drag and Drop natel Eclipse 0 04-09-2008 12:01 AM
Drag and drop abhivenugopal JavaServer Pages (JSP) and JSTL 0 01-30-2008 03:10 PM
help drag and drop in JTabbedPane RO86 AWT / Swing 0 08-14-2007 02:22 PM
help with drag and drop on JList ysl530 AWT / Swing 3 08-13-2007 06:38 PM
How to mouse-drag a JWindow? cruxblack New To Java 3 08-06-2007 10:52 AM


All times are GMT +3. The time now is 11:46 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org