PageRenderTime 30ms CodeModel.GetById 11ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

/trunk/Examples/java/class/runme.java

#
Java | 70 lines | 40 code | 16 blank | 14 comment | 1 complexity | d4df2de760e685bae37251f723b7ddc5 MD5 | raw file
 1// This example illustrates how C++ classes can be used from Java using SWIG.
 2// The Java class gets mapped onto the C++ class and behaves as if it is a Java class.
 3
 4public class runme {
 5  static {
 6    try {
 7        System.loadLibrary("example");
 8    } catch (UnsatisfiedLinkError e) {
 9      System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
10      System.exit(1);
11    }
12  }
13
14  public static void main(String argv[]) 
15  {
16    // ----- Object creation -----
17    
18    System.out.println( "Creating some objects:" );
19    Circle c = new Circle(10);
20    System.out.println( "    Created circle " + c );
21    Square s = new Square(10);
22    System.out.println( "    Created square " + s );
23    
24    // ----- Access a static member -----
25    
26    System.out.println( "\nA total of " + Shape.getNshapes() + " shapes were created" );
27    
28    // ----- Member data access -----
29    
30    // Notice how we can do this using functions specific to
31    // the 'Circle' class.
32    c.setX(20);
33    c.setY(30);
34    
35    // Now use the same functions in the base class
36    Shape shape = s;
37    shape.setX(-10);
38    shape.setY(5);
39    
40    System.out.println( "\nHere is their current position:" );
41    System.out.println( "    Circle = (" + c.getX() + " " + c.getY() + ")" );
42    System.out.println( "    Square = (" + s.getX() + " " + s.getY() + ")" );
43    
44    // ----- Call some methods -----
45    
46    System.out.println( "\nHere are some properties of the shapes:" );
47    Shape[] shapes = {c,s};
48    for (int i=0; i<shapes.length; i++)
49    {
50          System.out.println( "   " + shapes[i].toString() );
51          System.out.println( "        area      = " + shapes[i].area() );
52          System.out.println( "        perimeter = " + shapes[i].perimeter() );
53    }
54    
55    // Notice how the area() and perimeter() functions really
56    // invoke the appropriate virtual method on each object.
57    
58    // ----- Delete everything -----
59    
60    System.out.println( "\nGuess I'll clean up now" );
61    
62    // Note: this invokes the virtual destructor
63    // You could leave this to the garbage collector
64    c.delete();
65    s.delete();
66    
67    System.out.println( Shape.getNshapes() + " shapes remain" );
68    System.out.println( "Goodbye" );
69  }
70}