Ejemplo n.º 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);
   }
 }
Ejemplo n.º 2
0
  private Insets getButtonInsets(SynthContext context, Insets insets) {
    // The following calculations are derived from gtkbutton.c
    // (GTK+ version 2.8.20), gtk_button_size_allocate() method.
    int CHILD_SPACING = 1;
    int focusSize = getClassSpecificIntValue(context, "focus-line-width", 1);
    int focusPad = getClassSpecificIntValue(context, "focus-padding", 1);
    int xThickness = getXThickness();
    int yThickness = getYThickness();
    int w = focusSize + focusPad + xThickness + CHILD_SPACING;
    int h = focusSize + focusPad + yThickness + CHILD_SPACING;
    insets.left = insets.right = w;
    insets.top = insets.bottom = h;

    Component component = context.getComponent();
    if ((component instanceof JButton)
        && !(component.getParent() instanceof JToolBar)
        && ((JButton) component).isDefaultCapable()) {
      // Include the default border insets, but only for JButtons
      // that are default capable.  Note that
      // JButton.getDefaultCapable() returns true by default, but
      // GtkToolButtons are never default capable, so we skip this
      // step if the button is contained in a toolbar.
      Insets defaultInsets =
          getClassSpecificInsetsValue(context, "default-border", BUTTON_DEFAULT_BORDER_INSETS);
      insets.left += defaultInsets.left;
      insets.right += defaultInsets.right;
      insets.top += defaultInsets.top;
      insets.bottom += defaultInsets.bottom;
    }

    return insets;
  }
Ejemplo n.º 3
0
 void canvasFocusLost(FocusEvent e) {
   if (isXEmbedActive() && !e.isTemporary()) {
     xembedLog.fine("Forwarding FOCUS_LOST");
     int num = 0;
     if (AccessController.doPrivileged(new GetBooleanAction("sun.awt.xembed.testing"))) {
       Component opp = e.getOppositeComponent();
       try {
         num = Integer.parseInt(opp.getName());
       } catch (NumberFormatException nfe) {
       }
     }
     xembed.sendMessage(xembed.handle, XEMBED_FOCUS_OUT, num, 0, 0);
   }
 }
Ejemplo n.º 4
0
 /**
  * Load image from resource path (using getResource). Note that GIFs are loaded as _translucent_
  * indexed images. Images are cached: loading an image with the same name twice will get the
  * cached image the second time. If you want to remove an image from the cache, use purgeImage.
  * Throws JGError when there was an error.
  */
 @SuppressWarnings({"deprecation", "unchecked"})
 public JGImage loadImage(String imgfile) {
   Image img = (Image) loadedimages.get(imgfile);
   if (img == null) {
     URL imgurl = getClass().getResource(imgfile);
     if (imgurl == null) {
       try {
         File imgf = new File(imgfile);
         if (imgf.canRead()) {
           imgurl = imgf.toURL();
         } else {
           imgurl = new URL(imgfile);
           // throw new JGameError(
           //	"File "+imgfile+" not found.",true);
         }
       } catch (MalformedURLException e) {
         // e.printStackTrace();
         throw new JGameError("File not found or malformed path or URL '" + imgfile + "'.", true);
       }
     }
     img = output_comp.getToolkit().createImage(imgurl);
     loadedimages.put(imgfile, img);
   }
   try {
     ensureLoaded(img);
   } catch (Exception e) {
     // e.printStackTrace();
     throw new JGameError("Error loading image " + imgfile);
   }
   return new JREImage(img);
 }
Ejemplo n.º 5
0
 public JGImage crop(int x, int y, int width, int height) {
   @SuppressWarnings("unused")
   JGPoint size = getSize();
   int[] buffer = getPixels(x, y, width, height);
   return new JREImage(
       output_comp.createImage(new MemoryImageSource(width, height, buffer, 0, width)));
 }
Ejemplo n.º 6
0
 public JGImage flip(boolean horiz, boolean vert) {
   @SuppressWarnings("unused")
   Image flipped = null;
   JGPoint size = getSize();
   int[] buffer = getPixels();
   int[] flipbuf = new int[size.x * size.y];
   if (vert && !horiz) {
     for (int y = 0; y < size.y; y++) {
       for (int x = 0; x < size.x; x++) {
         flipbuf[(size.y - y - 1) * size.x + x] = buffer[(y * size.x) + x];
       }
     }
   } else if (horiz && !vert) {
     for (int y = 0; y < size.y; y++) {
       for (int x = 0; x < size.x; x++) {
         flipbuf[y * size.x + (size.x - x - 1)] = buffer[(y * size.x) + x];
       }
     }
   } else if (horiz && vert) {
     for (int y = 0; y < size.y; y++) {
       for (int x = 0; x < size.x; x++) {
         flipbuf[(size.y - y - 1) * size.x + (size.x - x - 1)] = buffer[(y * size.x) + x];
       }
     }
   }
   return new JREImage(
       output_comp.createImage(new MemoryImageSource(size.x, size.y, flipbuf, 0, size.x)));
 }
Ejemplo n.º 7
0
 /** for angle, only increments of 90 are allowed */
 public JGImage rotate(int angle) {
   @SuppressWarnings("unused")
   Image rot = null;
   JGPoint size = getSize();
   int[] buffer = getPixels();
   int[] rotate = new int[size.x * size.y];
   int angletype = (angle / 90) & 3;
   if (angletype == 0) return this;
   if (angletype == 1) {
     /* 1 2 3 4    9 5 1
      * 5 6 7 8 => a 6 2
      * 9 a b c    b 7 3
      *            c 8 4 */
     for (int y = 0; y < size.y; y++) {
       for (int x = 0; x < size.x; x++) {
         rotate[x * size.y + (size.y - 1 - y)] = buffer[(y * size.x) + x];
       }
     }
     return new JREImage(
         output_comp.createImage(new MemoryImageSource(size.y, size.x, rotate, 0, size.y)));
   }
   if (angletype == 3) {
     /* 1 2 3 4    4 8 c
      * 5 6 7 8 => 3 7 b
      * 9 a b c    2 6 a
      *            1 5 9 */
     for (int y = 0; y < size.y; y++) {
       for (int x = 0; x < size.x; x++) {
         rotate[(size.x - x - 1) * size.y + y] = buffer[(y * size.x) + x];
       }
     }
     return new JREImage(
         output_comp.createImage(new MemoryImageSource(size.y, size.x, rotate, 0, size.y)));
   }
   if (angletype == 2) {
     /* 1 2 3 4    c b a 9
      * 5 6 7 8 => 8 7 6 5
      * 9 a b c    4 3 2 1 */
     for (int y = 0; y < size.y; y++) {
       for (int x = 0; x < size.x; x++) {
         rotate[((size.y - y - 1) * size.x) + (size.x - x - 1)] = buffer[(y * size.x) + x];
       }
     }
   }
   return new JREImage(
       output_comp.createImage(new MemoryImageSource(size.x, size.y, rotate, 0, size.x)));
 }
Ejemplo n.º 8
0
 /**
  * Turn a (possibly) translucent or indexed image into a display-compatible bitmask image using
  * the given alpha threshold and render-to-background colour, or display-compatible translucent
  * image. The alpha values in the image are set to either 0 (below threshold) or 255 (above
  * threshold). The render-to-background colour bg_col is used to determine how the pixels
  * overlapping transparent pixels should be rendered. The fast algorithm just sets the colour
  * behind the transparent pixels in the image (for bitmask source images); the slow algorithm
  * actually renders the image to a background of bg_col (for translucent sources).
  *
  * @param thresh alpha threshold between 0 and 255
  * @param fast use fast algorithm (only set bg_col behind transp. pixels)
  * @param bitmask true=use bitmask, false=use translucent
  */
 public JGImage toDisplayCompatible(int thresh, JGColor bg_col, boolean fast, boolean bitmask) {
   Color bgcol = new Color(bg_col.r, bg_col.g, bg_col.b);
   int bgcol_rgb = (bgcol.getRed() << 16) | (bgcol.getGreen() << 8) | bgcol.getBlue();
   JGPoint size = getSize();
   int[] buffer = getPixels();
   // render image to bg depending on bgcol
   BufferedImage img_bg;
   if (bitmask) {
     img_bg = createCompatibleImage(size.x, size.y, Transparency.BITMASK);
   } else {
     img_bg = createCompatibleImage(size.x, size.y, Transparency.TRANSLUCENT);
   }
   int[] bg_buf;
   if (!fast) {
     Graphics g = img_bg.getGraphics();
     g.setColor(bgcol);
     // the docs say I could use bgcol in the drawImage as an
     // equivalent to the following two lines, but this
     // doesn't handle translucency properly and is _slower_
     g.fillRect(0, 0, size.x, size.y);
     g.drawImage(img, 0, 0, null);
     bg_buf = new JREImage(img_bg).getPixels();
   } else {
     bg_buf = buffer;
   }
   // g.dispose();
   // ColorModel rgb_bitmask = ColorModel.getRGBdefault();
   // rgb_bitmask = new PackedColorModel(
   //		rgb_bitmask.getColorSpace(),25,0xff0000,0x00ff00,0x0000ff,
   //		0x1000000, false, Transparency.BITMASK, DataBuffer.TYPE_INT);
   //		ColorSpace space, int bits, int rmask, int gmask, int bmask, int amask, boolean
   // isAlphaPremultiplied, int trans, int transferType)
   int[] thrsbuf = new int[size.x * size.y];
   for (int y = 0; y < size.y; y++) {
     for (int x = 0; x < size.x; x++) {
       if (((buffer[y * size.x + x] >> 24) & 0xff) >= thresh) {
         thrsbuf[y * size.x + x] = bg_buf[y * size.x + x] | (0xff << 24);
       } else {
         // explicitly set the colour of the transparent pixel.
         // This makes a difference when scaling!
         // thrsbuf[y*size.x+x]=bg_buf[y*size.x+x]&~(0xff<<24);
         thrsbuf[y * size.x + x] = bgcol_rgb;
       }
     }
   }
   return new JREImage(
       output_comp.createImage(
           new MemoryImageSource(
               size.x,
               size.y,
               // rgb_bitmask,
               img_bg.getColorModel(), // display compatible bitmask
               bitmask ? thrsbuf : bg_buf,
               0,
               size.x)));
 }
Ejemplo n.º 9
0
 /**
  * Returns the value to initialize the opacity property of the Component to. A Style should NOT
  * assume the opacity will remain this value, the developer may reset it or override it.
  *
  * @param context SynthContext identifying requestor
  * @return opaque Whether or not the JComponent is opaque.
  */
 @Override
 public boolean isOpaque(SynthContext context) {
   Region region = context.getRegion();
   if (region == Region.COMBO_BOX
       || region == Region.DESKTOP_PANE
       || region == Region.DESKTOP_ICON
       || region == Region.EDITOR_PANE
       || region == Region.FORMATTED_TEXT_FIELD
       || region == Region.INTERNAL_FRAME
       || region == Region.LIST
       || region == Region.MENU_BAR
       || region == Region.PANEL
       || region == Region.PASSWORD_FIELD
       || region == Region.POPUP_MENU
       || region == Region.PROGRESS_BAR
       || region == Region.ROOT_PANE
       || region == Region.SCROLL_PANE
       || region == Region.SPINNER
       || region == Region.SPLIT_PANE_DIVIDER
       || region == Region.TABLE
       || region == Region.TEXT_AREA
       || region == Region.TEXT_FIELD
       || region == Region.TEXT_PANE
       || region == Region.TOOL_BAR_DRAG_WINDOW
       || region == Region.TOOL_TIP
       || region == Region.TREE
       || region == Region.VIEWPORT) {
     return true;
   }
   Component c = context.getComponent();
   String name = c.getName();
   if (name == "ComboBox.renderer" || name == "ComboBox.listRenderer") {
     return true;
   }
   return false;
 }
Ejemplo n.º 10
0
 /** Behaves like loadImage(String). Returns null if there was an error. */
 @SuppressWarnings("unchecked")
 public static JGImage loadImage(URL imgurl) {
   Image img = (Image) loadedimages.get(imgurl);
   if (img == null) {
     img = output_comp.getToolkit().createImage(imgurl);
     loadedimages.put(imgurl, img);
   }
   try {
     ensureLoaded(img);
   } catch (Exception e) {
     System.err.println("Error loading image " + imgurl);
     return null;
   }
   return new JREImage(img);
 }
Ejemplo n.º 11
0
  public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    // Component printMe = getPrintComponent();

    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.black); // set default foreground color to black

    // for faster printing, turn off double buffering
    // RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);

    Dimension d = printComponent.getSize(); // get size of document
    double panelWidth = d.width; // width in pixels
    double panelHeight = d.height; // height in pixels
    double pageHeight = pf.getImageableHeight(); // height of printer page
    double pageWidth = pf.getImageableWidth(); // width of printer page
    double scale = pageWidth / panelWidth;
    int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight);

    // make sure we don't print empty pages
    if (pageIndex >= totalNumPages) {
      return Printable.NO_SUCH_PAGE;
    }

    // shift Graphic to line up with beginning of print-imageable region
    g2.translate(pf.getImageableX(), pf.getImageableY());

    // shift Graphic to line up with beginning of next page to print
    g2.translate(0f, -pageIndex * pageHeight);

    // scale the page so the width fits...
    g2.scale(scale, scale);

    // PRINT IT!
    printComponent.paint(g2);

    return Printable.PAGE_EXISTS;
  }
Ejemplo n.º 12
0
 /**
  * Returns the size style of a specified component.
  *
  * @return REGULAR, SMALL or MINI.
  */
 private int getSizeStyle(Component c) {
   // Aqua components have a different style depending on the
   // font size used.
   // 13 Point = Regular
   // 11 Point = Small
   //  9 Point = Mini
   if (c == null) {
     return REGULAR;
   }
   Font font = c.getFont();
   if (font == null) {
     return REGULAR;
   }
   int fontSize = font.getSize();
   return (fontSize >= 13) ? REGULAR : ((fontSize > 9) ? SMALL : MINI);
 }
Ejemplo n.º 13
0
 Window getTopLevel(Component comp) {
   while (comp != null && !(comp instanceof Window)) {
     comp = comp.getParent();
   }
   return (Window) comp;
 }
Ejemplo n.º 14
0
  /**
   * Standard constructor.
   *
   * @param type Type that you are going to be creating and editor for.
   * @param readOnly Set to true to create a read-only customizer.
   */
  public DynamicCustomizer(Class type, boolean readOnly) {
    super(new GridBagLayout());
    _readOnly = readOnly;
    _type = type;

    LabelFieldGBC gbc = new LabelFieldGBC();
    try {
      BeanInfo info = Introspector.getBeanInfo(type);
      // Set up pretty display stuff.
      setBorder(BorderFactory.createTitledBorder(info.getBeanDescriptor().getDisplayName()));
      setToolTipText(info.getBeanDescriptor().getShortDescription());

      // Get the properties and sort them.
      PropertyDescriptor[] props = info.getPropertyDescriptors();
      Arrays.sort(props, new PropertyComparator());
      for (int i = 0; i < props.length; i++) {
        // Ignore the "class" property, if it is provided.
        if (props[i].getName().equals("class")) continue;
        // Create a label for the field.
        JLabel label = new JLabel(props[i].getDisplayName() + ":");

        // Lookup the editor.
        PropertyEditor editor = getEditorForProperty(props[i]);

        // Add a listener to the editor so we know when to update
        // the bean's fields.
        editor.addPropertyChangeListener(_eListener);

        // XXX What we need to do right here is provide a component
        // that makes use of the "paintable" capability of the editor.
        Component comp = editor.getCustomEditor();
        if (comp == null) {
          comp = new JLabel("<No editor available.>");
          ((JLabel) comp).setBorder(BorderFactory.createEtchedBorder());
        }

        // See if it is a read-only property. If so, then just
        // display it.
        if (_readOnly || props[i].getWriteMethod() == null) {
          comp.setEnabled(false);
        }

        // Setup the accellerator key.
        label.setLabelFor(comp);
        label.setDisplayedMnemonic(label.getText().charAt(0));

        // Set the tool tip text, if any.
        String tip = props[i].getShortDescription();
        if (tip != null) {
          label.setToolTipText(tip);
          if (comp instanceof JComponent) {
            ((JComponent) comp).setToolTipText(tip);
          }
        }

        // Add the label and fields.
        add(label, gbc.forLabel());
        add(comp, gbc.forField());

        // Set the mappings between editor and property, etc. for
        // quick lookup later.
        _prop2Editor.put(props[i], editor);
        _editor2Prop.put(editor, props[i]);
      }
      // Filler...
      add(new JLabel(), gbc.forLastLabel());

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }