Пример #1
0
 public void drawCluster(Cluster cluster) {
   int index = clusters.indexOf(cluster);
   if (index != -1) {
     cluster.y = maxY;
     cluster.x = minX + index * factor;
     if (cluster.size() > 1) g.setColor(Color.RED);
     else g.setColor(Color.BLACK);
     g.drawRect(cluster.x - 1, cluster.y - cluster.size(), 2, 1 + cluster.size());
   } else {
     Cluster left = cluster.getLeft();
     Cluster right = cluster.getRight();
     drawCluster(left);
     drawCluster(right);
     int yBar = minY + (int) ((maxY - minY) * (cluster.getSimilarity() / threshold));
     g.setColor(Color.DARK_GRAY);
     if (left.y > yBar) {
       g.drawLine(left.x, left.y - 1, left.x, yBar);
       writeMap(left, yBar);
     }
     if (right.y > yBar) {
       g.drawLine(right.x, right.y - 1, right.x, yBar);
       writeMap(right, yBar);
     }
     g.setColor(Color.BLACK);
     g.drawLine(left.x, yBar, right.x, yBar);
     cluster.x = (right.x + left.x) / 2;
     cluster.y = yBar;
   }
 }
Пример #2
0
 // initialize default image
 private BufferedImage getDefaultImage(int w, int h) {
   BufferedImage defaultImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
   Graphics2D graphics2D = defaultImage.createGraphics();
   graphics2D.setColor(new Color(200, 200, 200));
   graphics2D.fillRect(0, 0, w, h);
   graphics2D.setColor(new Color(130, 130, 130));
   graphics2D.drawRect(0, 0, w - 1, h - 1);
   return defaultImage;
 }
 private BufferedImage createCustomImage() {
   BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);
   Graphics2D g2d = image.createGraphics();
   int dx = image.getWidth() / SQUARES;
   int dy = image.getHeight() / SQUARES;
   for (int i = 0; i < SQUARES; ++i) {
     for (int j = 0; j < SQUARES; ++j) {
       g2d.setColor(new Color(rand.nextInt()));
       g2d.fillRect(i * dx, j * dy, dx, dy);
     }
   }
   g2d.setColor(Color.GREEN);
   g2d.drawRect(0, 0, image.getWidth() - 1, image.getHeight() - 1);
   g2d.dispose();
   return image;
 }
Пример #4
0
 // compare the two images in this object.
 public void compare() {
   // setup change display image
   imgc = imageToBufferedImage(img1);
   Graphics2D gc = imgc.createGraphics();
   gc.setColor(Color.RED);
   // convert to gray images.
   img1 = imageToBufferedImage(GrayFilter.createDisabledImage(img1));
   img2 = imageToBufferedImage(GrayFilter.createDisabledImage(img2));
   // how big are each section
   int blocksx = (int) (img1.getWidth() / comparex);
   int blocksy = (int) (img1.getHeight() / comparey);
   // set to a match by default, if a change is found then flag non-match
   this.match = true;
   // loop through whole image and compare individual blocks of images
   for (int y = 0; y < comparey; y++) {
     if (debugMode > 0) System.out.print("|");
     for (int x = 0; x < comparex; x++) {
       int b1 =
           getAverageBrightness(
               img1.getSubimage(x * blocksx, y * blocksy, blocksx - 1, blocksy - 1));
       int b2 =
           getAverageBrightness(
               img2.getSubimage(x * blocksx, y * blocksy, blocksx - 1, blocksy - 1));
       int diff = Math.abs(b1 - b2);
       if (diff
           > factorA) { // the difference in a certain region has passed the threshold value of
                        // factorA
         // draw an indicator on the change image to show where change was detected.
         gc.drawRect(x * blocksx, y * blocksy, blocksx - 1, blocksy - 1);
         this.match = false;
       }
       if (debugMode == 1) System.out.print((diff > factorA ? "X" : " "));
       if (debugMode == 2) System.out.print(diff + (x < comparex - 1 ? "," : ""));
     }
     if (debugMode > 0) System.out.println("|");
   }
 }
Пример #5
0
  public VolatileImage render() {

    Graphics2D g2d = renderImage.createGraphics();

    try {

      // Draw background layer:
      for (int i = 0; i < backgroundLayer.length; i++) {
        g2d.drawImage(
            backgroundLayer[i],
            -(int) (camera.position.x * Math.pow(0.5, backgroundLayer.length - i)),
            -(int) (camera.position.y * Math.pow(0.5, backgroundLayer.length - i))
                + backgroundLayer[i].getHeight(this)
                - backgroundLayer[i].getHeight(this) / (i + 1),
            this);
      }

    } catch (Exception e) {
    }

    // Draw Tiles: (new)
    try {
      g2d.drawImage(
          tileLayer,
          camera.center.x - camera.position.x,
          camera.center.y - camera.position.y,
          this);
    } catch (Exception e) {
    }

    // Draw all kinds of Sprites:

    try {
      int a = 0;

      while (sprite[a] != null) {
        // Play Animation for sprite:
        if (sprite[a].animation.plays == true) {
          sprite[a].getAnimation().nextFrame();
        }

        // -- Draw sprite:
        g2d.drawImage(
            sprite[a].img,
            /*X1*/ sprite[a].posx
                + ((sprite[a].flipH - 1) / (-2)) * sprite[a].size.width /*camera*/
                - camera.position.x
                + camera.center.x, /*Y1*/
            sprite[a].posy
                + ((sprite[a].flipV - 1) / (-2)) * sprite[a].size.height /*camera*/
                - camera.position.y
                + camera.center.y,
            /*X2*/ sprite[a].posx
                + sprite[a].size.width * sprite[a].flipH
                + ((sprite[a].flipH - 1) / (-2)) * sprite[a].size.width /*camera*/
                - camera.position.x
                + camera.center.x, /*Y2*/
            sprite[a].posy
                + sprite[a].size.height * sprite[a].flipV
                + ((sprite[a].flipV - 1) / (-2)) * sprite[a].size.height /*camera*/
                - camera.position.y
                + camera.center.y, // destination
            sprite[a].getAnimation().col * sprite[a].size.width,
            sprite[a].getAnimation().row * sprite[a].size.height, // source
            (sprite[a].getAnimation().col + 1) * sprite[a].size.width,
            (sprite[a].getAnimation().row + 1) * sprite[a].size.height,
            this);

        a++;
      }
    } catch (Exception e) {
      g2d.drawString("Error drawing a Sprite", 20, 20);
    }

    // Draw "GUI":
    g2d.drawImage(coinSpriteSheet, 16, 16, 32, 32, 0, 0, 16, 16, this);
    g2d.setColor(Color.BLACK);
    g2d.drawString("x " + collectedCoins, 32, 30);
    g2d.setColor(Color.WHITE);
    g2d.drawString("x " + collectedCoins, 32, 29);

    if (showSpritePos == true) {
      for (int i = 0; i < numberOfSprites; i++) {
        g2d.setColor(Color.red);
        g2d.drawRect(
            /*X1*/ sprite[i].posx /*camera*/ - camera.position.x + camera.center.x, /*Y1*/
            sprite[i].posy /*camera*/ - camera.position.y + camera.center.y,
            1,
            1);
        g2d.setColor(Color.black);
      }
    }

    if (showSpriteNum == true) {
      for (int i = 0; i < numberOfSprites; i++) {
        g2d.setColor(Color.black);
        g2d.drawString(
            "" + i, /*X1*/
            sprite[i].posx /*camera*/ - camera.position.x + camera.center.x, /*Y1*/
            sprite[i].posy /*camera*/ - camera.position.y + camera.center.y);
        g2d.setColor(Color.white);
        g2d.drawString(
            "" + i, /*X1*/
            sprite[i].posx /*camera*/ - camera.position.x + camera.center.x, /*Y1*/
            sprite[i].posy /*camera*/ - camera.position.y + camera.center.y - 1);
      }
    }

    if (showSpritePos == true) {
      for (int i = 0; i < numberOfTiles; i++) {
        g2d.setColor(Color.red);
        g2d.drawRect(
            /*X1*/ tile[i].posx
                + ((tile[i].flipH - 1) / (-2)) * tile[i].size.width /*camera*/
                - camera.position.x
                + camera.center.x, /*Y1*/
            tile[i].posy
                + ((tile[i].flipV - 1) / (-2)) * tile[i].size.height /*camera*/
                - camera.position.y
                + camera.center.y,
            1,
            1);
        g2d.setColor(Color.black);
      }
    }

    if (showCamera == true) {
      g2d.setColor(Color.red);
      g2d.drawLine(
          0,
          camera.prefHeight - camera.position.y + camera.center.y,
          loadedLevel.getWidth() * 16,
          camera.prefHeight - camera.position.y + camera.center.y);
      g2d.setColor(new Color(1, 0, 0, 0.33f));
      g2d.fillRect(
          0,
          camera.prefHeight - camera.position.y + camera.tolerance,
          loadedLevel.getWidth() * 16,
          camera.tolerance);
      g2d.setColor(new Color(0, 1, 0, 0.33f));
      g2d.fillRect(
          camera.center.x - camera.position.x + camera.center.x,
          camera.center.y - camera.position.y + camera.center.y,
          camera.bounds.width - 2 * camera.center.x,
          camera.bounds.height);
      g2d.setColor(Color.green);
      g2d.drawLine(
          camera.center.x - camera.position.x + camera.center.x,
          0,
          camera.center.x - camera.position.x + camera.center.x,
          999);
      g2d.drawLine(
          camera.bounds.width - camera.center.x - camera.position.x + camera.center.x,
          0,
          camera.bounds.width - camera.center.x - camera.position.x + camera.center.x,
          999);
    }

    return renderImage;
  }
Пример #6
0
 private void drawRectangle(int x, int y, int width, int height) {
   gRef.drawRect(x, y, width, height);
 }
    public void paintComponent(Graphics g) {
      super.paintComponent(g);

      Graphics2D g2 = (Graphics2D) g;

      AlphaComposite ac, ac2 = null;
      ac2 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
      for (int i = 0; i < imagenum_now.length; i++) {
        for (int j = 0; j < imagenum_now[i].length; j++) {
          // System.out.println("x: " + i + ", y: " + j);

          g2.setComposite(ac2);

          if (imagenum_now[i][j] != -1) {
            g2.drawImage(
                // images.get(imagenum_now[i][j])
                // getImage(images.get(imagenum_now[i][j])),
                imagenum_now2[i][j], i * size_x, j * size_y, size_x, size_y, this);
          } else {
            // No picture found, ignoring
            // System.out.println("imagenum_now["+i+"]["+j+"] = -1");
          }

          try {
            ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, fade[i][j]);
          } catch (IllegalArgumentException e) {
            ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
          }
          g2.setComposite(ac);
          if (imagenum_next[i][j] != -1) {
            g2.drawImage(
                // images.get(imagenum_next[i][j]),
                // getImage(images.get(imagenum_next[i][j])),
                imagenum_next2[i][j], i * size_x, j * size_y, size_x, size_y, this);
          } else {
            // No picture found, ignoring
            // System.out.println("imagenum_now["+i+"]["+j+"] = -1");
          }

          /*
          if(i == 0 && j == 0)
          	System.out.println("" + imagenum_now[i][j] +
          			" => " +
          			imagenum_next[i][j] + ", fade: "+fade[i][j]);;
          */

          // Red border if the image is new
          if (number_of_frames_redborder != -1
              && (images_nevershown.contains((Integer) imagenum_next[i][j])
                  || images_nevershown.contains((Integer) imagenum_now[i][j]))) {
            g2.setComposite(ac2);
            g2.setColor(color_redborder);
            int bordertime = redborder[i][j];
            if (bordertime > 0) {
              // Draw border
              g2.drawRect(i * size_x, j * size_y, size_x - 1, size_y - 1);

              if (bordertime > number_of_frames_redborder) {
                // No more border
                redborder[i][j] = -number_of_frames_redborder;
              }
            }
            redborder[i][j]++;
          }
        }
      }
    }
Пример #8
0
  private void drawInventory(Graphics g)
        //  PRE:  g must be initialized.
        //  POST: Draws the inventory of the store if mode == 0, else draws the players inventory.
        //        If an item is selected, its border is highlighted white.
      {
    Graphics2D g2; // Graphics 2D object to allow for different brush stroke widths.
    Color start; // The start color for the inventory window gradient.
    Color end; // The end color for the inventory window gradient.
    int x1; // Upper-left x coordinate.
    int y1; // Upper-left y coordinate.
    int x2; // Bottom-right x coordinate.
    int y2; // Bottom-right y coordinate.
    int width; // Width of the inventory box.
    int height; // Height of the inventory box.
    int itemHeight; // The height of an item box.
    int itemY; // The y coordinate of the item.
    Item currentItem; // Item to store the current item

    g2 = (Graphics2D) g;
    start = new Color(120, 120, 220);
    end = new Color(50, 50, 150);

    x1 = inventoryPos[0].getScaledX();
    y1 = inventoryPos[0].getScaledY();
    x2 = inventoryPos[1].getScaledX();
    y2 = inventoryPos[1].getScaledY();
    width = x2 - x1;
    height = y2 - y1;

    while (height % ITEMSPERPAGE != 0) // ensure that the item boxes do not fall short of the window
    {
      height = ++y2 - y1;
    }

    itemHeight = height / ITEMSPERPAGE;

    Drawing.drawGradient(g2, start, end, x1, y1, width, height / 2);
    Drawing.drawGradient(g2, end, start, x1, y1 + height / 2, width, height / 2);

    g2.setColor(Color.BLACK);
    g2.setStroke(new BasicStroke(3));
    g2.drawRect(--x1, --y1, ++width, height);
    g2.setColor(Color.WHITE);

    switch (store) // Chooses the current store
    {
      case 0:
        connectItems(getUserItems("Weapon", orderToSort));
        break;
      case 1:
        connectItems(getUserItems("ArmorSmith", orderToSort));
        break;
      case 2:
        connectItems(getUserItems("Accessory", orderToSort));
        break;
      case 3:
        connectItems(getUserItems("General", orderToSort));
        break;
    }

    if (mode) // Sets mode to true for user inventory and displays
    {
      connectItems(getUserItems("Player", orderToSort));
    }

    int count = 0;
    // count = currentPage * ITEMSPERPAGE ;

    try // Try loading 10 items per page
    {

      currentItem = null;
      count = currentPage * ITEMSPERPAGE;
      for (int i = 0; i < ITEMSPERPAGE; i++) // draw each item into the window
      {
        if (i > itemsArray.length - 1) // Check if i excceds the array size
        {
          return;
        }
        itemY = y1 + (itemHeight) * i;

        currentItem = itemsArray[count];

        if (itemSelected == i) // if the item was selected, highlight it
        {
          g2.drawRect(x1, itemY, width, itemHeight);
        }

        drawItem(g2, i, currentItem);

        count++;
      }

    } catch (Exception e) // Catch generic exception and print it out
    {
      System.err.println(e.toString());
      System.err.println("Oh no, not like this...");
    }
  }
Пример #9
0
 // also clip, transform, composite,
 // public boolean isOpaque(){return false;}//theOpaque!=null&&theOpaque;}
 // ---------------------------------------------------------
 private void doPaint(Graphics2D g, int s, Object o) {
   // process an operation from the buffer
   // System.out.println(s);
   Object o1 = null,
       o2 = null,
       o3 = null,
       o4 = null,
       o5 = null,
       o6 = null,
       o7 = null,
       o8 = null,
       o9 = null,
       o10 = null,
       o11 = null;
   if (o instanceof Object[]) {
     Object[] a = (Object[]) o;
     if (a.length > 0) o1 = a[0];
     if (a.length > 1) o2 = a[1];
     if (a.length > 2) o3 = a[2];
     if (a.length > 3) o4 = a[3];
     if (a.length > 4) o5 = a[4];
     if (a.length > 5) o6 = a[5];
     if (a.length > 6) o7 = a[6];
     if (a.length > 7) o8 = a[7];
     if (a.length > 8) o9 = a[8];
     if (a.length > 9) o10 = a[9];
     if (a.length > 10) o11 = a[10];
   }
   switch (s) {
     case clear:
       paintBackground(g, theBackground);
       break;
       // public void addRenderingHints(Map<?,?> hints)
       // {toBuffer("addRenderingHints",hints );}
     case addRenderingHints:
       g.addRenderingHints((Map<?, ?>) o);
       break;
     case clip1:
       g.clip((Shape) o);
       break;
     case draw1:
       g.draw((Shape) o);
       break;
     case draw3DRect:
       g.draw3DRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Boolean) o5);
       break;
     case drawGlyphVector:
       g.drawGlyphVector((GlyphVector) o1, (Float) o2, (Float) o3);
       break;
     case drawImage1:
       g.drawImage((BufferedImage) o1, (BufferedImageOp) o2, (Integer) o3, (Integer) o4);
       break;
     case drawImage2:
       g.drawImage((Image) o1, (AffineTransform) o2, (ImageObserver) o3);
       break;
     case drawRenderableImage:
       g.drawRenderableImage((RenderableImage) o1, (AffineTransform) o2);
       break;
     case drawRenderedImage:
       g.drawRenderedImage((RenderedImage) o1, (AffineTransform) o2);
       break;
     case drawString1:
       g.drawString((AttributedCharacterIterator) o1, (Float) o2, (Float) o3);
       break;
     case drawString2:
       g.drawString((AttributedCharacterIterator) o1, (Integer) o2, (Integer) o3);
       break;
     case drawString3:
       g.drawString((String) o1, (Float) o2, (Float) o3);
       break;
     case drawString4:
       g.drawString((String) o1, (Integer) o2, (Integer) o3);
       break;
     case fill:
       g.fill((Shape) o);
       break;
     case fill3DRect:
       g.fill3DRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Boolean) o5);
       break;
     case rotate1:
       g.rotate((Double) o);
       break;
     case rotate2:
       g.rotate((Double) o1, (Double) o2, (Double) o3);
       break;
     case scale1:
       g.scale((Double) o1, (Double) o2);
       break;
     case setBackground:
       g.setBackground(
           (Color) o); // paintBackground(g,(Color)o); /*super.setBackground((Color)o) ;*/
       break;
     case setComposite:
       g.setComposite((Composite) o);
       break;
     case setPaint:
       g.setPaint((Paint) o);
       break;
     case setRenderingHint:
       g.setRenderingHint((RenderingHints.Key) o1, o2);
       break;
     case setRenderingHints:
       g.setRenderingHints((Map<?, ?>) o);
       break;
     case setStroke:
       g.setStroke((Stroke) o);
       break;
     case setTransform:
       g.setTransform(makeTransform(o));
       break;
     case shear:
       g.shear((Double) o1, (Double) o2);
       break;
     case transform1:
       g.transform(makeTransform(o));
       break;
     case translate1:
       g.translate((Double) o1, (Double) o2);
       break;
     case translate2:
       g.translate((Integer) o1, (Integer) o2);
       break;
     case clearRect:
       g.clearRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
     case copyArea:
       g.copyArea(
           (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6);
       break;
     case drawArc:
       g.drawArc(
           (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6);
       break;
     case drawBytes:
       g.drawBytes((byte[]) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5);
       break;
     case drawChars:
       g.drawChars((char[]) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5);
       break;
     case drawImage4:
       g.drawImage((Image) o1, (Integer) o2, (Integer) o3, (Color) o4, (ImageObserver) o5);
       break;
     case drawImage5:
       g.drawImage((Image) o1, (Integer) o2, (Integer) o3, (ImageObserver) o4);
       break;
     case drawImage6:
       g.drawImage(
           (Image) o1,
           (Integer) o2,
           (Integer) o3,
           (Integer) o4,
           (Integer) o5,
           (Color) o6,
           (ImageObserver) o7);
       break;
     case drawImage7:
       g.drawImage(
           (Image) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (ImageObserver) o6);
       break;
     case drawImage8:
       g.drawImage(
           (Image) o1,
           (Integer) o2,
           (Integer) o3,
           (Integer) o4,
           (Integer) o5,
           (Integer) o6,
           (Integer) o7,
           (Integer) o8,
           (Integer) o9,
           (Color) o10,
           (ImageObserver) o11);
       break;
     case drawImage9:
       g.drawImage(
           (Image) o1,
           (Integer) o2,
           (Integer) o3,
           (Integer) o4,
           (Integer) o5,
           (Integer) o6,
           (Integer) o7,
           (Integer) o8,
           (Integer) o9,
           (ImageObserver) o10);
       break;
     case drawLine:
       g.drawLine((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
     case drawOval:
       g.drawOval((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
     case drawPolygon1:
       g.drawPolygon((int[]) o1, (int[]) o2, (Integer) o3);
       break;
     case drawPolygon2:
       g.drawPolygon((Polygon) o);
       break;
     case drawPolyline:
       g.drawPolyline((int[]) o1, (int[]) o2, (Integer) o3);
       break;
     case drawRect:
       g.drawRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
     case drawRoundRect:
       g.drawRoundRect(
           (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6);
       break;
     case fillArc:
       g.fillArc(
           (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6);
       break;
     case fillOval:
       g.fillOval((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
       // {toBuffer("fillPolygon",mkArg(xPoints,  yPoints, nPoints) );}
     case fillPolygon1:
       g.fillPolygon((int[]) o1, (int[]) o2, (Integer) o3);
       break;
     case fillPolygon2:
       g.fillPolygon((Polygon) o);
       break;
     case fillRect:
       g.fillRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
     case fillRoundRect:
       g.fillRoundRect(
           (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6);
       break;
     case setClip1:
       g.setClip((Shape) o);
       break;
     case setColor:
       g.setColor((Color) o);
       break;
     case setFont:
       g.setFont((Font) o);
       break;
     case setPaintMode:
       g.setPaintMode();
       break;
     case setXORMode:
       g.setXORMode((Color) o);
       break;
     case opaque:
       super.setOpaque((Boolean) o);
       break;
     case drawOutline: // g.drawString((String)o1, (Integer)o2, (Integer)o3) ;break;
       {
         FontRenderContext frc = g.getFontRenderContext();
         TextLayout tl = new TextLayout((String) o1, g.getFont(), frc);
         Shape s1 = tl.getOutline(null);
         AffineTransform af = g.getTransform();
         g.translate((Integer) o2, (Integer) o3);
         g.draw(s1);
         g.setTransform(af);
       }
       ;
       break;
     default:
       System.out.println("Unknown image operation " + s);
   }
 }