metalutils.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 526 行 · 第 1/2 页

JAVA
526
字号
   * @param y the Y coordinate of the upper left corner of the rectangle   * @param w the width of the rectangle   * @param h the height of the rectangle   * @param g1 the relative width of the c1->c2 gradients   * @param g2 the relative width of the c2 solid area   * @param c1 the color 1   * @param c2 the color 2   * @param c3 the color 3   * @param dir the direction of the gradient, either   *        {@link SwingConstants#HORIZONTAL} or {@link SwingConstants#VERTICAL}   * @param mask the mask that should be used when painting the gradient as   *        described above   */  static void paintGradient(Graphics g, int x, int y, int w, int h, float g1,                            float g2, Color c1, Color c2, Color c3, int dir,                            int[][] mask)  {    if (dir == SwingConstants.HORIZONTAL)      paintHorizontalGradient(g, x, y, w, h, g1, g2, c1, c2, c3, mask);    else      paintVerticalGradient(g, x, y, w, h, g1, g2, c1, c2, c3, mask);  }  /**   * Paints a horizontal gradient. See {@link #paintGradient(Graphics, int,   * int, int, int, float, float, Color, Color, Color, int, int[][])}   * for details.   *   * @param x the X coordinate of the upper left corner of the rectangle   * @param y the Y coordinate of the upper left corner of the rectangle   * @param w the width of the rectangle   * @param h the height of the rectangle   * @param g1 the relative width of the c1->c2 gradients   * @param g2 the relative width of the c2 solid area   * @param c1 the color 1   * @param c2 the color 2   * @param c3 the color 3   * @param mask the mask that should be used when painting the gradient as   *        described above   */  static void paintHorizontalGradient(Graphics g, int x, int y, int w, int h,                                      float g1, float g2, Color c1, Color c2,                                      Color c3, int[][] mask)  {    // Calculate the coordinates.    int y0 = y;    int y1 = y + h;    // The size of the first gradient area (c1->2).    int w1 = (int) (w * g1);    // The size of the solid c2 area.    int w2 = (int) (w * g2);    int x0 = x;    int x1 = x0 + w1;    int x2 = x1 + w2;    int x3 = x2 + w1;    int x4 = x + w;    // Paint first gradient area (c1->c2).    int xc; // The current y coordinate.    for (xc = x0; xc < x1; xc++)      {        if (xc > x + w)          break;        // Perform color interpolation;        double factor = (xc - x0) / (double) w1;        int rInt = (int) ((c2.getRed() - c1.getRed()) * factor + c1.getRed());        int gInt = (int) ((c2.getGreen() - c1.getGreen()) * factor            + c1.getGreen());        int bInt = (int) ((c2.getBlue() - c1.getBlue()) * factor            + c1.getBlue());        Color interpolated = new Color(rInt, gInt, bInt);        g.setColor(interpolated);        if (mask != null)          {            y0 = mask[xc - x0][0] + y;            y1 = mask[xc - x0][1] + y;          }        g.drawLine(xc, y0, xc, y1);      }    // Paint solid c2 area.    g.setColor(c2);    if (mask == null)      {        g.fillRect(x1, y, x2 - x1, h);      }    else      {        for (xc = x1; xc < x2; xc++)          {            y0 = mask[xc - x0][0] + y;            y1 = mask[xc - x0][1] + y;            g.drawLine(xc, y0, xc, y1);          }      }    // Paint second gradient area (c2->c1).    for (xc = x2; xc < x3; xc++)      {        if (xc > x + w)          break;        // Perform color interpolation;        double factor = (xc - x2) / (double) w1;        int rInt = (int) ((c1.getRed() - c2.getRed()) * factor + c2.getRed());        int gInt = (int) ((c1.getGreen() - c2.getGreen()) * factor            + c2.getGreen());        int bInt = (int) ((c1.getBlue() - c2.getBlue()) * factor            + c2.getBlue());        Color interpolated = new Color(rInt, gInt, bInt);        g.setColor(interpolated);        if (mask != null)          {            y0 = mask[xc - x0][0] + y;            y1 = mask[xc - x0][1] + y;          }        g.drawLine(xc, y0, xc, y1);      }    // Paint third gradient area (c1->c3).    for (xc = x3; xc < x4; xc++)      {        if (xc > x + w)          break;        // Perform color interpolation;        double factor = (xc - x3) / (double) (x4 - x3);        int rInt = (int) ((c3.getRed() - c1.getRed()) * factor + c1.getRed());        int gInt = (int) ((c3.getGreen() - c1.getGreen()) * factor            + c1.getGreen());        int bInt = (int) ((c3.getBlue() - c1.getBlue()) * factor            + c1.getBlue());        Color interpolated = new Color(rInt, gInt, bInt);        g.setColor(interpolated);        if (mask != null)          {            y0 = mask[xc - x0][0] + y;            y1 = mask[xc - x0][1] + y;          }        g.drawLine(xc, y0, xc, y1);      }  }  /**   * Paints a vertical gradient. See {@link #paintGradient(Graphics, int, int,   * int, int, float, float, Color, Color, Color, int, int[][])} for details.   *   * @param x the X coordinate of the upper left corner of the rectangle   * @param y the Y coordinate of the upper left corner of the rectangle   * @param w the width of the rectangle   * @param h the height of the rectangle   * @param g1 the relative width of the c1->c2 gradients   * @param g2 the relative width of the c2 solid area   * @param c1 the color 1   * @param c2 the color 2   * @param c3 the color 3   * @param mask the mask that should be used when painting the gradient as   *        described above   */  static void paintVerticalGradient(Graphics g, int x, int y, int w, int h,                                    double g1, double g2, Color c1, Color c2,                                    Color c3, int[][] mask)  {    // Calculate the coordinates.    int x0 = x;    int x1 = x + w;    // The size of the first gradient area (c1->2).    int w1 = (int) (h * g1);    // The size of the solid c2 area.    int w2 = (int) (h * g2);    int y0 = y;    int y1 = y0 + w1;    int y2 = y1 + w2;    int y3 = y2 + w1;    int y4 = y + h;    // Paint first gradient area (c1->c2).    int yc; // The current y coordinate.    for (yc = y0; yc < y1; yc++)      {        if (yc > y + h)          break;        // Perform color interpolation;        double factor = (yc - y0) / (double) w1;        int rInt = (int) ((c2.getRed() - c1.getRed()) * factor + c1.getRed());        int gInt = (int) ((c2.getGreen() - c1.getGreen()) * factor            + c1.getGreen());        int bInt = (int) ((c2.getBlue() - c1.getBlue()) * factor            + c1.getBlue());        Color interpolated = new Color(rInt, gInt, bInt);        g.setColor(interpolated);        if (mask != null)          {            x0 = mask[yc - y0][0] + x;            x1 = mask[yc - y0][1] + x;          }        g.drawLine(x0, yc, x1, yc);      }    // Paint solid c2 area.    g.setColor(c2);    if (mask == null)      {        g.fillRect(x, y1, w, y2 - y1);      }    else      {        for (yc = y1; yc < y2; yc++)          {            x0 = mask[yc - y0][0] + x;            x1 = mask[yc - y0][1] + x;            g.drawLine(x0, yc, x1, yc);          }      }    // Paint second gradient area (c2->c1).    for (yc = y2; yc < y3; yc++)      {        if (yc > y + h)          break;        // Perform color interpolation;        double factor = (yc - y2) / (double) w1;        int rInt = (int) ((c1.getRed() - c2.getRed()) * factor + c2.getRed());        int gInt = (int) ((c1.getGreen() - c2.getGreen()) * factor            + c2.getGreen());        int bInt = (int) ((c1.getBlue() - c2.getBlue()) * factor            + c2.getBlue());        Color interpolated = new Color(rInt, gInt, bInt);        g.setColor(interpolated);        if (mask != null)          {            x0 = mask[yc - y0][0] + x;            x1 = mask[yc - y0][1] + x;          }        g.drawLine(x0, yc, x1, yc);      }    // Paint third gradient area (c1->c3).    for (yc = y3; yc < y4; yc++)      {        if (yc > y + h)          break;        // Perform color interpolation;        double factor = (yc - y3) / (double) (y4 - y3);        int rInt = (int) ((c3.getRed() - c1.getRed()) * factor + c1.getRed());        int gInt = (int) ((c3.getGreen() - c1.getGreen()) * factor            + c1.getGreen());        int bInt = (int) ((c3.getBlue() - c1.getBlue()) * factor            + c1.getBlue());        Color interpolated = new Color(rInt, gInt, bInt);        g.setColor(interpolated);        if (mask != null)          {            x0 = mask[yc - y0][0] + x;            x1 = mask[yc - y0][1] + x;          }        g.drawLine(x0, yc, x1, yc);      }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?