Results 1 to 8 of 8
- 05-03-2008, 11:49 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 35
- Rep Power
- 0
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
Java 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 11:51 AM.
- 05-03-2008, 07:12 PM #2
- 05-03-2008, 07:48 PM #3
Java 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); } }
- 05-03-2008, 09:30 PM #4
Member
- Join Date
- Nov 2007
- Posts
- 35
- Rep Power
- 0
Thank you very much I appreciate your help sukatoa & hardwired.
- 10-23-2008, 08:51 AM #5
Member
- Join Date
- Oct 2008
- Posts
- 1
- Rep Power
- 0
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..:>
-
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.
- 04-14-2009, 03:50 PM #7
Member
- Join Date
- Apr 2009
- Posts
- 1
- Rep Power
- 0
-
Similar Threads
-
Using reflection to create, fill, and display an array
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:15 PM -
Array Fill Test
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:45 PM -
Problem going outside paintComponent
By Thez in forum Java 2DReplies: 9Last Post: 12-08-2007, 04:59 PM -
Drawing outside paintComponent()
By DarkSide1 in forum Java 2DReplies: 2Last Post: 11-08-2007, 10:36 PM -
paint() and paintComponent()
By goldhouse in forum Java 2DReplies: 1Last Post: 07-17-2007, 03:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks