Results 1 to 5 of 5
  1. #1
    marina is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default colllision rectangle-polygon

    Hello!!
    Well,I have some problems with my program( I am new to java..).I want to know if there is a collision between a polygon and a rectangle.How am I going to do that??

    for the rectangle I used the method getBounds():
    Java Code:
    public Rectangle r;
    Graphics g;
    	
    	public Obstacle(int x,int y,int w,int h)
    	{
    		r=new Rectangle(x,y,w,h);
    	}
    	
    	
    	 public Rectangle getBounds() {
    	       return new Rectangle(r.x,r.y,r.width,r.height);
    	       }
    	
    	public void draw(Graphics gr)
      	{
      		g=gr;
      		g.setColor(Color.red);
      		g.fillRect(r.x,r.y,r.width,r.height);
      	}
    Am i able to do the same for the polygon (use getbounds) and then use the intersects method?
    Java Code:
    int[] xs = { (int)((Diameter/2)*1.4), -(int)((Diameter/2)*0.6), -(int)((Diameter/2)*0.6) };
    int[] ys = { 0, (int)((Diameter/2)*0.6), -(int)((Diameter/2)*0.6) };
    int[] xpoints = {0,0,0};
    int[] ypoints = {0,0,0};
    
     double sin = Math.sin(direction);
     double cos = Math.cos(direction);
    
     for(int tripoint = 0;tripoint < 3;tripoint++){
        xpoints[tripoint] = (int)x + (int)(cos * xs[tripoint] + -sin * ys[tripoint]);
        ypoints[tripoint] = (int)y + (int)(sin * xs[tripoint] + cos * ys[tripoint]);
    	                 }
    	         
    	                g.setColor(color);
    	                g.fillPolygon(xpoints,ypoints,3);
    	                g.drawPolygon(xpoints,ypoints,3);
    getBounds for the polygon:
    Java Code:
    public Rectangle getBounds() {
        	   return new Rectangle(x+(int)((Diameter)*1.4),y+(int)((Diameter)*0.6),(int)((Diameter)*1.4),(int)((Diameter)*0.6));
           }
    Thanks in advance!

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

    Default

    The Polygon class api shows a method
    Java Code:
    boolean intersects(Rectangle2D r)
    that might be helpful.

  3. #3
    marina is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default

    thanks for the reply!
    My problem is how to get the bounds of the polygon..
    I know about intersects method and i used it in my code but sometimes it doesn't "see" that there is a collision between the rectangle and the polygon.So I assumed there's something wrong with the polygon's getbounds method..
    Last edited by marina; 01-27-2009 at 08:46 PM.

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

    Default

    Using the bounds of a polygon to test for intersection is using the Rectangle intersects method. Bounds => Rectangle. Using the Polygon intersects method is a different thing. To demonstrate the difference:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class PolygonIntersection extends JPanel {
        Polygon polygon;
        Rectangle rect;
        boolean selected = false;
        boolean useBounds = false;
    
        public PolygonIntersection() {
            int[][] pts = {
                { 125, 176, 73 }, { 40, 130, 130 }
            };
            polygon = new Polygon(pts[0], pts[1], 3);
            rect = new Rectangle(100, 150, 175, 75);
            addMouseListener(ma);
            addMouseMotionListener(ma);
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            Color color = selected ? Color.red : Color.blue;
            g2.setPaint(color);
            g2.draw(polygon);
            g2.draw(rect);
            g2.setPaint(Color.pink);
            g2.draw(polygon.getBounds());
        }
    
        private void checkForIntersection() {
            selected = useBounds ? polygon.getBounds().intersects(rect)
                                 : polygon.intersects(rect);
            repaint();
        }
    
        private JPanel getLastPanel() {
            String[] ids = { "bounds", "polygon" };
            ActionListener al = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String id = e.getActionCommand();
                    boolean selected = ((JRadioButton)e.getSource()).isSelected();
                    useBounds = id.equals("bounds") && selected;
                    checkForIntersection();
                }
            };
            ButtonGroup group = new ButtonGroup();
            JPanel panel = new JPanel();
            for(int i = 0; i < ids.length; i++) {
                boolean b = useBounds ? i == 0 : i > 0;
                JRadioButton rb = new JRadioButton(ids[i], b);
                rb.setActionCommand(ids[i]);
                rb.addActionListener(al);
                group.add(rb);
                panel.add(rb);
            }
            return panel;
        }
    
        public static void main(String[] args) {
            PolygonIntersection test = new PolygonIntersection();
            JFrame f = new JFrame("drag the polygon");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.add(test.getLastPanel(), "Last");
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    
        private MouseAdapter ma = new MouseAdapter() {
            boolean dragging = false;
            Point start;
    
            public void mousePressed(MouseEvent e) {
                Point p = e.getPoint();
                if(polygon.contains(p)) {
                    start = p;
                    dragging = true;
                }
            }
    
            public void mouseReleased(MouseEvent e) {
                dragging = false;
            }
    
            public void mouseDragged(MouseEvent e) {
                if(dragging) {
                    Point p = e.getPoint();
                    int x = p.x - start.x;
                    int y = p.y - start.y;
                    polygon.translate(x, y);
                    start.setLocation(p);
                    checkForIntersection();
                }
            }
        };
    }

  5. #5
    marina is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default

    thanks very much for the reply!!I 'll study that code and hope to solve my problem..

Similar Threads

  1. polygon-shaped JComponent
    By zenMarko in forum New To Java
    Replies: 2
    Last Post: 11-04-2008, 06:06 PM
  2. Random Points inside a Polygon
    By nidhirastogi in forum Advanced Java
    Replies: 1
    Last Post: 09-23-2008, 03:28 AM
  3. How to Fill a Polygon in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:10 PM
  4. How to Draw a Polygon in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:09 PM
  5. help with rectangle class
    By darkgt in forum New To Java
    Replies: 7
    Last Post: 11-14-2007, 06:19 PM

Posting Permissions

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