PageRenderTime 21ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 70 lines | 40 code | 16 blank | 14 comment | 1 complexity | d4df2de760e685bae37251f723b7ddc5 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. public class runme {
  4. static {
  5. try {
  6. System.loadLibrary("example");
  7. } catch (UnsatisfiedLinkError e) {
  8. 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);
  9. System.exit(1);
  10. }
  11. }
  12. public static void main(String argv[])
  13. {
  14. // ----- Object creation -----
  15. System.out.println( "Creating some objects:" );
  16. Circle c = new Circle(10);
  17. System.out.println( " Created circle " + c );
  18. Square s = new Square(10);
  19. System.out.println( " Created square " + s );
  20. // ----- Access a static member -----
  21. System.out.println( "\nA total of " + Shape.getNshapes() + " shapes were created" );
  22. // ----- Member data access -----
  23. // Notice how we can do this using functions specific to
  24. // the 'Circle' class.
  25. c.setX(20);
  26. c.setY(30);
  27. // Now use the same functions in the base class
  28. Shape shape = s;
  29. shape.setX(-10);
  30. shape.setY(5);
  31. System.out.println( "\nHere is their current position:" );
  32. System.out.println( " Circle = (" + c.getX() + " " + c.getY() + ")" );
  33. System.out.println( " Square = (" + s.getX() + " " + s.getY() + ")" );
  34. // ----- Call some methods -----
  35. System.out.println( "\nHere are some properties of the shapes:" );
  36. Shape[] shapes = {c,s};
  37. for (int i=0; i<shapes.length; i++)
  38. {
  39. System.out.println( " " + shapes[i].toString() );
  40. System.out.println( " area = " + shapes[i].area() );
  41. System.out.println( " perimeter = " + shapes[i].perimeter() );
  42. }
  43. // Notice how the area() and perimeter() functions really
  44. // invoke the appropriate virtual method on each object.
  45. // ----- Delete everything -----
  46. System.out.println( "\nGuess I'll clean up now" );
  47. // Note: this invokes the virtual destructor
  48. // You could leave this to the garbage collector
  49. c.delete();
  50. s.delete();
  51. System.out.println( Shape.getNshapes() + " shapes remain" );
  52. System.out.println( "Goodbye" );
  53. }
  54. }