Results 1 to 8 of 8
  1. #1
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default 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 12:51 PM.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    this may help you.
    freedom exists in the world of ideas

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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);
        }
    }

  4. #4
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default

    Thank you very much I appreciate your help sukatoa & hardwired.

  5. #5
    chiamingtan is offline Member
    Join Date
    Oct 2008
    Posts
    1
    Rep Power
    0

    Default 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..:>

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    Aekkelpotta is offline Member
    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    "CODE"

    Sorry for the bump, but I was not allowed to write a private message due to my post count and I was not allowed to write you an email.

    My question, is this code free to use?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    There are no copyright notices nor stipulations mentioned, and it has been posted on a public forum, so I don't see why not.

Similar Threads

  1. Replies: 0
    Last Post: 04-23-2008, 09:15 PM
  2. Array Fill Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:45 PM
  3. Problem going outside paintComponent
    By Thez in forum Java 2D
    Replies: 9
    Last Post: 12-08-2007, 05:59 PM
  4. Drawing outside paintComponent()
    By DarkSide1 in forum Java 2D
    Replies: 2
    Last Post: 11-08-2007, 11:36 PM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 04:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •