Пример #1
0
 /**
  * Create possibly volatile scratch image for fast painting. A scratch image can become
  * invalidated, when this happens any actions involving it are ignored, and it needs to be
  * recreated. Use isScratchImageValid() to check this.
  */
 public static Image createScratchImage(int width, int height) {
   try {
     Image img =
         (Image)
             tryMethod(
                 output_comp,
                 "createVolatileImage",
                 new Object[] {new Integer(width), new Integer(height)});
     if (img == null) {
       // no such method -> create regular image
       return output_comp.createImage(width, height);
     }
     // if (img.validate(output_comp.getGraphicsConfiguration())
     // == VolatileImage.IMAGE_INCOMPATIBLE) {
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
     GraphicsDevice gs = ge.getDefaultScreenDevice();
     GraphicsConfiguration gc = gs.getDefaultConfiguration();
     Integer valid = (Integer) tryMethod(img, "validate", new Object[] {gc});
     // output_comp.getGraphicsConfiguration() });
     if (valid.intValue() == 2) { // I checked, IMAGE_INCOMPATIBLE=2
       // Hmm, somehow it didn't work. Create regular image.
       return output_comp.createImage(width, height);
     }
     return img;
   } catch (java.security.AccessControlException e) {
     // we're not allowed to do this (we're probably an applet)
     return output_comp.createImage(width, height);
   }
 }
Пример #2
0
  /**
   * Creates a device compatible BufferedImage
   *
   * @param width the width in pixels
   * @param height the height in pixels
   */
  public static BufferedImage getDeviceCompatibleImage(int width, int height) {

    GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice screenDevice = graphicsEnvironment.getDefaultScreenDevice();
    GraphicsConfiguration graphicConfiguration = screenDevice.getDefaultConfiguration();
    BufferedImage image = graphicConfiguration.createCompatibleImage(width, height);

    return image;
  }
Пример #3
0
 /** Create empty image with given alpha mode that should be efficient on this display */
 public static BufferedImage createCompatibleImage(int width, int height, int transparency) {
   // try {
   GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
   GraphicsDevice gs = ge.getDefaultScreenDevice();
   GraphicsConfiguration gc = gs.getDefaultConfiguration();
   // always use bitmask transparency
   BufferedImage bimage = gc.createCompatibleImage(width, height, transparency);
   return bimage;
   // } catch (HeadlessException e) { // this exception is not in 1.2
   // The system does not have a screen
   //	e.printStackTrace();
   //	return null;
   // }
 }
Пример #4
0
 public Point getPreferredLocation() {
   if (!IJ.isJava14()) return new Point(0, 0);
   GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
   Rectangle maxBounds = ge.getMaximumWindowBounds();
   int ijX = Prefs.getInt(IJ_X, -99);
   int ijY = Prefs.getInt(IJ_Y, -99);
   if (ijX >= 0 && ijY > 0 && ijX < (maxBounds.x + maxBounds.width - 75))
     return new Point(ijX, ijY);
   Dimension tbsize = toolbar.getPreferredSize();
   int ijWidth = tbsize.width + 10;
   double percent = maxBounds.width > 832 ? 0.8 : 0.9;
   ijX = (int) (percent * (maxBounds.width - ijWidth));
   if (ijX < 10) ijX = 10;
   return new Point(ijX, maxBounds.y);
 }
Пример #5
0
  // This method returns a buffered image with the contents of an image
  // Source: http://www.exampledepot.com/egs/java.awt.image/Image2Buf.html
  public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
      return (BufferedImage) image;
    }

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see Determining If an Image Has Transparent Pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
      // Determine the type of transparency of the new buffered image
      int transparency = Transparency.OPAQUE;
      if (hasAlpha) {
        transparency = Transparency.BITMASK;
      }

      // Create the buffered image
      GraphicsDevice gs = ge.getDefaultScreenDevice();
      GraphicsConfiguration gc = gs.getDefaultConfiguration();
      bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
      // The system does not have a screen
    }

    if (bimage == null) {
      // Create a buffered image using the default color model
      int type = BufferedImage.TYPE_INT_RGB;
      if (hasAlpha) {
        type = BufferedImage.TYPE_INT_ARGB;
      }
      bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
  }
 public void testMethodCallInSuper() throws Exception {
   // todo[yole] make this test work in headless
   if (!GraphicsEnvironment.isHeadless()) {
     Class cls = loadAndPatchClass("TestMethodCallInSuper.form", "MethodCallInSuperTest");
     JDialog instance = (JDialog) cls.newInstance();
     assertEquals(1, instance.getContentPane().getComponentCount());
   }
 }
Пример #7
0
  static {
    //  Do not initialize keyboard settings
    //  if we are running headless.

    try {
      GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

      if (!ge.isHeadless()) {
        MENU_SHORTCUT_KEY_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

        MENU_SHORTCUT_SHIFT_KEY_MASK = MENU_SHORTCUT_KEY_MASK + InputEvent.SHIFT_MASK;
      } else {
        MENU_SHORTCUT_KEY_MASK = 0;
        MENU_SHORTCUT_SHIFT_KEY_MASK = 0;
      }
    } catch (Exception e) {
    }
  }
Пример #8
0
  public static DisplayDeviceArea[] getPresentationAndImageDeviceAreas() {
    DisplayDeviceArea[] displayDeviceAreas = null;
    GraphicsDevice[] gs = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
    if (gs.length == 1) {
      DisplayMode dm = gs[0].getDisplayMode();
      int width = dm.getWidth();
      int height = dm.getHeight();
      float presentationAspectRatio = 5f / 4; // usual screen for presentations
      float imageAspectRatio = 3f / 4 * 2; // pair of portrait monitors

      float presentationHorizontalProportion = 0.33f;

      int presentationWidth = (int) (width * presentationHorizontalProportion);

      int presentationHeight = (int) (presentationWidth / presentationAspectRatio);
      if (presentationHeight > height) {
        presentationHeight = height;
      }
      int presentationX = 0;
      int presentationY = height - presentationHeight;

      int imageWidth = width - presentationWidth;
      int imageHeight = (int) (imageWidth / imageAspectRatio);
      if (imageHeight > height) {
        imageHeight = height;
      }
      int imageX = presentationWidth;
      int imageY = height - imageHeight;

      displayDeviceAreas = new DisplayDeviceArea[2];
      displayDeviceAreas[0] =
          new DisplayDeviceArea(
              gs[0], presentationX, presentationY, presentationWidth, presentationHeight);
      displayDeviceAreas[1] = new DisplayDeviceArea(gs[0], imageX, imageY, imageWidth, imageHeight);
    } else if (gs.length == 2) {
      DisplayMode dm1 = gs[0].getDisplayMode();
      DisplayMode dm2 = gs[1].getDisplayMode();
      int width1 = dm1.getWidth();
      int width2 = dm2.getWidth();
      int height1 = dm1.getHeight();
      int height2 = dm2.getHeight();
      GraphicsDevice presentationDevice;
      GraphicsDevice imageDevice;
      if (width1 * height1 > width2 * height2) {
        presentationDevice = gs[1];
        imageDevice = gs[0];
      } else {
        presentationDevice = gs[0];
        imageDevice = gs[1];
      }
      displayDeviceAreas = new DisplayDeviceArea[2];
      displayDeviceAreas[0] = new DisplayDeviceArea(presentationDevice);
      displayDeviceAreas[1] = new DisplayDeviceArea(imageDevice);
    }
    return displayDeviceAreas;
  }
Пример #9
0
 protected JComboBox createFontChoice() {
   CommandChoice choice = new CommandChoice();
   String fonts[] =
       GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
   for (int i = 0; i < fonts.length; i++) {
     choice.addItem(
         new ChangeAttributeCommand(fonts[i], FigureAttributeConstant.FONT_NAME, fonts[i], this));
   }
   return choice;
 }
 @Test
 public void test_main_online() throws IOException {
   URL online = new URL("http://luckydonald.github.io/OfflineData.bin");
   if (GraphicsEnvironment.isHeadless()) {
     this.binFileReader = new BinFileReader(online);
   } else {
     this.binFileReader = new BinFileReaderGui(online);
   }
   do_read_from_bin();
 }
Пример #11
0
 public static BufferedImage tileImage(BufferedImage im, int width, int height) {
   GraphicsConfiguration gc =
       GraphicsEnvironment.getLocalGraphicsEnvironment()
           .getDefaultScreenDevice()
           .getDefaultConfiguration();
   int transparency = Transparency.OPAQUE; // Transparency.BITMASK;
   BufferedImage compatible = gc.createCompatibleImage(width, height, transparency);
   Graphics2D g = (Graphics2D) compatible.getGraphics();
   g.setPaint(new TexturePaint(im, new Rectangle(0, 0, im.getWidth(), im.getHeight())));
   g.fillRect(0, 0, width, height);
   return compatible;
 }
Пример #12
0
  JFrame openMonitorGUI(String title) {
    try {
      MonitorGUI gui = new MonitorGUI(this, title);

      JFrame frame = new JFrame(title);
      frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
      frame.addWindowListener(this);
      frame.getContentPane().add(gui);

      GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
      Rectangle r = ge.getMaximumWindowBounds();
      frame.setSize(400, 200);
      frame.setLocationRelativeTo(null);
      frame.pack();
      frame.setVisible(true);
      return frame;
    } catch (Exception e) {
      System.out.println("9\b" + getClass().getName() + "\n\t" + e);
      return null;
    }
  }
 @Test
 public void test_main_offline() throws IOException {
   File offline = new File("OfflineData.bin");
   if (offline.exists() && !offline.isDirectory()) {
     this.getLogger().info("OfflineData.bin file exists.");
     if (GraphicsEnvironment.isHeadless()) {
       this.binFileReader = new BinFileReader(offline);
     } else {
       this.binFileReader = new BinFileReaderGui(offline);
     }
     do_read_from_bin();
     System.out.println("");
   } else {
     getLogger().warning("No OfflineData.bin file!");
   }
 }
Пример #14
0
  public static void main(String[] args) {
    try {
      if (args.length != 2) {
        System.err.println("USAGE: java RenderMap map.txt image.png");
        System.exit(1);
      }
      Game game = new Game(args[0], 100, 0);
      if (game.Init() == 0) {
        System.err.println("Error while loading map " + args[0]);
        System.exit(1);
      }

      ArrayList<Color> colors = new ArrayList<Color>();
      colors.add(new Color(106, 74, 60));
      colors.add(new Color(74, 166, 60));
      colors.add(new Color(204, 51, 63));
      colors.add(new Color(235, 104, 65));
      colors.add(new Color(237, 201, 81));
      Color bgColor = new Color(188, 189, 172);
      Color textColor = Color.BLACK;
      Font planetFont = new Font("Sans Serif", Font.BOLD, 11);
      Font fleetFont = new Font("Sans serif", Font.PLAIN, 7);

      GraphicsConfiguration gc =
          GraphicsEnvironment.getLocalGraphicsEnvironment()
              .getDefaultScreenDevice()
              .getDefaultConfiguration();

      BufferedImage image = gc.createCompatibleImage(640, 480);

      Graphics2D _g = (Graphics2D) image.createGraphics();

      // Turn on AA/Speed
      _g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      _g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
      _g.setRenderingHint(
          RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

      game.Render(640, 480, 0.0, null, colors, bgColor, textColor, planetFont, fleetFont, _g);

      File file = new File(args[1]);
      ImageIO.write(image, "png", file);
    } catch (Exception e) {
      System.err.println(e);
    }
  }
 public void do_read_from_bin() throws IOException {
   Database db = new Database();
   DBEntry foo = binFileReader.readNextEntry(db);
   ArrayList<BinFileReadLogger> loggerz = binFileReader.getLoggerz();
   long endPos = -1;
   this.getLogger();
   for (BinFileReadLogger log : loggerz) {
     endPos = log.getEndPosition();
     // System.out.println(log);
   }
   if (!GraphicsEnvironment.isHeadless()) {
     ((BinFileReaderGui) binFileReader).updateHex();
   }
   System.out.println(foo.toSimpleString());
   if (endPos < 499796) {
     // Le fail.
     this.getLogger()
         .warning("Didn't read the complete file. Read " + endPos + " of 499796 bytes.");
   }
 }
Пример #16
0
 public GraphicsConfiguration getDeviceConfiguration() {
   GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
   GraphicsDevice gd = ge.getDefaultScreenDevice();
   return (gd.getDefaultConfiguration());
 }
Пример #17
0
 static {
   guiAvailable = !GraphicsEnvironment.isHeadless();
 }