|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
The pictureBox properties allow you a lot of control in determining how your pictureBox displays the image and responds to users. Table 12.3 provides an easy reference to each property name, its description, and its default.
Referring to the image property: if you click the button on the right part of the field, it brings up a Selector box with which you can select the image that will be displayed. Now, suppose I have just selected a JPG file. As you can see in Figure 12.6, when you select an image file, it shows up in your control.
In some of the examples, we will change the visible property to False so that we can do our own image drawing and manipulation; but for most of your uses, this will always be set to True.
Displaying ImagesTo draw images, youll use the Graphics classs drawImage() method. There are several variations of this, but the easiest one to use takes three arguments: the first argument as an Image class, the second argument as the x coordinate of the upper-left corner of the image, and the third argument as the y coordinate of the upper-left corner of the image. The following examples show you how to use this easiest form of drawImage() in both an application and an applet: // Application protected void onPaint( PaintEvent pe ) { // m_Image is an Image class that's already loaded int x = 10, y = 10; pe.graphics.drawImage( m_Image, x, y ); } // Applet public void paint( Graphics g ) { // m_Image is an Image class that's already loaded int x = 10, y = 10; g.drawImage( m_Image, x, y, this ); } Figure 12.7 (shown after Listing 12.1) shows an application. The application used a pictureBox to load the image of the sailboats, but then the visible property of the pictureBox was set to False. Our first action, then, used the pictureBoxs getImage() method to get the image. Next we used the drawImage() method and the onPaint() method to draw this image ourselves. So rather than rely on pictureBoxs native drawing capabilities, we went ahead and drew the image ourselves. In this case, it was just to show you how to draw an image in an application. However, in the future at times youll want more control than a pictureBox offers, and youll want to draw the image yourself. The source code for an entire application is given in Listing 12.1.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |