Java Reference
In-Depth Information
(200,100)
(0,0)
(1365,0)
x
(0.767)
(1365,767)
y
FIGURE 9.2: Pixels in a screen.
typical resolution for a 15” monitor. A pixel is just a dot on the screen. In other words, the
screen is comprised of a rectangular grid of pixels.
Figure 9.2 shows the coordinates of the different pixels of the screen. This is similar
to the standard x/y axes from geometry, where the only difference is that the y axis goes
down instead of up. The top left corner of the screen is pixel (0,0). Assuming we have 1366
by 768 pixel screen, the bottom right corner will have coordinates (1365, 767) (remember
that counting starts at 0). The pixel that is shown in the figure has coordinates (200,100).
Of course, no program should assume a specific screen resolution. Instead, a well-designed
program should dynamically determine the current resolution. Here is an example code that
prints the number of vertical and horizontal pixels of the current resolution.
Dimension scrnsize = Toolkit . getDefaultToolkit () . getScreenSize () ;
System. out . println ( "The height in pixels is: " +scrnsize . height) ;
System. out . println ( "The width in pixels is: " +scrnsize . width) ;
We are now ready to explain how the setLocation and setSize methods work. The
setLocation method specifies the top left location of the window. The x value is specified
first and the y value is specified second. The statement:
setLocation(50,100)
means that the window will be placed 50 pixels to the right of the top left corner of the
screen and 100 pixels down from it. The statement:
setSize (488 , 600)
defines the size of the window. The width of the window will be 488 pixels, while the height
of the window will be 600 pixels. Note that in the initial code, the numbers were defined as
constants in order to make the code more versatile.
The last line of the constructor, setResizable(false) , makes the window non-resizable.
By default, a window is resizable. We decided to make the window non-resizable in order
to restrict the movement of the ball to the specified window size.
 
Search WWH ::




Custom Search