Exemplo n.º 1
0
  // long lastFrame = -1;
  public void repaint(RectListManager devRLM) {
    if (devRLM == null) return;

    // long t0 = System.currentTimeMillis();
    // if (lastFrame != -1) {
    //     System.out.println("InterFrame time: " + (t0-lastFrame));
    // }
    // lastFrame = t0;

    CachableRed cr;
    WritableRaster syncRaster;
    WritableRaster copyRaster;

    updateWorkingBuffers();
    if ((rootCR == null) || (workingBaseRaster == null)) {
      // System.err.println("RootCR: " + rootCR);
      // System.err.println("wrkBaseRaster: " + workingBaseRaster);
      return;
    }
    cr = rootCR;
    syncRaster = workingBaseRaster;
    copyRaster = workingRaster;

    Rectangle srcR = rootCR.getBounds();
    // System.out.println("RootCR: " + srcR);
    Rectangle dstR = workingRaster.getBounds();
    if ((dstR.x < srcR.x)
        || (dstR.y < srcR.y)
        || (dstR.x + dstR.width > srcR.x + srcR.width)
        || (dstR.y + dstR.height > srcR.y + srcR.height))
      cr = new PadRed(cr, dstR, PadMode.ZERO_PAD, null);

    boolean repaintAll = false;

    Rectangle dr = copyRaster.getBounds();

    // Ensure only one thread works on baseRaster at a time...
    synchronized (syncRaster) {
      // System.out.println("Dynamic:");
      if (repaintAll) {
        // System.out.println("Repainting All");
        cr.copyData(copyRaster);
      } else {
        java.awt.Graphics2D g2d = null;
        if (false) {
          BufferedImage tmpBI =
              new BufferedImage(
                  workingOffScreen.getColorModel(),
                  copyRaster.createWritableTranslatedChild(0, 0),
                  workingOffScreen.isAlphaPremultiplied(),
                  null);
          g2d = GraphicsUtil.createGraphics(tmpBI);
          g2d.translate(-copyRaster.getMinX(), -copyRaster.getMinY());
        }

        if ((isDoubleBuffered) && (currentRaster != null) && (damagedAreas != null)) {
          damagedAreas.subtract(devRLM, COPY_OVERHEAD, COPY_LINE_OVERHEAD);
          damagedAreas.mergeRects(COPY_OVERHEAD, COPY_LINE_OVERHEAD);

          // color-objects are immutable, so we can reuse this handful of instances
          Color fillColor = new Color(0, 0, 255, 50);
          Color borderColor = new Color(0, 0, 0, 50);

          Iterator iter = damagedAreas.iterator();
          while (iter.hasNext()) {
            Rectangle r = (Rectangle) iter.next();
            if (!dr.intersects(r)) continue;
            r = dr.intersection(r);
            // System.err.println("Copy: " + r);
            Raster src =
                currentRaster.createWritableChild(r.x, r.y, r.width, r.height, r.x, r.y, null);
            GraphicsUtil.copyData(src, copyRaster);
            if (g2d != null) {
              g2d.setPaint(fillColor);
              g2d.fill(r);
              g2d.setPaint(borderColor);
              g2d.draw(r);
            }
          }
        }

        Color fillColor = new Color(255, 0, 0, 50);
        Color borderColor = new Color(0, 0, 0, 50);

        Iterator iter = devRLM.iterator();
        while (iter.hasNext()) {
          Rectangle r = (Rectangle) iter.next();
          if (!dr.intersects(r)) continue;
          r = dr.intersection(r);

          // System.err.println("Render: " + r);
          WritableRaster dst =
              copyRaster.createWritableChild(r.x, r.y, r.width, r.height, r.x, r.y, null);
          cr.copyData(dst);
          if (g2d != null) {
            g2d.setPaint(fillColor);
            g2d.fill(r);
            g2d.setPaint(borderColor);
            g2d.draw(r);
          }
        }
      }
    }

    if (HaltingThread.hasBeenHalted()) return;

    // System.out.println("Dmg: "   + damagedAreas);
    // System.out.println("Areas: " + devRects);

    // Swap the buffers if the rendering completed cleanly.
    BufferedImage tmpBI = workingOffScreen;

    workingBaseRaster = currentBaseRaster;
    workingRaster = currentRaster;
    workingOffScreen = currentOffScreen;

    currentRaster = copyRaster;
    currentBaseRaster = syncRaster;
    currentOffScreen = tmpBI;

    damagedAreas = devRLM;
  }