Example Usage of the Fruity framework

This example attempts to show how the Fruity Framework may be used, by listing a [mildly contrived] example before and after using Fruity. The first listing is a simple class that displays a 5 pointed star in a Frame. The imports have been removed for brevity. A couple of points to note in the code:

  • Line 38 - This builds up the star
  • Line 46 - This constructs a component that will draw the star (or any shape)
  • Line 62 - This is the ViewComponent paint method - It just clears the component, then draws the shape at 0,0
This will display the shape, but as (0,0) in the Graphics is at the top-left corner, only part of the star will be visible. If the shape was drawn at a different location, resizing the window may result in it being drawn out of the visible area.

The AffineTransform class does offer some functionality for scaling/translating shape when drawing. Only problems with it are (a) you've got to manage it yourself, and (b) it scales the line thickness.

Now jump on to the after example.

ExampleBefore.java
16   public class ExampleBefore extends JFrame 
17   { 
18       public static void main(String args[]) 
19       { 
20           ExampleBefore t = new ExampleBefore(); 
21           t.addWindowListener(new WindowAdapter() 
22           { 
23               public void windowClosing(WindowEvent e) { System.exit(0); } 
24           }); 
25           t.setSize(500, 500); 
26           t.setVisible(true); 
27       } 
28    
29    
30       public ExampleBefore() 
31       { 
32           super("Old style drawing"); 
33           Container cp = getContentPane(); 
34           cp.setLayout(new BorderLayout()); 
35           // 
36           // Create a 5 pointed star. 
37           // 
38           Polygon star = new Polygon(); 
39           star.addPoint(0,-100); 
40           star.addPoint(59, 81); 
41           star.addPoint(-95, -31); 
42           star.addPoint(95, -31); 
43           star.addPoint(-59, 81); 
44           star.addPoint(0,-100); 
45           // 
46           ViewComponent view = new ViewComponent(star); 
47           cp.add(BorderLayout.CENTER, view); 
48       } 
49   } 
50    
51    
52   class ViewComponent extends JPanel 
53   { 
54       private Shape m_shape; 
55    
56       public ViewComponent(Shape root) 
57       { 
58           super(); 
59           m_shape = root; 
60       } 
61    
62       public void paint(Graphics g)
63       { 
64           Graphics2D g2 = (Graphics2D)g; 
65           g2.setColor(Color.white); 
66           Dimension d = getSize(); 
67           g2.fillRect(0,0, d.width, d.height); 
68           g2.setColor(Color.black); 
69           g2.draw(m_shape); 
70       } 
71   } 
72   

This is very similar to the before example. The only changes are:

  • Line 48 - This constructs a 'ViewBox' instead of a ViewComponent (ViewComponent class has been removed)
  • Line 53 - This adds a key listener so we can use the keyboard to pan/zoom (the mouse interaction is always enabled)
Just this simple change is all that is needed to use fruity in our example. Now, all the work to calculate view bounds and pan/zoom is handled by fruity. Regardless of the coordinates of the shape, and the size of the component, it will initially be displayed centered in the window. Resizing the window will change the viewed area (and a fit view will show the whole object once again). Good, eh?

Fruity is much more than this; it can:

  • Scale images as well as shapes
  • Define Compound shapes (a shape which contains a list of shapes)
  • Assign attributes to objects (e.g. line color/thickness/style) that may be inherited from the containing shape
  • Selectively draw the object in a different way when dynamically panning/zooming

ExampleAfter.java
16   public class ExampleAfter extends JFrame 
17   { 
18       private Polygon star; 
19    
20       public static void main(String args[]) 
21       { 
22           ExampleAfter t = new ExampleAfter(); 
23           t.addWindowListener(new WindowAdapter() 
24           { 
25               public void windowClosing(WindowEvent e) { System.exit(0); }
26           }); 
27           t.setSize(500, 500); 
28           t.setVisible(true); 
29       } 
30    
31    
32       public ExampleAfter() 
33       { 
34           super("New style drawing"); 
35           Container cp = getContentPane(); 
36           cp.setLayout(new BorderLayout()); 
37           // 
38           // Create a 5 pointed star. 
39           // 
40           star = new Polygon(); 
41           star.addPoint(0,-100); 
42           star.addPoint(59, 81); 
43           star.addPoint(-95, -31); 
44           star.addPoint(95, -31); 
45           star.addPoint(-59, 81); 
46           star.addPoint(0,-100); 
47           // 
48           ViewBox view = new ViewBox(star);
49           cp.add(BorderLayout.CENTER, view); 
50           // 
51           // We need to do this, 'cos we're not doing focus handling. 
52           // 
53           addKeyListener(view); 
54       } 
55   } 
56