Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Powerful Printing in Flash MX 2004 : Page 3

ActionScript 2.0's new PrintJob class allows you to add robust printing support to your Flash-based Web applications. Learn how to use this new feature—along with a code design that allows on-the-fly interface assembly on any movie clip.


advertisement
Adding Drawing Code
When the user clicks on the drawing interface (drawArea), look up the color selected, create a new Circle object, and then add the circle to the array of circles:

drawArea.onPress = function() { var c; switch (colorGroup.getValue()) { case "red": c = 0xFF0000; break; case "green": c = 0x00FF00; break; case "blue": c = 0x0000FF; break; } var circle:Circle = new Circle(drawArea._xmouse,drawArea._ymouse,c); circles.push(circle);

Note that you have not actually drawn anything, but just stored the information about the new circle. The drawing comes in the very next line that concludes the function:

update(drawArea); };

What's going on here? You've delegated drawing to the update() function, sending in the name of the clip in which the circles are to be drawn—in this case, drawArea. The update() function does the actual drawing:


// draws the circles on the movie clip provided function update(mc) { for (var i=0;i<circles.length;i++) { // make a copy of the 'circle' movie mc.attachMovie("circle","circle" + i,i); // get a reference to this new clip var circleClip = mc["circle" + i]; // move the new clip circleClip._x = circles[i].getX(); circleClip._y = circles[i].getY(); var clipColor = new Color(circleClip); clipColor.setRGB(circles[i].getColor()); } }

Note that this function takes a movie clip as an argument, so it can draw circles on any movie clip, not just the drawArea clip. By organizing your code in this fashion, you avoid repeating the code for drawing the circles. That is, you can use this same method for preparing your printouts of the drawing.

Here is the print function, wired to the "print" button on the main interface. As you can see, this function starts off with two calls to update(), once for each of the print pages:

printButton.onPress = function() { // prepare the printing area update(printArea1.printPreview); update(printArea2); // shrink the preview printArea1.printPreview._xscale = 25; printArea1.printPreview._yscale = 25; printArea1.dateField.text = new Date(); var printJob:PrintJob = new PrintJob(); if (printJob.start()) { var numPages:Number = 0; if (printJob.addPage(printArea1)) { numPages++; } if (printJob.addPage(printArea2)) { numPages++; } if (numPages > 0) { printJob.send(); } delete printJob; } }

Figure 2. The Offscreen Clips: Pressing the print button copies the drawing twice to the offscreen clips.

After the calls to update(), which render the circles on the offscreen movie clips, shrink the print preview movie clip to one quarter og its original dimensions so that it appears smaller—like a preview should. The rest is just the PrintJob boilerplate code from above. So when you press the print button, two new copies of the drawing are made to the offscreen clips. Figure 2 shows a screenshot taken from within the Flash Player that exposes these offscreen areas.

For a closer look, the entire source code is shown in Listing 1.

The code that draws the user interface also handles the assembly of the print interfaces. The result is less code, clearer design, and a more maintainable application. The strategy of isolating the drawing code from any specific references to where to draw is a common design practice and can be useful in many Flash applications. Java developers may recognize this same design scheme in the Java Abstract Window Toolkit (AWT), where the paint() method that draws interfaces always takes a Graphics object that tells Java where to draw. It is a design pattern that—among other things—works well in Flash and makes printing a lot easier.



Eric Jansson is Assistant Director of Systems and Development for the ACS Technology Center, a regional center supporting the development and use of technology in higher education. His work has focused on commercial and open-source application development in the field of secondary and post-secondary education.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap