Tài liệu Using the PrintJob Class pptx

15 297 0
Tài liệu Using the PrintJob Class pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

< Day Day Up > Using the PrintJob Class The PrintJob class is a built-in Flash class that gives the programmer control over what can be printed in an SWF as well as how it's printed. To use the PrintJob class, a new instance of the class must be created. var myPrintJob:PrintJob = new PrintJob(); With this script, a new instance of the PrintJob class is created and referred to as myPrintJob. To tell the PrintJob object what content to print, you must use the addPage() method of the PrintJob class. We'll get to this soon. Before you can use the addPage() method to add all printable content to the print job, however, you must first call the start() method of the PrintJob class: var myPrintJob:PrintJob = new PrintJob(); var result:Boolean = myPrintJob.start(); The first line of this script creates the instance of the PrintJob class. The second line invokes the start() method. The moment that the start() method is called, a pop-up window (specific to the operating system) asks whether the user wants to proceed with printing. If the user selects Print, the Boolean value true is returned and stored as the value of the variable result. If the user doesn't have a printer installed or cancels the print pop-up window, the Boolean value false is returned. This feature allows you to program an application to react one way if the user successfully initializes the print option, and another if the user cancels the print request or doesn't have a printer. For example: var myPrintJob:PrintJob = new PrintJob(); var result:Boolean = myPrintJob.start(); if (result) { //Successfully initialized print action //Add pages to print here } else { //User does not have printer or user canceled print action } N OTE After the start() method is called but before the user responds to the pop-up window, Flash is paused and no frames are executed. All animations and code halt until the user responds. If the value of result is true, the user has a printer and has chosen the Print option. It's then time to use the addPage() method of the PrintJob class to add the printable content. The syntax for invoking the addPage() method looks like this: myPrintJob.addPage(target, printArea, options, frameNumber); The addPage() method has four parameters: • target. This option defines the timeline where the page lives that you want to print. If this parameter is entered as a number, it's interpreted as pointing to a level of the movie. If the value entered is a string, it points to a movie clip. • printArea. This parameter expects an object with four properties: xMin, xMax, yMin, and yMax. These properties together form a rectangle determining the printable area of the target. All of these measurements are relative to the registration point of the target. For example, if xMin has a value of -300, the left border of the printable area is 300 pixels to the left of the registration point of the target. By default, leaving the printArea parameter blank prints the entire dimensions of the movie clip (or what can fit on the printed page). • options. This setting specifies whether the page should be printed as a bitmap image or as vector graphics. The parameter value needs to be an object with a single property, printAsBitmap. This property has a Boolean value. If true, the page is printed as a bitmap. If false, it's printed as vector graphics. By default, leaving the options parameter blank prints the page as vector graphics. By printing as a bitmap you can print movies that maintain their transparencies and color effects, as shown onscreen. • frameNumber. This parameter is a number specifying the frame within the target to be printed. It works only with frame numbers, not frame labels. By default, if this parameter is blank, the currently displayed frame of the target is printed. All the parameters of the addPage() method are optional except target. Let's look at some examples of this method in use. The following script creates a new PrintJob class instance, starts the print request, and adds a page to be printed. The currently displayed frame of the myClip_mc movie clip instance will be printed as vector graphics. var myPrintJob:PrintJob = new PrintJob(); var result:Boolean = myPrintJob.start(); if (result) { myPrint.addPage("myClip_mc"); } else { //User does not have printer or user canceled print action } To use the default value of a parameter, enter null. The following line adds Frame 5 of the myClip_mc movie clip instance to the PrintJob class instance, and specifies that it should be printed as a bitmap: myPrint.addPage("myClip_mc", null, {printAsBitmap:true}, 5); To specify the dimensions of a movie clip to be printed, you must define the printArea (second parameter) using an object, as shown here: myPrintJob.addPage(0, {xMin:30, xMax:250, yMin:27, yMax:300}); The target is level 0. This addPage() method instructs Flash to print all content in level 0 on the current frame that's shown between the x positions of 30 and 250, and the y positions of 27 and 300. Only content found within these dimensions will be printed. You can add pages from various timelines to a single PrintJob instance, allowing the user to print content from those various timelines from a single Print dialog box: myPrintJob.addPage("invitation_mc", null, {printAsBitmap:true}, 2); myPrintJob.addPage("map_mc", null, {printAsBitmap:false}, 1); myPrintJob.addPage(1, null, {printAsBitmap:true}, null); myPrintJob.addPage("guestList_mc", null, {printAsBitmap:true}, 4); To add all frames of a timeline to a print job, use a looping statement: for(i = 1; i <= myMovieClip_mc._totalframes; ++1){ myPrintJob.addPage("myMovieClip_mc", null, null, i); } With each loop, a page is added to the print job. The current value of i specifies by which frame in the myMovieClip_mc instance to print for that page. This loop continues until i is greater than the number of frames in the instance. TIP Remember that a timeline needn't be visible to add frames from that timeline to a print j ob. This feature allows you to create hidden content in your movie that might only be appropriate for printing purposes, such as coupons, instructions, or maps that don't fit into your project's overall design. After all pages have been added to a PrintJob instance, you invoke the send() method of the PrintJob class to start the printing. myPrintJob.send(); After you're done with the instance of the PrintJob class, you should delete it. delete myPrintJob; TIP Don't leave instances around that no longer have any use; that's a waste of memory: A complete script for creating a print job will look something like this: var myPrintJob:PrintJob = new PrintJob(); var result:Boolean = myPrintJob.start(); if (result) { myPrintJob.addPage("invitation_mc", null, {printAsBitmap:true}, 2); myPrintJob.addPage("map_mc", null, {printAsBitmap:false}, 1); myPrintJob.addPage(1, null, {printAsBitmap:true}, null); myPrintJob.addPage("guestList_mc", null, {printAsBitmap:true}, 4); for(i = 1; i <= myMovieClip_mc._totalframes; ++1){ myPrintJob.addPage("myMovieClip_mc", null, null, i); } myPrintJob.send(); delete myPrintJob; } else { //User does not have printer or user canceled print action } In the following exercise, you'll dynamically select part of an image and print it. 1. Open PrintArea1.fla in the Lesson21/Assets folder. There are four layers on the main timeline: Animals, Interface, PrintArea Box, and Actions. The Animals layer contains six frames of animal pictures, the Interface layer contains the project's main interface graphics, the PrintArea Box layer contains a movie clip instance named box_mc. Frame 1 in the Actions layer is where you'll add all the ActionScript for this exercise. When this exercise is complete, you'll be able to navigate images using the Left Arrow and Right Arrow keys on the keyboard. Clicking an image and dragging down and to the right draws a box on top of the image. As soon as the user releases the mouse button, a print command is sent to print the area of the image that was just selected. The box_mc movie clip is resized as the mouse moves, to give the impression that the user is selecting part of the image. 2. With the Actions panel open, select Frame 1 of the Actions layer and add the following script: 3. 4. stop(); 5. 6. box_mc._visible = false; 7. The first action prevents the timeline from moving forward until we tell it to do so. The second line makes the box_mc movie clip instance invisible initially. We only want it to be shown if the user has pressed the left mouse button and dragged to create a selection. 3. Add the following variable to track the state of the mouse: 4. 5. var down:Boolean = false; 6. The movie initializes with the mouse button up. When the left mouse button is pressed, the down variable changes to true. 4. Add the following four variables to track the print area dimensions: 5. 6. var xMin:Number; 7. 8. var xMax:Number; 9. 10. var yMin:Number; 11. 12. var yMax:Number; 13. To specify the area of a target to print, you specify four values: xMin, xMax, yMin, and yMax. These values are declared here with no value. When the mouse button is pressed, the xMin and yMin values are set. As the mouse moves, the xMax and yMax values are set. 5. Add the following onMouseDown event to handle initializing the selection: 6. 7. this.onMouseDown = function() { 8. 9. down = true; 10. 11. xMin = _xmouse; 12. 13. yMin = _ymouse; 14. 15. box_mc._x = xMin; 16. 17. box_mc._y = yMin; 18. 19. }; 20. When the left mouse button is pressed, the down variable's value is set to true. The minimum selection values, xMin and yMin, are set based on the mouse position when the event occurs. These two values are immediately used to position the box_mc movie clip. 6. To handle scaling the selection as the mouse moves, add the following script: 7. 8. this.onMouseMove = function() { 9. 10. updateAfterEvent(); 11. 12. if (down) { 13. 14. box_mc._visible = true; 15. 16. xMax = _xmouse; 17. 18. yMax = _ymouse; 19. 20. box_mc._width = xMax - xMin; 21. 22. box_mc._height = yMax - yMin; 23. 24. } 25. 26. }; 27. The updateAfterEvent function is a built-in function in Flash. It tells Flash to redraw graphics onscreen after the mouse moves, rather than waiting until the end of the current frame. This technique gives smoother results to objects that change as the mouse moves. Every time the mouse moves, a conditional statement is evaluated. It states that if down is true (as it is when the mouse button is pressed), the box_mc instance is [...]... portion of the elephant graphic, as defined by the selection dimensions, will print After the addPage() action, the function sends the added page to the printer by using the send() method When the send() method is called, the Flash player sends all the added pages (a single page in this case) to the printer to be printed The last line in this function deletes the instance of the PrintJob class because... delete myPrintJob; 28 29 } 30 This function is called after the user has clicked, dragged, and released the mouse button A new instance of the PrintJob class is created and stored as myPrintJob; then the start() method is called, the result of which is stored as the value of the result variable If the value of result is true, the user has a printer and has opted to continue the printing process The addPage()... method is then called and the target timeline to print is set as level 0 The second parameter passes in an object whose properties are the dimensions that were gathered during the selection process The third parameter specifies that the image should be printed as a bitmap The fourth parameter specifies that the frame to print is the current frame where the main timeline resides Thus, if the user has... visible Next, the values of xMax and yMax are set based on the current position of the mouse Finally, the box_mc movie clip instance's dimensions are set based on the distances between the minimum values (captured when the mouse button was pressed) and the maximum values (which change as the user moves the mouse around) The end result is that the box_mc movie clip instance scales as the mouse moves,... When the onKeyDown event occurs, a conditional statement checks whether the key pressed down is the Left Arrow or Right Arrow key If the Left Arrow key is pressed, the main timeline moves to the next frame If the Right Arrow key is pressed, the main timeline moves to the previous frame If any other key is pressed, nothing happens Let's test our work 10 Select Control > Test Movie Select a portion of the. .. area of the image Let's create that function next 8 At the end of the current script, add the following function definition: 9 10 11 function printImage() { 12 13 var myPrintJob :PrintJob = new PrintJob( ); 14 15 var result:Boolean = myPrintJob.start(); 16 17 if (result) { 18 19 myPrintJob.addPage(0, {xMin:xMin, xMax:xMax, yMin:yMin, yMax:yMax}, {printAsBitmap 20 21 :true}, _currentframe); 22 23 myPrintJob.send();... of the image and then release the mouse button to print it After you select a portion of the image, a print window should pop up, opened by your operating system If you proceed with the print job, the selected area of the image will be printed NOTE The ActionScript that we added in this exercise supports selecting a portion of the image by clicking and dragging down and to the right Using more advanced... selecting 7 Add the following script to capture the onMouseUp event: 8 9 this.onMouseUp = function() { 10 11 down = false; 12 13 box_mc._visible = false; 14 15 printImage(); 16 17 }; 18 When the mouse button is released, the down variable is set back to false and the box_mc movie clip becomes invisible again Then the printImage() function is called; this function uses the dimension variables from the selection... case) to the printer to be printed The last line in this function deletes the instance of the PrintJob class because we're done with it The last thing we need to do is add the script to enable the user to navigate the animal images 9 At the end of the current script, add the following function definition: 10 11 this.onKeyDown = function(){ 12 13 if(Key.isDown(Key.RIGHT)){ 14 15 nextFrame(); 16 17 }else... clicking and dragging down and to the right Using more advanced ActionScript, we could have supported selecting in any direction 11 Close the test movie and save your work as PrintArea2.fla You have successfully created a simple application that uses the PrintJob class You can print a specific area of an image with this application < Day Day Up > . < Day Day Up > Using the PrintJob Class The PrintJob class is a built-in Flash class that gives the programmer control over what can. first call the start() method of the PrintJob class: var myPrintJob :PrintJob = new PrintJob( ); var result:Boolean = myPrintJob.start(); The first

Ngày đăng: 21/01/2014, 13:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan