Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Mathematics for Computer Graphics - Lecture 12 Dr. Philippe B. Laval Kennesaw State University October 6, 2003 Abstract This document is about creating Java Applets as they relate to the project we are developing in class. 1 Type Conversion Type conversion occurs when a variable of one type is assigned a value of another type. Type conversion can be done one of three ways: • Automatic conversion • Type casting • Type conversion routines. 1.1 Automatic Conversion When one type of data is assigned to another type of variable, automatic type conversion will take place if the following two conditions are met: 1. The two types are compatible. 2. The destination type is larger than the source type. This type of conversion is often called a widening conversion. For example, an int can always hold a byte. A double can always hold a float. 1 1.2 Type Casting When the types are not compatible, a type cast must be used. The general form is (target type) value. For example, suppose that a is a double and b is a float. The statement b = a would cause an error because b cannot hold a. To force a type conversion, one must use a type cast. It would be a statement of the form b = (float) a 1.3 Conversion Routines The basic types such as int, double, float have been encapsulated in a class which has the same name but begins with an upper case letter. These classes hold an object of the basic type along with conversion routines. Let us look at some of these classes 1.3.1 Integer This encapsulates the type int. There are two constructors: • Integer(int value) • Integer(String s) Some of the useful methods it implements are • public double doubleValue() Returns the value of this integer as a double. There is a similar method for floatValue(), intValue(), longValue, byteValue() • public static int parseInt(String s) parses the string as a signed integer. • public String toString() returns the String representation of this int. 1.3.2 Long This encapsulates the type long. There are two constructors: • Long(long value) • Long(String s) Some of the useful methods it implements are 2 • public double doubleValue() Returns the value of this long integer as a double. There is a similar method for floatValue(), intValue(), longValue, byteValue() • public static long parseLong(String s) parses the string as a signed long integer. • public String toString() returns the String representation of this long integer. 1.3.3 Double This encapsulates the type double. It has similar methods as the previous two types, and a few more. Here are some of the extra methods: • public static boolean isInfinite(double d) • public boolean isInfinite() • public static boolean isNaN(double d) • public static boolean isNaN() 1.3.4 Float This encapsulates the type float. It has similar methods to Double. 1.3.5 String This class represents character strings. Here are the methods associated with it. Methods beginning with * are static. • charAt(int index) Returns the character at the specified index. • compareTo(String anotherString) Compares two strings lexicographically. • concat(String str) Concatenates the specified string to the end of this string. • * copyValueOf(char[] data) Returns a String that is equivalent to the specified character array. • * copyValueOf(char[] data, int offset, int count) Returns a String that is equivalent to the specified character array. • endsWith(String suffix) Tests if this string ends with the specified suffix. 3 • equals(Object anObject) Compares this string to the specified object. • equalsIgnoreCase(String anotherString) Compares this String to another object. • getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) Copies characters from this string into the destination character array. • indexOf(String str) Returns the index within this string of the first occurrence of the specified substring. • indexOf(String str, int fromIndex) Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. • indexOf(int ch, int fromIndex) Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index. • indexOf(int ch) Returns the index within this string of the first occurrence of the specified character. • lastIndexOf(String str, int fromIndex) Returns the index within this string of the last occurrence of the specified substring. • lastIndexOf(int ch, int fromIndex) Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. • lastIndexOf(int ch) Returns the index within this string of the last occurrence of the specified character. • lastIndexOf(String str) Returns the index within this string of the rightmost occurrence of the specified substring. • length() Returns the length of this string. • regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) Tests if two string regions are equal. • regionMatches(int toffset, String other, int ooffset, int len) Tests if two string regions are equal. 4 • replace(char oldChar, char newChar) Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. • startsWith(String prefix, int toffset) Tests if this string starts with the specified prefix. • startsWith(String prefix) Tests if this string starts with the specified prefix. • substring(int beginIndex) Returns a new string that is a substring of this string. • substring(int beginIndex, int endIndex) Returns a new string that is a substring of this string. • toCharArray() Converts this string to a new character array. • toLowerCase() Converts this String to lowercase. • toLowerCase(Locale locale) Converts all of the characters in this String to lower case using the rules of the given locale. • toString() This object (which is already a string!) is itself returned. • toUpperCase(Locale locale) Converts all of the characters in this String to upper case using the rules of the given locale. • toUpperCase() Converts this string to uppercase. • trim() Removes white space from both ends of this string. • * valueOf(Object obj) Returns the string representation of the Object argument. • * valueOf(char c) Returns the string representation of the char argument. • * valueOf(boolean b) Returns the string representation of the boolean argument. • * valueOf(long l) Returns the string representation of the long argument. 5 • * valueOf(int i) Returns the string representation of the int argument. • * valueOf(char[] data, int offset, int count) Returns the string representation of a specific subarray of the char array argument. • * valueOf(char[] data) Returns the string representation of the char array argument. • * valueOf(float f) Returns the string representation of the float argument. • * valueOf(double d) Returns the string representation of the double argument. 2 2.1 Java Applets and Computer Graphics Introduction The main purpose of this class is to learn about 3D computer graphics. Java 2, our platform of development, provides the programmer with several tools to help in this task. Howver, because we are trying to understand the mathematics behind computer graphics, we will not use most of these tools. We will use some of them though; we will not do everything from scratch. More precisely, since our goal is to understand 3D computer graphics, we will develop our own tools to do 3D computer graphics, using the 2D tools Java 2 provides. The 2D tools Java 2 provides are called Java2D. Java 2D form an integral part of Java 2. In other words, once you install Java 2, Java2D is available to you. In the next section, we will look at the features of Java2D we will use for this project. Among the tools available in Java 2 that we will not use are Java3D and the Advanced Imaging API. Though we will not use these tools it is important to know they exist for future projects you might be involved with. Unlike Java2D, these two tools are not part of Java 2. They must be downloaded and installed separately. They are available from Sun Microsystems, at http://java.sun.com. The project we will be developing will run as a Java applet. We will also need to learn how to write Java applets as well as deal with user interfaces. There are several choices here. One can use AWT (the Advanced Window Toolkit) for the user interface. Another option is SWING. It is a more powerful user interface. It allows the programmer to make applets behave and look more like regular programs. 6 3 Components Used in an Applet 3.1 Basics of a Java Applet A Java applet is a java program which runs inside a Java enabled web browser. See the notes on the vect3D class, given on September 17, for more details regarding the difference between a java applet and a java application. When creating a java applet, remember the following: 1. The main class of your java applet must be saved in a file with the same name. 2. You must also create an html file which will call the applet. In its simplest form, an applet simply defines an area on which you can place various components. It is these various components which will perform the tasks you want the applet to perform. A java applet is "event driven". This means that the applet sits there, quietly, until the user does something. That thing can be a mouse click, pressing a key, ... Whenever this happens, an event is triggered. By looking at what kind of event is triggered, and where it was triggered, the programmer decides which action should be taken. In reality, only the events the programmer decides to listen to will be triggered. A given event can be listened to over the entire applet, or over certain components of the applet. The second way is usually the way it is done. Catching and handling events is a big part of writing a java applet. The applet JApplet1.java is an applet which does nothing, except opens an area in your browser which is 426 by 266 pixels. Every applet must contain at least the code this applet contains. Look at the code and the comments in it to understand what an applet is made of. The applet JAppletE.java is the same as the above applet, but we have enabled two events: the lost focus event and the mouse click event. Again, look at the code The applet JAppletAllEvents.java is similar to JApplet1.java, but all the events have been enabled so you can see what they are. We now review some of the components an applet can be made of, the events they can handle and how to handle these events. 3.2 JPanel If an applet can be divided into different areas, it is a good idea to draw a panel corresponding to each area. Other objects can then be drawn to each panel. This allows the programmer to enable different events for each panel. The applet JAppletPanel.java is a very simple applet on which 3 panels have been drawn The applet JAppletPanelE.java is like JAppletPanel, but some events have been enabled. Note how events can be enabled for specific panels. 7 3.3 JLabel A display area for a short text string or an image, or both. A label does not react to input events. As a result, it cannot get the keyboard focus. Some useful methods associated with it are: • public String getText() Returns the text string that the label displays. • public void setText(String text) Defines the single line of text this component will display. See the test1.java applet for how this object is used. 3.4 JTextField JTextField is a lightweight component that allows the editing of a single line of text. It is intended to be source-compatible with java.awt.TextField where it is reasonable to do so. This component has capabilities not found in the java.awt.TextField class. The superclass should be consulted for additional capabilities. Some useful methods associated with it are: • public String getText() Returns the text string that the label displays. • public void setText(String text) Defines the single line of text this component will display. See the test1.java applet for how this object is used. 3.5 JTextArea A TextArea is a multi-line area that displays plain text. It is intended to be a lightweight component that provides source compatibility with the java.awt.TextArea class where it can reasonably do so. This component has capabilities not found in the java.awt.TextArea class. The superclass should be consulted for additional capabilities. Alternative multi-line text classes with more capabilities are JTextPane and JEditorPane. In addition to the methods of the previous component, it also has: • append(String str) Appends the given text to the end of the document. • getColumns() Returns the number of columns in the TextArea. • getLineCount() Determines the number of lines contained in the area. 8 • getLineEndOffset(int line) Determines the offset of the end of the given line. • getLineOfOffset(int offset) Translates an offset into the components text to a line number. • getLineStartOffset(int line) Determines the offset of the start of the given line. • getLineWrap() Gets the line-wrapping policy of the text area. • getPreferredScrollableViewportSize() • getRows() Returns the number of rows in the TextArea. • insert(String str, int pos) Inserts the specified text at the specified position. 3.6 JComboBox Swing’s implementation of a ComboBox — a combination of a text field and drop-down list that lets the user either type in a value or select it from a list that is displayed when the user asks for it. The editing capability can also be disabled so that the JComboBox acts only as a drop down list. Some useful methods associated with it are: • addItem(Object anObject) Adds an item to the item list. • getItemAt(int index) Returns the list item at the specified index. • getItemCount() Returns the number of items in the list. • getSelectedIndex() Returns the index of the currently selected item in the list. • getSelectedItem() Returns the currently selected item. See the test1.java applet for how this object is used. 3.7 JButton An implementation of a "push" button. See the test1.java applet for how this object is used. 9 4 Assignment Go over the various program examples, including the test1 applet and make sure you understand them. If you have any questions, have them ready for the next class period. 5 Resources This is a list of books and other resources I used to compile these notes. References [BG1] Burger, Peter, and Gillies, Duncan, Interactive Computer Graphics, Addison-Wesley, 1990. [DD1] Deitel, H.M., and Deitel, P. J., Java, How to Program, Prentice Hall, 1999. [DP1] Dunn, Fletcher and Parberry, Ian, 3D Math Primer for Graphics and Game Development, Wordware Publishing, Inc., 2002. [FD1] Foley, J.D., Van Dam, A., Feiner, S.K., and Hughes, J.F., Computer Graphics, Principles and Practices, Addison-Wesley, 1995. [FD2] Foley, J.D., Van Dam, A., Feiner, S.K., Hughes, J.F., and Philipps, R.L., Introduction to Computer Graphics, Addison-Wesley, 1997. [H1] Hill, F.S. JR., Computer Graphics Using Open GL, Prentice Hall, 2001. [LE1] Lengyel, Eric, Mathematics for 3D Game Programming & Computer Graphics, Charles River Media, Inc., 2002. [SE1] Schneider, Philip J., and Eberly, David H., Geometric Tools for Computer Graphics, Morgan Kaufman, 2003. [SP1] Shirley, Peter, Fundamentals of Computer Graphics, A K Peters, 2002. [SJ1] Stewart, James, Calculus, Concepts and Contexts, second edition, Brooks/Cole, 2001. [WG1] Wall, David, and Griffith, Arthur, Graphics Programming with JFC, Wiley, 1999. [AW1] Watt, Alan, 3D Computer Graphics, Addison-Wesley, 2000. [AW2] Watt, Alan, The Computer Image, Addison-Wesley, 1999. 10