Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: draw line to edge of rectangle

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post draw line to edge of rectangle

    i want my line 2d to stop being drawn when it hits the edge of the rectangle 2d (code below)

    my declairations:
        Point pointStart = new Point(posX, posY);
        Point pointEnd   = new Point(0,0);
        Rectangle2D example;
        public static boolean shooting = false;
        Line2D line = new Line2D.Double();
        int x = 0;
        int y = 0;

    the mouse listener:
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseMoved(MouseEvent e) {
                    pointEnd = e.getPoint();
            		linex = pointEnd.x + 10;
            		liney = pointEnd.y + 10;
                }
            });
    the paint comptent: (i am repainting at end so it is a loop)
           int x = (this.getWidth() - bkgrndImage.getWidth()) / 2;
           int y = (this.getHeight() - bkgrndImage.getHeight()) / 2;
     line = new Line2D.Double(x, y, linex, liney);
     example = new Rectangle2D.Double(mob1x, mob1y,50,50);
        g2.setStroke(new BasicStroke(3f));
        g2.setPaint(Color.RED);
     if (shooting){
     
        	g2.draw(line);
     
        	if(line.intersects(example))
        	{
    			System.out.println("line collided with rect");
    			pointEnd.x = (int) example.getCenterX();
    			pointEnd.y = (int) example.getCenterY();
    	        //line = new Line2D.Double(x, y, pointEnd.x, pointEnd.y);
           // g2.draw(line);
        }
        } else {
    		pointEnd.x = (int) x;
    		pointEnd.y = (int) y;
    		//repaint();
        }

    i dont think i have missed anything
    NOTE: it does work overall but i want it so instead of jumping to the middle, it stops at the edge.

    --Thanks in advanced!

    --Bc1151


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: draw line to edge of rectangle

    Can you make a small, complete program that compiles, executes and shows the problem for testing?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: March 25th, 2013, 07:36 AM
  2. Writing a method to draw a rotatable rectangle using Graphics2D
    By johnpooley3 in forum AWT / Java Swing
    Replies: 3
    Last Post: May 18th, 2012, 09:28 AM
  3. Unable to draw a rectangle
    By n00b in forum AWT / Java Swing
    Replies: 3
    Last Post: November 1st, 2011, 07:32 AM
  4. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM
  5. g.drawLine doesn't draw line in for loop
    By shumpi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 12th, 2010, 06:15 PM