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:20 AM
Moderator
 
Join Date: Nov 2007
Posts: 1,637
Java Tip will become famous soon enoughJava Tip will become famous soon enough
How to Draw Curve with mouse
This Java tip shows how to draw Curve with mouse.

Code:
import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.geom.CubicCurve2D; import java.awt.geom.Line2D; import java.awt.geom.QuadCurve2D; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class CubicCurveMouse extends JFrame { DrawingCanvas canvas; JLabel label = new JLabel("Mouse Location (x, y): "); JLabel coords = new JLabel(""); public CubicCurveMouse() { super(); Container container = getContentPane(); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(1, 2)); panel.add(label); panel.add(label); panel.add(coords); container.add(panel, BorderLayout.SOUTH); canvas = new DrawingCanvas(); container.add(canvas); addWindowListener(new WindowEventHandler()); setSize(300,300); setVisible(true); } class WindowEventHandler extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } public static void main(String arg[]) { new CubicCurveMouse(); } class DrawingCanvas extends Canvas { float x1, y1, xc1cur, yc1cur, xc1new, yc1new, xc2cur, yc2cur, xc2new, yc2new, x4cur, y4cur, x4new, y4new; int pressNo = 0; int dragFlag1 = -1; int dragFlag2 = -1; boolean clearFlag = false; float dashes[] = { 5f, 5f }; BasicStroke stroke; public DrawingCanvas() { setBackground(Color.white); addMouseListener(new MyMouseListener()); addMouseMotionListener(new MyMouseListener()); setSize(400, 400); stroke = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10f, dashes, 0f); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; if (pressNo == 1) { g2D.setXORMode(getBackground()); g2D.setColor(Color.black); g2D.setStroke(stroke); // Erase the currently existing line g2D.draw(new Line2D.Float(x1, y1, x4cur, y4cur)); // Draw the new line g2D.draw(new Line2D.Float(x1, y1, x4new, y4new)); // Update the currently existing coordinate values x4cur = x4new; y4cur = y4new; }else if (pressNo == 2) { g2D.setXORMode(getBackground()); g2D.setColor(Color.black); g2D.setStroke(stroke); if (dragFlag1 != -1) { g2D.draw(new QuadCurve2D.Float(x1, y1, xc1cur, yc1cur, x4new, y4new)); } dragFlag1++; // Reset the drag-flag g2D.draw(new QuadCurve2D.Float(x1, y1, xc1new, yc1new, x4new, y4new)); xc1cur = xc1new; yc1cur = yc1new; }else if (pressNo == 3) { g2D.setXORMode(getBackground()); g2D.setColor(Color.black); if (dragFlag2 != -1) { g2D.draw(new CubicCurve2D.Float(x1, y1, xc1new, yc1new, xc2cur, yc2cur, x4new, y4new)); } dragFlag2++; // Reset the drag flag g2D.draw(new CubicCurve2D.Float(x1, y1, xc1new, yc1new, xc2new, yc2new, x4new, y4new)); xc2cur = xc2new; yc2cur = yc2new; } if (clearFlag) { g2D.setXORMode(getBackground()); g2D.setColor(Color.black); g2D.setStroke(stroke); g2D.draw(new Line2D.Float(x1, y1, x4new, y4new)); g2D.draw(new QuadCurve2D.Float(x1, y1, xc1new, yc1new, x4new, y4new)); clearFlag = false; } } class MyMouseListener extends MouseAdapter implements MouseMotionListener { public void mousePressed(MouseEvent e) { if (pressNo == 0) { pressNo++; x1 = x4cur = e.getX(); y1 = y4cur = e.getY(); } else if (pressNo == 1) { pressNo++; xc1cur = e.getX(); yc1cur = e.getY(); } else if (pressNo == 2) { pressNo++; xc2cur = e.getX(); yc2cur = e.getY(); } } public void mouseReleased(MouseEvent e) { if (pressNo == 1) { x4new = e.getX(); y4new = e.getY(); canvas.repaint(); } else if (pressNo == 2) { xc1new = e.getX(); yc1new = e.getY(); canvas.repaint(); } else if (pressNo == 3) { xc2new = e.getX(); yc2new = e.getY(); canvas.repaint(); pressNo = 0; dragFlag1 = -1; dragFlag2 = -1; clearFlag = true; } } public void mouseDragged(MouseEvent e) { if (pressNo == 1) { x4new = e.getX(); y4new = e.getY(); String string = "(" + Integer.toString(e.getX()) + ", " + Integer.toString(e.getY()) + ")"; coords.setText(string); canvas.repaint(); } else if (pressNo == 2) { xc1new = e.getX(); yc1new = e.getY(); String string = "(" + Integer.toString(e.getX()) + ", " + Integer.toString(e.getY()) + ")"; coords.setText(string); canvas.repaint(); } else if (pressNo == 3) { xc2new = e.getX(); yc2new = e.getY(); String string = "(" + Integer.toString(e.getX()) + ", " + Integer.toString(e.getY()) + ")"; coords.setText(string); canvas.repaint(); } } public void mouseMoved(MouseEvent e) { String string = "(" + Integer.toString(e.getX()) + ", " + Integer.toString(e.getY()) + ")"; coords.setText(string); } } } }
__________________
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
Mouse drag and drop to draw Java Tip java.awt 0 06-24-2008 12:16 AM
how to draw a fill rectangle using mouse and paintComponent? java_fun2007 New To Java 3 05-03-2008 10:30 PM
The mouse and the cheese Don Quixote Java 2D 4 08-15-2007 11:55 PM
mouse over on JButton gradon Java Applets 1 08-04-2007 06:50 AM
Mouse over JButton sandor AWT / Swing 1 05-17-2007 10:15 PM


All times are GMT +3. The time now is 02:42 AM.


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