How to Use the graphics class in an application?

Source: Internet
Author: User
Tags call back print format

I recently learned how to print in Java, but how to tune the format and paper is also difficult. I found the following article, first, and then I will study it.

 

Link: http://www.javanb.com/j2se/1/18480.html

 

<Table cellspacing = 0 cellpadding = 0 border = 0 class = "zh114" align = "right"> <tr> <TD> </tr> </table> <br/>

Printjob, printable class:
Printing mainly uses the print callback function of the printjob class to print all the work. The printjob class is an abstract class and cannot be used to create a new instance. However, you can get a printjob handle by using its static method getprintjob. With such a joystick, we can enable it at any time and call it "Print it to me !" (Print), if so, it will ask you: "What do you want me to print ?", That is, to call back your printed content. If you do not have the printed content, it will throw a sentence: "Nothing !" (Printexception ). Otherwise, it will be printed faithfully as needed.
Therefore, you should provide a printable content (printable ). There is only one method you need to implement:
Print (Graphics g, pageformat PF, int pageindex );
All your print control is actually implemented in this method. Provide a simple example:
  Import Java. AWT. graphics; <br/> Import Java. AWT. print. *; <br/> public class testprint {<br/> Public static void main (string [] ARGs) {<br/> printjob job = printjob. getprintjob (); // get the handle. <Br/> job. setprintable (New Hello (); // tell the job where to find the printed content. <Br/> try {<br/> job. Print (); // print it to me! <Br/>} catch (printexception e) {<br/> system. Out. println ("nothing !"); // Or printer hardware problems. <Br/>}< br/> class Hello implements printable {// printable content. <Br/> Public int print (Graphics g, // White Paper that can be printed at will. <Br/> pageformat PF, // print format, default. <Br/> int pageindex) {// page number, starting from 0. <Br/> If (pageindex! = 0) return no_such_page; // stop printing. <Br/> G. drawstring ("Hello World !", 100,100); // draw a string on paper. <Br/> return page_exits; // print again. <Br/>}< br/>   

How is it? It's easy enough. Use your imagination and draw your favorite things on graphics!
  
Pageformat, paper class:
In the parameter list of the print () method of printable in this example, the pageformat class is a bit unfamiliar. As the name suggests, it is the print format. During the printing process, the selected horizontal and vertical pages are controlled by pageformat. The default format is letter paper, and the general page margins are obtained through the defaultpage of printjob ). After you get the pageformat instance, you can easily set the printing area (the size of the paper cannot be set directly) and the printing direction. How can I change the size of the paper? The page settings provided by Java have several typical paper types (such as A4 and A3). to customize the paper size, you can obtain the paper instance name from the getpaper () method in the pageformat instance, then, use setsize () of paper to change the actual size of the paper. Let's take a look at the following example:
  
  Import Java. AWT. *; <br/> Import Java. AWT. print. *; <br/> public class testformat {<br/> Public static void main (string [] ARGs) {<br/> printerjob job = printjob. getprinterjob (); <br/> pageformat = job. defaultpage (); // obtain the response page format <br/> double inch = 72; // 1 inch <br/> paper = pageformat. getpaper (); // obtain the paper in the page format <br/> paper. setsize (5 * inch, 5 * inch );/ /The paper size is 5 inch. <br/> pageformat. setimageablearea (<br/> inch, // coordinate X, that is, the left margin is 1 inch <br/> inch, // coordinate Y, that is, the top margin is 1 inch <br/> paper. getwidth ()-2 * inch, // width, 5 inch-2 inch (about 1 inch) <br/> paper. getheight ()-2 * inch); // height, 5 inch-2 inch (up and down each 1 inch) <br/> pageformat. setpaper (paper); // use the paper as the format <br/> job. setprintable (New rect (), pageformat); // not only the printed content, but also the format <br/> If (job. printdialog () {// The familiar print Setting Dialog Box. If OK, true and canc are returned. El returns false <br/> try {<br/> job. print (); <br/>}catch (printerexception e) {<br/> system. out. println (E); <br/>}< br/> class rect implements printable {<br/> Public int print (gaphics G, pageformat, int pageindex) {<br/> If (pageindex! = 0) return no_such_page; <br/> G. fillrect (100,100, 100); // draw a square <br/> return page_exists; <br/>}< br/>   

Compared with testprint. java, the current example is much more complicated. All newly added content has comments. I wanted to print a 100 square, but when you rush to get the printed result, you can only get a small square (actually 100-72 = 28 ). Because you draw from the top left corner of the paper, but there is a 1 inch margin that makes your idea smaller.
  
If what you print is in a uniform format (most of the time), printing becomes so simple. The key is that what you draw on paper is based on the page number (pageindex) can print different things.
  
Lesson: add the statement (no_such_page) to control the print end in the print () method, otherwise the printer will tirelessly beat it to its position. There are more than a dozen printers in my drawer. If I didn't force off the power, it would be far more than enough.
  
The constructor of the preview class is as follows:
  Public printpreviewer (pageable P, int page) {<br/> pageable = P; <br/> pageindex = page; <br/> printcomponent = new printcomponent (null, null ); <br/> printcomponent. setborder (borderfactory. createbevelborder (<br/> bevelborder. raised); <br/> buildlayout (); <br/> displaypage (pageindex); <br/>}< br/> <br/> protected void buildlayout () {<br/> setlayout (New borderlayout (); <br/> jpanel Panel = new jpanel (); <br/> panel. setlayout (New flowlayout (flowlayout. center, 10, 10); <br/> panel. add (printcomponent); <br/> scrollpane = new jscrollpane (panel); <br/> Add (scrollpane, borderlayout. center); <br/> Add (getbottompanel (), borderlayout. south); <br/> addlisteners (); <br/>}< br/>   
<Table width = "96%"> <tr> <TD background = "http: /// images/dian.gif "Height =" 3 "> </TD> </tr> </table> <br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.