Õ¾ÄÚËÑË÷: ÇëÊäÈëËÑË÷¹Ø¼ü´Ê
µ±Ç°Ò³Ãæ: ͼÊéÊ×Ò³ > SWT: The Standard Widget Toolkit

16.2 Class Point - SWT: The Standard Widget Toolkit

Previous Section  < Day Day Up >  Next Section

16.2 Class Point

Points in SWT are simple data structures. There is only one constructor.

Point(int x, int y) Sets the public fields, x and y, to the provided values.

The x and y fields of Point are directly accessed to read or modify their values. Unlike many SWT objects, Point does not allocate any operating system resources, so there is no need for it to implement a dispose() method.

The primary purpose of an instance of class Point is to represent a structure for passing to or receiving from the operating system a pair of values. Every platform has at least one structure of this form that is used to represent values such as positions in the x-y coordinate plane and width-height pairs. In the SWT platform interface, Point instances are converted to and from these underlying structures. It is important to note that the range of values that are allowed in the operating system structures may be less than the range that can be stored in an int field, so the conversion from Point to operating system equivalent may cause values to be clipped. No error is generated in this case. Most applications do not hit this limit, because the smallest range of values across all supported platforms is –32768 to 32767 (i.e., a short). However, if you are using values outside that range, you will need to watch out for portability issues.

Because class Point is intended to be simply a data structure, it does not have any of the interesting API that would be associated with the abstract mathematical notion of points, such as translation, distance computation, or magnitude comparison. Effectively, all you can do with points is create them, modify their public fields, compare them for equality, and pass them to other routines.

Putting Points in Hashed Collections

Of course, all expert Java programmers know this one already, but one of the most common mistakes that people make with mutable data structures is to modify their fields after they have been stored, based on their hash values. The hashCode() method in class Point uses the values of x and y, so if you are storing points in hashed collections, you must not modify their fields after inserting them. We have seen variants of the code shown below on more than one occasion. Watch out; this kind of bug can be very subtle to find.






Hashtable h = new Hashtable();

Point p = new Point(100,100);

h.put(p, "get the point?");

p.x = 200;  // BAD BAD BAD: changes hashCode of p.



    Previous Section  < Day Day Up >  Next Section