intersect.java

Download: source code

This Java code demonstrates the intersection calculations of our Rect class.


01: //Test2.java
02: import java.awt.Graphics;
03: import java.awt.Color;
04: import javax.swing.JPanel;
05: import javax.swing.JFrame;
06: 
07: 
08: class Panel4 extends JPanel
09: {
10: 
11:         Rect a,b;
12:         
13:         public Panel4()
14:         {
15:             //a = new Rect(25,50,175,75);
16:             //a = new Rect(10,20,100,90);
17:             a = new Rect(70,60,85,140);
18:             b = new Rect(50,40,150,130);
19:         }
20: 
21:         public void paintComponent(Graphics g)
22:         {
23:             super.paintComponent( g );
24:             Rect c;
25:             c = Rect.bounding(a,b);
26:             c.fillcolor = Color.cyan;
27:             //c.draw(g);
28:             a.draw(g);
29:             b.draw(g);
30:             c = Rect.intersection(a,b);
31:             if (c!=null) {
32:                 c.fillcolor = Color.red;
33:                 if (a.contains(b) || b.contains(a)) c.fillcolor = Color.blue;
34:                 c.draw(g);
35:             }
36:                 
37:         }
38: }
39: 
40: public class intersect
41: {
42: 
43: 
44:         public static void main(String args[] )
45:         {
46: 
47:                 Panel4 panel = new Panel4();
48: 
49:                 JFrame application = new JFrame("Intersect");
50:                 application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
51:                 application.add(panel);
52: 
53:                 application.setSize(200,200);
54:                 application.setVisible(true);
55:         }
56: }


Results


Maintained by John Loomis, updated Sat Mar 08 19:23:18 2008