Pixel Coordinates

Before we look into some important issues relating to sizing a window, it is important to understand the system of defining pixel coordinates in Java. Take a look at the following figure.

click to expand
Figure 9-1:

This diagram shows a set of pixels of resolution 10x5. This means that the grid is 10 pixels in width and 5 pixels in height. The coordinate system is relative to the top-left corner of a given rectangular area. This is the standard coordinate system for any rectangular screen or off-screen graphics in Java, such as a window or a button or an off-screen image defined by a location (x, y) and a dimension (width, height). The location represents the position of the graphic relative to its parent or, to be more precise, the current translation position of the Graphics object to which it is drawing (translations will be explained a little later in the chapter). There are three basic classes for representing rectangular regions throughout the Java2D API: Point, Dimension, and Rectangle, which are all members of the java.awt package. We will use these classes throughout this chapter and in further chapters.

This brings us to the last line of code added to the constructor of the TemplateGraphicsApplication example: setBounds. This method simply defines the size of our window, with four integer values for the x, y, width, and height, respectively. Alternatively, we could also have passed a reference to a Rectangle object to set the bounds, as follows:

JFrame myFrame = new JFrame();   myFrame.setBounds(new Rectangle(0, 0, 400, 400));

We can retrieve the size of the window as follows:

Rectangle bounds = myFrame.getBounds();

You may also set the location and size of the window separately using the methods setLocation and setSize, passing in Point and Dimension objects respectively. You will get used to all of these methods as you progress through the examples and can easily look up these methods in the Java SDK.



Java 1.4 Game Programming
Java 1.4 Game Programming (Wordware Game and Graphics Library)
ISBN: 1556229631
EAN: 2147483647
Year: 2003
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net