Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 05-03-2008, 01:49 PM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
how to draw a fill rectangle using mouse and paintComponent?
hi
I need to make a simple program to draw fillRect using MouseMotionListener and paintComponent on a JPanel.
I don't know how to get the width and height of the rectangle but I've tried this it doesn't draw any rectangle:
I calculated the height and width according to the mousedragged point and mousereleased point.

Thanks


Code:
import java.awt.Point; import java.awt.*; import java.awt.event.*; import javax.swing.JPanel; import javax.swing.*; public class DrawRect extends JPanel { private int pointCount =0; private int pointCount2 =0; private Point points[] = new Point[100]; private Point points2[] = new Point[100]; public DrawRect() { addMouseMotionListener( new MouseMotionAdapter() { public void mouseDragged(MouseEvent ev) { if (pointCount < points.length) { points[pointCount] = ev.getPoint(); pointCount++; repaint(); } }//end mouse drag public void mouseReleased(MouseEvent ev) { if (pointCount2<points2.length) { points2[pointCount2]= ev.getPoint(); pointCount2++; repaint(); } } } ); } public void paintComponent(Graphics g) { super.paintComponent(g); for (int i =0;i<pointCount;i++) g.fillRect(points[i].x,points[i].y,points2[i].x - points[0].x,points2[i].y - points[0].y); } public static void main(String[] args) { DrawRect test = new DrawRect(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }

Last edited by java_fun2007 : 05-03-2008 at 01:51 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-03-2008, 09:12 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
this may help you.
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-03-2008, 09:48 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DrawRectRx extends JPanel { private int pointCount =0; private Point points[] = new Point[100]; private Point points2[] = new Point[100]; private Point start = new Point(); private Point end = new Point(); Rectangle rect = new Rectangle(); public DrawRectRx() { addMouseMotionListener( new MouseMotionAdapter() { public void mouseDragged(MouseEvent ev) { end = ev.getPoint(); rect.setFrameFromDiagonal(start, end); repaint(); }//end mouse drag }); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { start = e.getPoint(); } public void mouseReleased(MouseEvent ev) { points[pointCount] = start; points2[pointCount] = ev.getPoint(); pointCount++; rect.setFrameFromDiagonal(start, start); repaint(); } }); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Draw line being dragged. g2.setPaint(Color.red); g2.draw(rect); // Draw lines between points in arrays. g2.setPaint(Color.blue); Rectangle r = new Rectangle(); for (int i =0; i < pointCount; i++) { r.setFrameFromDiagonal(points[i], points2[i]); g2.fill(r); } } public static void main(String[] args) { DrawRectRx test = new DrawRectRx(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-03-2008, 11:30 PM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
Thank you very much I appreciate your help sukatoa & hardwired.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-23-2008, 10:51 AM
Member
 
Join Date: Oct 2008
Posts: 1
chiamingtan is on a distinguished road
Help needed....
I need to drag and display a rectangle in a JPanel... displaying its coordinates as well. Is anyone out there able to provide helps?? many thanks..:>
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-23-2008, 11:52 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 764
Fubarable is on a distinguished road
You should start your own thread for this, not resurrect an old dormant thread.

Also, we can help, but to help we have to know what problems you're encountering. In your new thread, I recommend that you post your code and ask specific questions on how your current code is not working.
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
Using reflection to create, fill, and display an array Java Tip java.lang 0 04-23-2008 10:15 PM
Array Fill Test Java Tip java.lang 0 04-14-2008 10:45 PM
Problem going outside paintComponent Thez Java 2D 9 12-08-2007 06:59 PM
Drawing outside paintComponent() DarkSide1 Java 2D 2 11-09-2007 12:36 AM
paint() and paintComponent() goldhouse Java 2D 1 07-17-2007 05:43 AM


All times are GMT +3. The time now is 12:23 PM.


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