加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Using Headless Mode in the Java SE Platform--转

发布时间:2020-12-14 06:21:50 所属栏目:Java 来源:网络整理
导读:原文地址: ?is a system configuration in which the display device,keyboard,or mouse is lacking. Sounds unexpected,but actually you can perform different operations in this mode,even with graphic data. ?system for further rendering. ?class

原文地址:

?is a system configuration in which the display device,keyboard,or mouse is lacking. Sounds unexpected,but actually you can perform different operations in this mode,even with graphic data.

?system for further rendering.

?class is an abstract superclass of all actual implementations of the Abstract Window Toolkit (AWT). Subclasses of Toolkit are used to bind the various AWT components to particular native toolkit implementations.

?class is an abstract class that describes the collection of?objects and??objects available to a Java technology application on a particular platform. The resources in this??might be local or on a remote machine.??objects can be monitors,printers,or image buffers and are the destination of??drawing methods. Each??has many?objects associated with it. These objects specify the different configurations in which the??can be used.

?methods that check for headless mode support.

?
,a??is thrown from areas of the??and?classes that depend on a display device,or mouse. ?can support a display device,or mouse. If this method returns??is thrown from areas of the?that depend on a display device,or mouse.

?method checks the specific system property,?,instead of the system's hardware configuration.

?is thrown when code that depends on a display device,or mouse is called in an environment that does not support any of these. The exception is derived from an?,which is itself derived from a?.

method. This method enables you to set the desired value for the system property that is indicated by the specific key.

System.setProperty("java.awt.headless","true"); ?is a system property,and??is a value that is assigned to it.

java -Djava.awt.headless=true

?is set to??method of the??class to create an instance of headless toolkit:

Toolkit tk = Toolkit.getDefaultToolkit();

?method of the?class:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); boolean headless_check = ge.isHeadless(); ?system property. If this property has a??value,then a??will be thrown from areas of the??classes that are dependent on a display device,or mouse.

  • ,monaco; color: #666666;">java.awt.print.*,monaco; color: #666666;">javax.print.*?classes

?class.

final Canvas c = new Canvas() { public void paint(Graphics g) { Rectangle r = getBounds(); g.drawLine(0,r.width - 1,r.height - 1); g.drawLine(0,r.height - 1,0); } };

?class to draw a text string. The??object is used to render this string.

public void paint(Graphics g) { g.setFont(new Font("Arial",Font.ITALIC,12)); g.drawString("Test",32,8); }

?object is used to render this line.

public void paint(Graphics g) { g.setColor(new Color(255,127,0)); g.drawLine(0,0); }

?method of the??class decodes the?image file shown in Figure 1 and returns a buffered image.

Image i = null; try { File f = new File("grapefruit.jpg"); i = ImageIO.read(f); } catch (Exception z) { z.printStackTrace(System.err); }

?Image File <p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; list-style: none; -webkit-font-smoothing: antialiased; line-height: 16px; font-size: 12px; font-family: Arial,sans-serif;"><strong style="margin: 0px; padding: 0px; list-style: none; -webkit-font-smoothing: antialiased;">Print


<p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; list-style: none; -webkit-font-smoothing: antialiased; line-height: 16px; font-size: 12px; font-family: Arial,sans-serif;">This code shows how to print a prepared canvas that enables you to define the printer as the default surface for the?<code style="margin: 0px; padding: 0px; list-style: none; -webkit-font-smoothing: antialiased; font-family: courier,monaco; color: #666666;">paint
?method.


<table class="grey4" style="margin-right: 0px; margin-left: 0px; padding: 0px; list-style: none; -webkit-font-smoothing: antialiased; color: #000000; font-family: Arial,sans-serif; font-size: 12px; vertical-align: top;">

PrinterJob pj = PrinterJob.getPrinterJob();
       pj.setPrintable(new Printable()
       {
           public int print(Graphics g,PageFormat pf,int pageIndex)
           {
               if (pageIndex > 0)
               {
                   return Printable.NO_SUCH_PAGE;
               }
           ((Graphics2D)g).translate(pf.getImageableX(),pf.getImageableY());

           // Paint canvas.
           c.paint(g);

           return Printable.PAGE_EXISTS;
       }
   });

?class's?method.

Toolkit tk = Toolkit.getDefaultToolkit(); tk.beep();

?example uses all the capabilities described in this article.

?compiler. Copy the image fileto the directory that contains the??class.

import java.awt.*; import java.io.*; import java.awt.print.*;

import javax.imageio.*;

public class HeadlessBasics
{
public static void main(String[] args)
{
// Set system property.
// Call this BEFORE the toolkit has been initialized,that is,// before Toolkit.getDefaultToolkit() has been called.
System.setProperty("java.awt.headless","true");

    // This triggers creation of the toolkit.
    // Because java.awt.headless property is set to true,this 
    // will be an instance of headless toolkit.
    Toolkit tk = Toolkit.getDefaultToolkit();
    // Standard beep is available.
    tk.beep();

    // Check whether the application is
    // running in headless mode.
    GraphicsEnvironment ge = 
    GraphicsEnvironment.getLocalGraphicsEnvironment();
    System.out.println("Headless mode: " + ge.isHeadless());

    // No top levels are allowed.
    boolean created = false;
    try
    {
        Frame f = new Frame("Frame");
        created = true;
    }
    catch (Exception z)
    {
        z.printStackTrace(System.err);
        created = false;
    }
    System.err.println("Frame is created: " + created);

    // No other components except Canvas and Panel are allowed.
    created = false;
    try
    {
        Button b = new Button("Button");
        created = true;
    }
    catch (Exception z)
    {
        z.printStackTrace(System.err);
        created = false;
    }
    System.err.println("Button is created: " + created);

    // Canvases can be created.
    final Canvas c = new Canvas()
    {
        public void paint(Graphics g)
        {
            Rectangle r = getBounds();
            g.drawLine(0,r.height - 1);
            // Colors work too.
            g.setColor(new Color(255,0);
            // And fonts
            g.setFont(new Font("Arial",8);
        }
    };
    // And all the operations work correctly.
    c.setBounds(32,128,128);

    // Images are available.
    Image i = null;
    try
    {
        File f = new File("grapefruit.jpg");
        i = ImageIO.read(f);
    }
    catch (Exception z)
    {
        z.printStackTrace(System.err);
    }
    final Image im = i;

    // Print system is available.
    PrinterJob pj = PrinterJob.getPrinterJob();
    pj.setPrintable(new Printable()
    {
        public int print(Graphics g,int pageIndex)
        {
            if (pageIndex > 0)
            {
                return Printable.NO_SUCH_PAGE;
            }
            ((Graphics2D)g).translate(pf.getImageableX(),pf.getImageableY());

            // Paint the canvas.
            c.paint(g);

            // Paint the image.
            if (im != null)
            {
                g.drawImage(im,64,null);
            }

            return Printable.PAGE_EXISTS;
        }
    });
    try
    {
        pj.print();
    }
    catch (Exception z)
    {
        z.printStackTrace(System.err);
    }
}

}

?Printed Output <p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; list-style: none; -webkit-font-smoothing: antialiased; line-height: 16px; font-size: 12px; font-family: Arial,sans-serif;">Also,you can expect to see the following messages:


<table class="grey4" style="margin-right: 0px; margin-left: 0px; padding: 0px; list-style: none; -webkit-font-smoothing: antialiased; color: #000000; font-family: Arial,sans-serif; font-size: 12px; vertical-align: top;">

Headless mode: true
java.awt.HeadlessException
at java.awt.GraphicsEnvironment.checkHeadless(Unknown Source)
at java.awt.Window.(Unknown Source)
at java.awt.Frame.(Unknown Source)
at HeadlessBasics.main(HeadlessBasics.java:24)
Frame is created: false
java.awt.HeadlessException
at java.awt.GraphicsEnvironment.checkHeadless(Unknown Source)
at java.awt.Button.(Unknown Source)
at HeadlessBasics.main(HeadlessBasics.java:39)
Button is created: false
s.

java HeadlessBasics 2> standard_output.txt
and replace them with the methods and classes that are independent of headless mode.

. It will be thrown first among the other possible exceptions. That is why the code sample in the section "Example: Using Headless Mode" contains no special requirement to catch the other exceptions.

?is a Sun Microsystems software engineer in Saint Petersburg,Russia. He has been working in the Abstract Window Toolkit (AWT) project for several years,with his primary functional areas in modality,robot,and multiscreen systems.

?is a Sun Microsystems technical writer in Saint Petersburg,Russia. She supports documentation for the AWT project and updates the Java Tutorial. Prior to her assignment for Sun,she worked as a technical writer for eight years.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!