home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


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.

Table 12.3 The pictureBox Properties

Property Name Description Default

allowDrop Determines whether the control will receive drag/drop notifications. False
anchor Defines at which edges of the container a certain control is found. When a control is anchored to the edge, the distance between the control’s closest edge and the specified edge will remain constant. Top Left
backgroundColor Displays text and graphics in a control. Background
Gray
borderStyle Controls which type of border the pictureBox should have. None
contextMenu A pop-up menu to show when the user right-clicks the control. None
cursor Determines which cursor will appear when the mouse passes over the control. Normal
dock Determines the docking location of the control. None
enabled Indicates whether the control is enabled. True
image Determines the image that’s displayed in the pictureBox. None
location Determines the position of the upper-left corner of the control with respect to its container. 0, 0
modifiers Indicates the visibility level of the object. None
name Indicates the name used in code to identify the object. (See following Note box.) None
size Determines the width and height of the pictureBox. Normal
sizeMode Controls how the pictureBox will handle image placement and control sizing. Draws the image inside of the box without resizing the box in any way or scaling the image. Normal
centerImage Centers the image in the window. False
tabIndex Determines the index in the tab order that this control will occupy. 1
visible Determines whether the control is visible or hidden. True

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.


Figure 12.6  When you select an image file, it shows up in a 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.


Note:  In the name field, you can name your file anything you want. I recommend that you name it something that’s meaningful to your program for easier reference.
For the Ambitious Reader

In the section before this, where we talked about getting images in a Java applet, we used the getImage() method to load in an image from some URL out on the Internet. Well, in an application, you’re not going to be loading images off of Internet servers, but you might want to get the same kind of access to an image that you get when you’re working with Image classes. Image classes will give you a lot more flexibility because you can use the image to draw many times, you can do image processing, and you can do things the pictureBox control won’t let you do. The pictureBox control has its own getImage() method. You can use this getImage() method just like you used the getImage() method in an applet. The following example shows you how to obtain the image from a pictureBox in an application:

// The pictureBox1 control already contains an image.
Image img = pictureBox1.getImage();

Displaying Images

To draw images, you’ll use the Graphics class’s 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 pictureBox’s 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 pictureBox’s 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 you’ll want more control than a pictureBox offers, and you’ll want to draw the image yourself. The source code for an entire application is given in Listing 12.1.


Previous Table of Contents Next


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.