示例#1
0
 /*    */ public static void main(String[] args) /*    */ {
   /* 59 */ System.out.println("");
   /* 60 */ for (int i = 0; i < credits.length; i++) /* 61 */ System.out.println(credits[i]);
   /* 62 */ System.out.println("");
   /*    */ try
   /*    */ {
     /* 65 */ Frame f = new Frame(credits[0]);
     /* 66 */ f.setLayout(new GridLayout(credits.length, 1));
     /* 67 */ for (int i = 0; i < credits.length; i++) /* 68 */ f.add(new Label(credits[i], 1));
     /* 69 */ f.pack();
     /* 70 */ Toolkit t = f.getToolkit();
     /* 71 */ f.setLocation(
         (t.getScreenSize().width - f.getSize().width) / 2,
         (t.getScreenSize().height - f.getSize().height) / 2);
     /* 72 */ f.addWindowListener(
         new WindowAdapter() {
           /*    */ public void windowClosing(WindowEvent e) {
             /* 74 */ System.exit(0);
             /*    */ }
           /* 76 */ });
     /* 77 */ f.show();
     /*    */ }
   /*    */ catch (Exception localException) {
   }
   /*    */ }
示例#2
0
  @SuppressWarnings("deprecation")
  public static void main(String[] args) {
    System.out.println("");
    for (int i = 0; i < credits.length; i++) System.out.println(credits[i]);
    System.out.println("");

    try {
      java.awt.Frame f = new java.awt.Frame(credits[0]);
      f.setLayout(new java.awt.GridLayout(credits.length, 1));
      for (int i = 0; i < credits.length; i++)
        f.add(new java.awt.Label(credits[i], java.awt.Label.CENTER));
      f.pack();
      java.awt.Toolkit t = f.getToolkit();
      f.setLocation(
          (t.getScreenSize().width - f.getSize().width) / 2,
          (t.getScreenSize().height - f.getSize().height) / 2);
      f.addWindowListener(
          new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent e) {
              System.exit(0);
            } // end windowClosed
          }); // end WindowAdapter
      f.show();
    } // end try
    catch (Exception e) {
    }
  } // end main
示例#3
0
  /**
   * The constructor for this class has a bunch of arguments: The frame argument is required for all
   * printing in Java. The jobname appears left justified at the top of each printed page. The font
   * size is specified in points, as on-screen font sizes are. The margins are specified in inches
   * (or fractions of inches).
   */
  public HardcopyWriter(
      Frame frame,
      String jobname,
      int fontsize,
      double leftmargin,
      double rightmargin,
      double topmargin,
      double bottommargin)
      throws HardcopyWriter.PrintCanceledException {
    // Get the PrintJob object with which we'll do all the printing.
    // The call is synchronized on the static printprops object, which
    // means that only one print dialog can be popped up at a time.
    // If the user clicks Cancel in the print dialog, throw an exception.
    Toolkit toolkit = frame.getToolkit(); // get Toolkit from Frame
    synchronized (printprops) {
      job = toolkit.getPrintJob(frame, jobname, printprops);
    }
    if (job == null) throw new PrintCanceledException("User cancelled print request");

    pagesize = job.getPageDimension(); // query the page size
    pagedpi = job.getPageResolution(); // query the page resolution

    // Bug Workaround:
    // On windows, getPageDimension() and getPageResolution don't work, so
    // we've got to fake them.
    if (System.getProperty("os.name").regionMatches(true, 0, "windows", 0, 7)) {
      // Use screen dpi, which is what the PrintJob tries to emulate, anyway
      pagedpi = toolkit.getScreenResolution();
      System.out.println(pagedpi);
      // Assume a 8.5" x 11" page size.  A4 paper users have to change this.
      pagesize = new Dimension((int) (8.5 * pagedpi), 11 * pagedpi);
      System.out.println(pagesize);
      // We also have to adjust the fontsize.  It is specified in points,
      // (1 point = 1/72 of an inch) but Windows measures it in pixels.
      fontsize = fontsize * pagedpi / 72;
      System.out.println(fontsize);
      System.out.flush();
    }

    // Compute coordinates of the upper-left corner of the page.
    // I.e. the coordinates of (leftmargin, topmargin).  Also compute
    // the width and height inside of the margins.
    x0 = (int) (leftmargin * pagedpi);
    y0 = (int) (topmargin * pagedpi);
    width = pagesize.width - (int) ((leftmargin + rightmargin) * pagedpi);
    height = pagesize.height - (int) ((topmargin + bottommargin) * pagedpi);

    // Get body font and font size
    font = new Font("Monospaced", Font.PLAIN, fontsize);
    metrics = toolkit.getFontMetrics(font);
    lineheight = metrics.getHeight();
    lineascent = metrics.getAscent();
    charwidth = metrics.charWidth('0'); // Assumes a monospaced font!

    // Now compute columns and lines will fit inside the margins
    chars_per_line = width / charwidth;
    lines_per_page = height / lineheight;

    // Get header font information
    // And compute baseline of page header: 1/8" above the top margin
    headerfont = new Font("SansSerif", Font.ITALIC, fontsize);
    headermetrics = toolkit.getFontMetrics(headerfont);
    headery = y0 - (int) (0.125 * pagedpi) - headermetrics.getHeight() + headermetrics.getAscent();

    // Compute the date/time string to display in the page header
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
    df.setTimeZone(TimeZone.getDefault());
    time = df.format(new Date());

    this.jobname = jobname; // save name
    this.fontsize = fontsize; // save font size
  }
示例#4
0
文件: Tiny.java 项目: srnsw/xena
 /** Locates the frame on the screen center. */
 private void locateOnScreen(Frame frame) {
   Dimension paneSize = frame.getSize();
   Dimension screenSize = frame.getToolkit().getScreenSize();
   frame.setLocation(
       (screenSize.width - paneSize.width) / 2, (screenSize.height - paneSize.height) / 2);
 }
示例#5
0
  private static void init() {
    String[] instructions = {
      "To be able to run this test it is required to have a default",
      "printer configured in your user environment.",
      "If no default printer exists, then test passes.",
      " ",
      "There will be 2 print dialogs.  The first dialog should show",
      "portrait as the selected orientation.  The 2nd dialog should show",
      "landscape as the selected orientation.",
      " ",
      "Visual inspection of the printed pages is needed. A passing",
      "test will print 2 pages in portrait and 2 pages in landscape.",
      "The pages have on the center of the page the text \"Center\"",
      "2 rectangles will appear above and below it, the one below is",
      "filled."
    };
    Sysout.createDialog();
    Sysout.printInstructions(instructions);

    PrintJob job = null;
    Dimension dim = null;
    JobAttributes jobAttributes = new JobAttributes();
    PageAttributes pageAttributes = new PageAttributes();
    String center = "Center";
    Font font = new Font("SansSerif", Font.PLAIN, 200);
    FontMetrics metrics = null;
    int width = 0;
    Graphics g = null;

    jobAttributes.setDialog(DialogType.NATIVE);
    pageAttributes.setOrigin(OriginType.PRINTABLE);
    pageAttributes.setPrinterResolution(new int[] {1200, 1200, 3});
    pageAttributes.setOrientationRequested(OrientationRequestedType.PORTRAIT);
    jobAttributes.setSides(SidesType.TWO_SIDED_LONG_EDGE);

    job = f.getToolkit().getPrintJob(f, "Portrait Test", jobAttributes, pageAttributes);
    if (job != null) {
      dim = job.getPageDimension();
      for (int i = 0; i < 2; i++) {
        g = job.getGraphics();

        g.drawLine(0, 0, dim.width, 0);
        g.drawLine(dim.width, 0, dim.width, dim.height);
        g.drawLine(dim.width, dim.height, 0, dim.height);
        g.drawLine(0, dim.height, 0, 0);

        g.drawRect(dim.width / 2 - 200, dim.height / 3 - 300, 400, 600);
        g.fillRect(dim.width / 2 - 200, 2 * dim.height / 3 - 300, 400, 600);

        g.setFont(font);
        metrics = g.getFontMetrics();
        width = metrics.stringWidth(center);
        g.setColor(Color.black);
        g.drawString(center, (dim.width / 2) - (width / 2), dim.height / 2);

        g.dispose();
      }
      job.end();
      job = null;
    }

    pageAttributes.setOrientationRequested(OrientationRequestedType.LANDSCAPE);

    job = f.getToolkit().getPrintJob(f, "Landscape Test", jobAttributes, pageAttributes);
    if (job != null) {
      dim = job.getPageDimension();
      for (int i = 0; i < 2; i++) {
        g = job.getGraphics();
        g.drawLine(0, 0, dim.width, 0);
        g.drawLine(dim.width, 0, dim.width, dim.height);
        g.drawLine(dim.width, dim.height, 0, dim.height);
        g.drawLine(0, dim.height, 0, 0);

        g.drawRect(dim.width / 2 - 200, dim.height / 3 - 300, 400, 600);
        g.fillRect(dim.width / 2 - 200, 2 * dim.height / 3 - 300, 400, 600);

        g.setFont(font);
        metrics = g.getFontMetrics();
        width = metrics.stringWidth(center);
        g.setColor(Color.black);
        g.drawString(center, (dim.width / 2) - (width / 2), dim.height / 2);

        g.dispose();
      }
      job.end();
      job = null;
    }
    System.out.println("done");
  }
  /**
   * Creates a new <code>java.awt.Frame</code>. This frame is the root for the AWT components that
   * will be embedded within the composite. In order for the embedding to succeed, the composite
   * must have been created with the SWT.EMBEDDED style.
   *
   * <p>IMPORTANT: As of JDK1.5, the embedded frame does not receive mouse events. When a
   * lightweight component is added as a child of the embedded frame, the cursor does not change. In
   * order to work around both these problems, it is strongly recommended that a heavyweight
   * component such as <code>java.awt.Panel</code> be added to the frame as the root of all
   * components.
   *
   * @param parent the parent <code>Composite</code> of the new <code>java.awt.Frame</code>
   * @return a <code>java.awt.Frame</code> to be the parent of the embedded AWT components
   * @exception IllegalArgumentException
   *     <ul>
   *       <li>ERROR_NULL_ARGUMENT - if the parent is null
   *       <li>ERROR_INVALID_ARGUMENT - if the parent Composite does not have the SWT.EMBEDDED style
   *     </ul>
   *
   * @since 3.0
   */
  public static Frame new_Frame(final Composite parent) {
    if (parent == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
    if ((parent.getStyle() & SWT.EMBEDDED) == 0) {
      SWT.error(SWT.ERROR_INVALID_ARGUMENT);
    }
    int /*long*/ handle = parent.embeddedHandle;
    /*
     * Some JREs have implemented the embedded frame constructor to take an integer
     * and other JREs take a long.  To handle this binary incompatibility, use
     * reflection to create the embedded frame.
     */
    Class clazz = null;
    try {
      String className =
          embeddedFrameClass != null ? embeddedFrameClass : "sun.awt.X11.XEmbeddedFrame";
      clazz = Class.forName(className);
    } catch (Throwable e) {
      SWT.error(SWT.ERROR_NOT_IMPLEMENTED, e, " [need JDK 1.5 or greater]");
    }
    initializeSwing();
    Object value = null;
    Constructor constructor = null;
    try {
      constructor = clazz.getConstructor(new Class[] {int.class, boolean.class});
      value =
          constructor.newInstance(new Object[] {new Integer((int) /*64*/ handle), Boolean.TRUE});
    } catch (Throwable e1) {
      try {
        constructor = clazz.getConstructor(new Class[] {long.class, boolean.class});
        value = constructor.newInstance(new Object[] {new Long(handle), Boolean.TRUE});
      } catch (Throwable e2) {
        SWT.error(SWT.ERROR_NOT_IMPLEMENTED, e2);
      }
    }
    final Frame frame = (Frame) value;
    parent.setData(EMBEDDED_FRAME_KEY, frame);
    if (Device.DEBUG) {
      loadLibrary();
      setDebug(frame, true);
    }
    try {
      /* Call registerListeners() to make XEmbed focus traversal work */
      Method method = clazz.getMethod("registerListeners", null);
      if (method != null) method.invoke(value, null);
    } catch (Throwable e) {
    }
    final AWTEventListener awtListener =
        new AWTEventListener() {
          public void eventDispatched(AWTEvent event) {
            if (event.getID() == WindowEvent.WINDOW_OPENED) {
              final Window window = (Window) event.getSource();
              if (window.getParent() == frame) {
                parent
                    .getDisplay()
                    .asyncExec(
                        new Runnable() {
                          public void run() {
                            if (parent.isDisposed()) return;
                            Shell shell = parent.getShell();
                            loadLibrary();
                            int /*long*/ awtHandle = getAWTHandle(window);
                            if (awtHandle == 0) return;
                            int /*long*/ xWindow =
                                OS.gdk_x11_drawable_get_xid(
                                    OS.GTK_WIDGET_WINDOW(OS.gtk_widget_get_toplevel(shell.handle)));
                            OS.XSetTransientForHint(
                                OS.gdk_x11_display_get_xdisplay(OS.gdk_display_get_default()),
                                awtHandle,
                                xWindow);
                          }
                        });
              }
            }
          }
        };
    frame.getToolkit().addAWTEventListener(awtListener, AWTEvent.WINDOW_EVENT_MASK);
    final Listener shellListener =
        new Listener() {
          public void handleEvent(Event e) {
            switch (e.type) {
              case SWT.Deiconify:
                EventQueue.invokeLater(
                    new Runnable() {
                      public void run() {
                        frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_DEICONIFIED));
                      }
                    });
                break;
              case SWT.Iconify:
                EventQueue.invokeLater(
                    new Runnable() {
                      public void run() {
                        frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_ICONIFIED));
                      }
                    });
                break;
            }
          }
        };
    Shell shell = parent.getShell();
    shell.addListener(SWT.Deiconify, shellListener);
    shell.addListener(SWT.Iconify, shellListener);

    Listener listener =
        new Listener() {
          public void handleEvent(Event e) {
            switch (e.type) {
              case SWT.Dispose:
                Shell shell = parent.getShell();
                shell.removeListener(SWT.Deiconify, shellListener);
                shell.removeListener(SWT.Iconify, shellListener);
                parent.setVisible(false);
                EventQueue.invokeLater(
                    new Runnable() {
                      public void run() {
                        frame.getToolkit().removeAWTEventListener(awtListener);
                        frame.dispose();
                      }
                    });
                break;
              case SWT.Resize:
                if (Library.JAVA_VERSION >= Library.JAVA_VERSION(1, 6, 0)) {
                  final Rectangle clientArea = parent.getClientArea();
                  EventQueue.invokeLater(
                      new Runnable() {
                        public void run() {
                          frame.setSize(clientArea.width, clientArea.height);
                        }
                      });
                }
                break;
            }
          }
        };
    parent.addListener(SWT.Dispose, listener);
    parent.addListener(SWT.Resize, listener);

    parent
        .getDisplay()
        .asyncExec(
            new Runnable() {
              public void run() {
                if (parent.isDisposed()) return;
                final Rectangle clientArea = parent.getClientArea();
                EventQueue.invokeLater(
                    new Runnable() {
                      public void run() {
                        frame.setSize(clientArea.width, clientArea.height);
                        frame.validate();
                      }
                    });
              }
            });
    return frame;
  }