COMP 557 – Tutorial 1: Eclipse, Java, JOGL, Mintools
Total Page:16
File Type:pdf, Size:1020Kb
COMP 557 { Tutorial 1: Eclipse, Java, JOGL, MINtools TA: Emmanuel Piuze McGill University September 11, 2012 Contents 1 Java 3 1.1 History...........................................3 1.2 Commenting Your Code..................................4 1.3 Primitive data types....................................4 1.3.1 Primitive type wrappers..............................4 1.4 Operators..........................................5 1.5 Methods...........................................5 1.6 The Main Method.....................................6 1.7 Rounding..........................................7 1.8 Logic.............................................7 1.9 Loops............................................8 1.10 Classes............................................9 1.11 JRE Convenience Classes................................. 10 1.11.1 Lists......................................... 10 1.12 Vecmath package...................................... 11 1.12.1 Points and Vectors................................. 11 1.12.2 Matrices....................................... 12 1.12.3 Quaternions..................................... 13 2 Eclipse 13 2.1 History........................................... 13 2.2 Initial Setup......................................... 14 2.2.1 Verifying if Java is Properly Installed...................... 14 2.2.2 Downloading and Installing Eclipse........................ 14 2.2.3 Configuring JREs and JDK Compiler Compliance............... 14 2.3 Configuring JOGL User Libraries............................. 15 2.4 Workspace Management.................................. 16 2.5 Perspectives, Views and Packages............................. 16 2.6 Creating a Java Project.................................. 17 2.7 Creating a Folder, Package, Class, Interface, Enum or Others............. 17 2.8 Must-Read: Tips and Tricks................................ 17 1 COMP 557 { Tutorial 1 September 11, 2012 TA: Emmanuel Piuze-Phaneuf 2.9 Code refactoring...................................... 19 2.10 Debugging and Hot Code Replace............................ 20 3 Swing and MINtools 20 3.1 Master of the UI: Creating The EasyViewer....................... 20 3.2 Building your UI...................................... 22 3.2.1 The getControls() method............................. 22 3.2.2 CollapsiblePanel.................................. 22 3.3 Parameters......................................... 22 3.3.1 Constructors.................................... 22 3.3.2 Controls....................................... 23 3.3.3 Listeners...................................... 24 3.4 Keyboard and Mouse Input................................ 24 Page 2 of 26 COMP 557 { Tutorial 1 September 11, 2012 TA: Emmanuel Piuze-Phaneuf Introduction This tutorial will help you get started for assignment 0 and subsequent assignments. Let me know if something is not clear, if you find a mistake, if you would like to add something to this tutorial, or if you simply find it useful! Topics that will be covered include the following: 1. Java programming language • Refresher (syntax, operators, loops, constructors, etc.) • Basic data structures and operations (lists, hash tables, etc.) • Vecmath package (vectors, points, matrices, quaternions and operations on them) 2. Eclipse open development platform • Setting up a Java workspace (project builder, window placement, etc.) • Quick navigation (jump-to's, class methods, fields, etc.) • Code refactoring • Hot-code debugging (runtime modifications and effects) 3. Swing and MINtools • Basic Java Swing elements and syntax (JButton, JLabel, JSlider, etc.) • Parameter types (boolean, integer, double) and listeners • UI elements (control frames, collapsible panels, etc.) • Interactivity (KeyAdapter, MouseMotionListener, keyPressed, mouseMoved, etc.) 1 Java This part of the tutorial will give you a brief Java refresher. Most concepts are not explained thoroughly so I assume that you have had some experience with Java at some point in your life. If it's not the case, I highly recommend that you dig through some examples in any Java reference book you can find on the web or at the Schulich Library. 1.1 History Java is an object-oriented programming language developed by Sun Microsystems (now Oracle) in 1995. Its syntax is similar to C and C++ but has a simpler object model and runs in a protected virtual machine, thus exposing less low-level facilities. There is no such thing as a pointer in Java: everything is passed by reference, except for primitive types (e.g. int, double) and the special String class. Memory allocation is also being taken care of automatically, as well as deallocation (garbage collection). Java applications are multiplatform in the sense that once compiled to bytecode they can run on any machine that has a Java Virtual Machine (JVM) installed. Page 3 of 26 COMP 557 { Tutorial 1 September 11, 2012 TA: Emmanuel Piuze-Phaneuf 1.2 Commenting Your Code First and foremost, the most important and most neglected part of any programming language, the comments. In Java, comments can be single-line or multiline, and can optionally be (although recommended by standard coding guidelines) of two different flavours. The first flavour (single asterisk), for both single- and multi-line comments, should be used inside methods for describing blocks of code or for commenting code out. The second flavour (double asterisk) should be used everywhere else, for example when describing the signature of a method or a class. 1 // 1. This is a single −line comment of the first flavour. 2 3 // 2. Commenting a block of code using the first flavour in multi−l i n e . 4 /∗ 5 double z = Random.nextDouble(); 6 double y = z ∗ z ; 7 ∗/ 8 9 /∗∗ 3. This is a single −line comment of the second flavour ∗/ 10 11 /∗∗ 12 ∗ 4. This is a multi−line comment of the second flavour. 13 ∗ Note the double asterisk at the top. 14 ∗ This method adds up two numbers together. 15 ∗ @param x the first number 16 ∗ @param y the second number 17 ∗/ 18 public int add ( int x , int y ) f 19 return x + y ; 20 g 1.3 Primitive data types In Java, primitive data types are predefined by the language and are named using reserved keywords. The eight primitive data types supported by the Java programming language are: byte, short, int, long, float, double, boolean, char. The String class is a special case that is treated analogously to a primitive data type. Primitive type by themselves cannot be modified by reference (no pointers) and don't have class methods, i.e. 1 double x = 1 . 5 ; 2 x.round(); // Invalid: no class methods exist for primitive types 3 double y = x ; // Valid: y equals 1.5 4 x = 2 ; // Valid, but y still equals 1.5: no reference for primitive types 1.3.1 Primitive type wrappers The JRE contains class wrappers for each primitive type. These wrappers provide additional operations (rounding, type casting, truncating, etc.) and allow primitives to be treated like objects (that is, they can be passed by reference). The naming convention for these classes is the name of the primitive type they reference with the first letter in uppercase (e.g. Double, Integer, Float, etc.). They do not have constructors and are created as follows: 1 // Create a new Double object with the value 1.5 Page 4 of 26 COMP 557 { Tutorial 1 September 11, 2012 TA: Emmanuel Piuze-Phaneuf 2 Double x = 1.5; 3 4 // Extract primitive type from wrapper 5 double xp = x.doubleValue(); 6 7 // Test for NaN (not a number) 8 i f (x.isNaN()) exit(1); 9 10 // Type casting from double to Integer 11 int y = x.intValue(); 12 13 // Type casting from String to Float 14 Float z = Float.valueOf("1.5"); 1.4 Operators Here are some examples showing the use of common operators: 1 int x = 1 ; 2 int y = 1 ; 3 int z ; 4 5 // Additive , multiplicative 6 z = x + y ; // z is the sum of x and y 7 z = x − y ; // z is the difference of x and y 8 z = y ∗ x ; // z is y times x 9 z = x / y ; // z is x divided by y 10 z = x % y ; // z is the reminder of x divided by y (modulus) 11 12 // Assignment 13 x += 2 ; // x is incremented in place by 2 14 x ∗= 2 ; // x is multiplied in place by 2 15 x /= 2 ; // x is divided in place by 2 16 17 // P o s t f i x 18 x++; // x is incremented by 1 after "x" is executed 19 x−−; // x is decremented by 1 after "x" is executed Other operators include unary, shift, relational, bitwise, logical and ternary operators (see Oracle's Java Operators for a com plete list along with the operator precedence). String objects can be manipulated using the + sign, which acts as a concatenation operator: 1 String s = "Programmers " + "using C "; 2 s += " can't use this"; 3 // s equals "Programmers using C can't use this" 1.5 Methods Methods in Java have a signature similar to those in C. Methods are required to have a return type { null if nothing is returned, just like void in C {, a name, a comma-delimited parameter list in a parenthesis, and a body enclosed in brackets , e.g. 1 int sum( int x , int y ) f 2 return x + y ; 3 g Page 5 of 26 COMP 557 { Tutorial 1 September 11, 2012 TA: Emmanuel Piuze-Phaneuf Optionally, you can specify a modifier such as public or private, depending on the scope of the method, and static. See Section 1.10 on Java classes for more on this. Other elements include an exception list (not covered in this tutorial) and generic types (also not covered). 1.6 The Main Method Second most important part of your application, the Main method. This method is how the Java Virtual Machine knows that your application wants to be compiled. Without it, your code is just a complicated arrangement of spaghetti but without sauce. This method must appear within a class, but it can be any class. Here is an example of a main() method invoking the constructor for the application living in the class MyApp: Page 6 of 26 COMP 557 { Tutorial 1 September 11, 2012 TA: Emmanuel Piuze-Phaneuf 1 /∗∗ 2 ∗ @param args 3 ∗/ 4 public static void main(String [] args) f 5 new MyApp( ) ; 6 g More onto constructors and classes later... 1.7 Rounding If you cast a double or a float directly to an integer, the decimal part of the number is simply omitted, no rounding operation is performed.