Пример #1
1
  /*
   * sets the colormap to Rainbow
   */
  private void setColorModelRainbow() {
    // Copy in the default colors
    for (int i = 0; i < tableSize; i++) {
      cm_red[i] = puke_red[i];
      cm_green[i] = puke_green[i];
      cm_blue[i] = puke_blue[i];
    }

    // Set rainbow for attribute colors
    float maxHue = 0.8f;
    float hue = 0;
    float delta = maxHue / attrSize;
    int rainbowColor;
    for (int i = startColor; i <= endColor; ++i, hue += delta) {
      rainbowColor = Color.HSBtoRGB(hue, 1.0f, 1.0f);
      cm_red[i] = (byte) ((rainbowColor >> 16) & 0xff);
      cm_green[i] = (byte) ((rainbowColor >> 8) & 0xff);
      cm_blue[i] = (byte) ((rainbowColor >> 0) & 0xff);
    }

    colorModel = new IndexColorModel(8, tableSize, cm_red, cm_green, cm_blue);
  }
Пример #2
0
  public void draw(Graphics g, int max) {
    if (enterable) {
      float hue = ((float) cost / (float) max);
      if (solution || (moveable != null && moveable.solution)) {
        g.setColor(new Color(Color.HSBtoRGB(1.0f - hue, 1f, 1f)));
      } else {
        g.setColor(new Color(Color.HSBtoRGB(hue, 0.5f, 0.8f)));
      }

      g.fillRect(x * size, y * size, size, size);
    } else {
      g.setColor(Color.BLACK);
      g.fillRect(x * size, y * size, size, size);
    }

    g.setColor(Color.BLACK);
    g.drawRect(x * size, y * size, size, size);

    if (enterable) {
      g.setColor(Color.RED);
      g.drawString(cost + "", (x * size) + (size / 2), (y * size) + (size / 2));
    }

    if (moveable != null) {
      moveable.draw(g, cost);
    }
  }
Пример #3
0
  /**
   * Sets the selected color of this panel.
   *
   * <p>If this panel is in RED, GREEN, or BLUE mode, then this method converts these values to RGB
   * coordinates and calls <code>setRGB</code>.
   *
   * <p>This method may regenerate the graphic if necessary.
   *
   * @param h the hue value of the selected color.
   * @param s the saturation value of the selected color.
   * @param b the brightness value of the selected color.
   */
  public void setHSB(float h, float s, float b) {
    if (Float.isInfinite(h) || Float.isNaN(h))
      throw new IllegalArgumentException("The hue value (" + h + ") is not a valid number.");
    // hue is cyclic, so it can be any value:
    while (h < 0) h++;
    while (h > 1) h--;

    if (s < 0 || s > 1)
      throw new IllegalArgumentException("The saturation value (" + s + ") must be between [0,1]");
    if (b < 0 || b > 1)
      throw new IllegalArgumentException("The brightness value (" + b + ") must be between [0,1]");

    if (hue != h || sat != s || bri != b) {
      if (mode == ColorPicker.HUE || mode == ColorPicker.BRI || mode == ColorPicker.SAT) {
        float lastHue = hue;
        float lastBri = bri;
        float lastSat = sat;
        hue = h;
        sat = s;
        bri = b;
        if (mode == ColorPicker.HUE) {
          if (lastHue != hue) {
            regenerateImage();
          }
        } else if (mode == ColorPicker.SAT) {
          if (lastSat != sat) {
            regenerateImage();
          }
        } else if (mode == ColorPicker.BRI) {
          if (lastBri != bri) {
            regenerateImage();
          }
        }
      } else {

        Color c = new Color(Color.HSBtoRGB(h, s, b));
        setRGB(c.getRed(), c.getGreen(), c.getBlue());
        return;
      }

      Color c = new Color(Color.HSBtoRGB(hue, sat, bri));
      red = c.getRed();
      green = c.getGreen();
      blue = c.getBlue();

      regeneratePoint();
      repaint();
      fireChangeListeners();
    }
  }
 static Color[] getRainbowColors(int count) {
   Color c[] = new Color[count];
   for (int i = 0; i < count; i++) {
     c[i] = new Color(Color.HSBtoRGB((float) (2. * ((i * 11) % count) / count), 0.5f, 0.95f));
   }
   return c;
 }
Пример #5
0
  public void drawBackground() {
    int i, x, y, width = getWidth(), height = getHeight();

    imBackBuff = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D gib = (Graphics2D) imBackBuff.getGraphics();
    gib.setBackground(Color.WHITE);
    gib.clearRect(0, 0, width, height);
    // draw gravity field
    float randShift = rand.nextFloat();
    float randMult = 1.0f; // 0.6f*rand.nextFloat()+0.2f;
    for (y = 0; y < height; y += 4) {
      for (x = 0; x < width; x += 4) {
        float fieldStrength = 0;

        for (i = 0; i < planets.length; i++) {
          Planet p = planets[i];

          double rSquared = Math.pow(p.x - x, 2) + Math.pow(p.y - y, 2);

          fieldStrength += p.mass / rSquared;
          int rgb = Color.HSBtoRGB(randMult * fieldStrength + randShift, 1.0f, 1.0f);
          gib.setColor(new Color(rgb));
          gib.drawRect(x, y, 2, 2);
        }
      }
    }
  }
Пример #6
0
  protected void renderRecordOverlay(int width, int height, float partialTicks) {
    if (recordPlayingUpFor > 0) {
      mc.mcProfiler.startSection("overlayMessage");
      float hue = (float) recordPlayingUpFor - partialTicks;
      int opacity = (int) (hue * 256.0F / 20.0F);
      if (opacity > 255) opacity = 255;

      if (opacity > 0) {
        GlStateManager.pushMatrix();
        GlStateManager.translate((float) (width / 2), (float) (height - 68), 0.0F);
        GlStateManager.enableBlend();
        GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0);
        int color = (recordIsPlaying ? Color.HSBtoRGB(hue / 50.0F, 0.7F, 0.6F) & WHITE : WHITE);
        fontrenderer.drawString(
            recordPlaying,
            -fontrenderer.getStringWidth(recordPlaying) / 2,
            -4,
            color | (opacity << 24));
        GlStateManager.disableBlend();
        GlStateManager.popMatrix();
      }

      mc.mcProfiler.endSection();
    }
  }
  private Color getThreadBlockColor(BumpThread bt) {
    if (bt == null) return Color.BLACK;

    synchronized (thread_colors) {
      Color c = thread_colors.get(bt);
      if (c == null) {
        double v;
        int ct = thread_colors.size();
        if (ct == 0) v = 0;
        else if (ct == 1) v = 1;
        else {
          v = 0.5;
          int p0 = ct - 1;
          int p1 = 1;
          for (int p = p0; p > 1; p /= 2) {
            v /= 2.0;
            p0 -= p1;
            p1 *= 2;
          }
          if ((p0 & 1) == 0) p0 = 2 * p1 - p0 + 1;
          v = v * p0;
        }
        float h = (float) (v * 0.8);
        float s = 0.7f;
        float b = 1.0f;
        int rgb = Color.HSBtoRGB(h, s, b);
        rgb |= 0xc0000000;
        c = new Color(rgb, true);
        thread_colors.put(bt, c);
      }
      return c;
    }
  }
Пример #8
0
 private Color changeColor() {
   // Commands to change the colors of the two Yin Yangs randomly
   // to some other color
   return new Color(
       Color.HSBtoRGB(
           randomNumber.nextFloat(), randomNumber.nextFloat(), randomNumber.nextFloat()));
 }
Пример #9
0
    public void composeRGB(int[] src, int[] dst, float alpha) {
      int w = src.length;

      for (int i = 0; i < w; i += 4) {
        int sr = src[i];
        int dir = dst[i];
        int sg = src[i + 1];
        int dig = dst[i + 1];
        int sb = src[i + 2];
        int dib = dst[i + 2];
        int sa = src[i + 3];
        int dia = dst[i + 3];
        int dor, dog, dob;

        Color.RGBtoHSB(sr, sg, sb, sHSB);
        Color.RGBtoHSB(dir, dig, dib, dHSB);

        dHSB[0] = sHSB[0];

        int doRGB = Color.HSBtoRGB(dHSB[0], dHSB[1], dHSB[2]);
        dor = (doRGB & 0xff0000) >> 16;
        dog = (doRGB & 0xff00) >> 8;
        dob = (doRGB & 0xff);

        float a = alpha * sa / 255f;
        float ac = 1 - a;

        dst[i] = (int) (a * dor + ac * dir);
        dst[i + 1] = (int) (a * dog + ac * dig);
        dst[i + 2] = (int) (a * dob + ac * dib);
        dst[i + 3] = (int) (sa * alpha + dia * ac);
      }
    }
Пример #10
0
  @NotNull
  public static Icon colorize(@NotNull final Icon source, @NotNull Color color, boolean keepGray) {
    float[] base = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);

    final BufferedImage image =
        UIUtil.createImage(source.getIconWidth(), source.getIconHeight(), Transparency.TRANSLUCENT);
    final Graphics2D g = image.createGraphics();
    source.paintIcon(null, g, 0, 0);
    g.dispose();

    final BufferedImage img =
        UIUtil.createImage(source.getIconWidth(), source.getIconHeight(), Transparency.TRANSLUCENT);
    int[] rgba = new int[4];
    float[] hsb = new float[3];
    for (int y = 0; y < image.getRaster().getHeight(); y++) {
      for (int x = 0; x < image.getRaster().getWidth(); x++) {
        image.getRaster().getPixel(x, y, rgba);
        if (rgba[3] != 0) {
          Color.RGBtoHSB(rgba[0], rgba[1], rgba[2], hsb);
          int rgb = Color.HSBtoRGB(base[0], base[1] * (keepGray ? hsb[1] : 1f), base[2] * hsb[2]);
          img.getRaster()
              .setPixel(x, y, new int[] {rgb >> 16 & 0xff, rgb >> 8 & 0xff, rgb & 0xff, rgba[3]});
        }
      }
    }

    return createImageIcon(img);
  }
Пример #11
0
 public static float[] HSBtoHSL(float hue, float saturation, float brightness, float[] hsl) {
   int rgb = Color.HSBtoRGB(hue, saturation, brightness);
   double r = ((rgb & 0xff0000) >> 16) / 255.;
   double g = ((rgb & 0x00ff00) >> 8) / 255.;
   double b = ((rgb & 0x0000ff)) / 255.;
   //		int rr = (rgb & 0xff0000)>>16;
   //		int gg = (rgb & 0x00ff00)>>8;
   //		int bb = (rgb & 0x0000ff);
   //		System.out.println("Red "+ rr + " Green " + gg + " Blue " + bb);
   //		System.out.println("Normalized: " + r + " " + g + " " + b);
   double max = Math.max(Math.max(r, g), b);
   double min = Math.min(Math.min(r, g), b);
   //		System.out.println(max + " " + min);
   if (hsl == null) hsl = new float[3];
   hsl[0] = hue;
   hsl[2] = (float) (.5 * (max + min));
   hsl[1] =
       (hsl[2] == 0)
           ? 0
           : (hsl[2] <= .5)
               ? (float) ((max - min) / (2 * hsl[2]))
               : (float) ((max - min) / (2 - 2 * hsl[2]));
   //		System.out.println("hue " + hsl[0] + " sat " + hsl[1] + " Luminosity " + hsl[2]);
   return hsl;
 }
Пример #12
0
 public static Color saturationAdjust(Color original, float saturation) {
   float[] hsb = getHSB(original);
   hsb[1] = saturation;
   if (hsb[1] < 0.0f) hsb[1] = 0.0f;
   else if (hsb[1] > 1.0f) hsb[1] = 1.0f;
   int newRGB = Color.HSBtoRGB(hsb[0], hsb[1], 1.0f);
   return new Color(newRGB);
 }
Пример #13
0
 public static Color brightnessAdjust(Color original, float brightness) {
   float[] hsb = getHSB(original);
   hsb[2] = brightness;
   if (hsb[2] < 0.0f) hsb[2] = 0.0f;
   else if (hsb[2] > 1.0f) hsb[2] = 1.0f;
   int newRGB = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
   return new Color(newRGB);
 }
Пример #14
0
  @Override
  public BufferedImage doTransform(BufferedImage src, BufferedImage dest) {
    int hueP = hue.getValue();
    int satP = saturation.getValue();
    int briP = brightness.getValue();

    if ((hueP == 0) && (satP == 0) && (briP == 0)) {
      return src;
    }

    float satShift = saturation.getValueAsPercentage();
    float briShift = brightness.getValueAsPercentage();
    float hueShift = hue.getValueAsFloat() / 360.0f;

    int[] srcData = ImageUtils.getPixelsAsArray(src);
    int[] destData = ImageUtils.getPixelsAsArray(dest);

    int length = srcData.length;
    assert length == destData.length;

    float[] tmpHSBArray = {0.0f, 0.0f, 0.0f};

    for (int i = 0; i < length; i++) {
      int rgb = srcData[i];
      int a = rgb & 0xFF000000;
      int r = (rgb >>> 16) & 0xFF;
      int g = (rgb >>> 8) & 0xFF;
      int b = (rgb) & 0xFF;

      tmpHSBArray = Color.RGBtoHSB(r, g, b, tmpHSBArray);

      float shiftedHue = tmpHSBArray[0] + hueShift;
      float shiftedSat = tmpHSBArray[1] + satShift;
      float shiftedBri = tmpHSBArray[2] + briShift;

      if (shiftedSat < 0.0f) {
        shiftedSat = 0.0f;
      }
      if (shiftedSat > 1.0f) {
        shiftedSat = 1.0f;
      }

      if (shiftedBri < 0.0f) {
        shiftedBri = 0.0f;
      }
      if (shiftedBri > 1.0f) {
        shiftedBri = 1.0f;
      }

      int newRGB = Color.HSBtoRGB(shiftedHue, shiftedSat, shiftedBri); // alpha is 255 here
      newRGB &= 0x00FFFFFF; // set alpha to 0
      destData[i] = a | newRGB; // add the real alpha
    }

    return dest;
  }
Пример #15
0
 public NBTTagCompound createDefault() {
   NBTTagCompound nbt = new NBTTagCompound("tag");
   nbt.setInteger("material", 0);
   nbt.setString("name", "Blank Artifact");
   nbt.setString("icon", "Artifact");
   int col = Color.HSBtoRGB((float) (rand.nextInt(360) / 360F), .8f, 1);
   nbt.setLong("overlay_color", col);
   nbt.setIntArray("allComponents", new int[] {0, 0, 0, 0, 0});
   return nbt;
 }
Пример #16
0
 public void generateColorWheel() {
   for (int index = 0; index < myPixels.length; index++) {
     if (myAlphas[index] != 0) {
       myPixels[index] =
           myAlphas[index]
               | 0xffffff & Color.HSBtoRGB(myHues[index], mySat[index], myBrightness);
     }
   }
   newPixels();
 }
Пример #17
0
 private static Color brightness(Color color, double newBrightness, double newBrightnessKof) {
   //        color.
   float[] hue = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
   float h = hue[0];
   float s = hue[1];
   float b =
       (float)
           (((double) hue[2] * (1d - newBrightnessKof))
               + ((double) newBrightness * newBrightnessKof));
   int rgb = Color.HSBtoRGB(h, s, b);
   return new Color(rgb);
 }
Пример #18
0
  /**
   * Creates a new color that is 30% less saturated
   *
   * @return
   */
  public Color lessSaturated() {
    float[] hsb =
        java.awt.Color.RGBtoHSB((int) (r * 255f), (int) (g * 255f), (int) (b * 255f), null);
    hsb[1] *= 0.7f;

    int rgbColor = java.awt.Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);

    int r = (rgbColor & 0x00FF0000) >> 16;
    int g = (rgbColor & 0x0000FF00) >> 8;
    int b = rgbColor & 0x000000FF;
    return new Color(r, g, b, (int) this.a * 255);
  }
Пример #19
0
  @Override
  public int getColorFromItemStack(ItemStack par1ItemStack, int par2) {
    if (par2 == 0) return super.getColorFromItemStack(par1ItemStack, par2);

    Element element = Element.class.getEnumConstants()[par1ItemStack.getItemDamage()];
    float bright =
        (float)
            Math.cos(
                (double) ClientTickHandler.elapsedClientTicks
                    / (double) ResourcesReference.BRIGHTNESS_DIVISOR_WAND);
    return Color.HSBtoRGB(
        element.getHue() / 360F,
        1F,
        ConfigurationHandler.wandFlicker ? Math.max(0.2F, (bright + 1F) / 2F) : 0.9F);
  }
Пример #20
0
  @Nullable
  private Color gatherRGB() {
    try {
      final int r = Integer.parseInt(myRed.getText());
      final int g = Integer.parseInt(myGreen.getText());
      final int b = Integer.parseInt(myBlue.getText());

      //noinspection UseJBColor
      return isRGBMode()
          ? new Color(r, g, b)
          : new Color(Color.HSBtoRGB(((float) r) / 360f, ((float) g) / 100f, ((float) b) / 100f));
    } catch (Exception ignore) {
    }
    return null;
  }
Пример #21
0
 public int filterRGB(int x, int y, int rgb) {
   int a = rgb & 0xff000000;
   int r = (rgb >> 16) & 0xff;
   int g = (rgb >> 8) & 0xff;
   int b = rgb & 0xff;
   Color.RGBtoHSB(r, g, b, hsb);
   hsb[0] += hFactor;
   while (hsb[0] < 0) hsb[0] += Math.PI * 2;
   hsb[1] += sFactor;
   if (hsb[1] < 0) hsb[1] = 0;
   else if (hsb[1] > 1.0) hsb[1] = 1.0f;
   hsb[2] += bFactor;
   if (hsb[2] < 0) hsb[2] = 0;
   else if (hsb[2] > 1.0) hsb[2] = 1.0f;
   rgb = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
   return a | (rgb & 0xffffff);
 }
Пример #22
0
  @Override
  public Paint getPaint(double value) {
    float hue;
    if (((value < this.lowerBound) || (value > this.upperBound)) && mode) {
      return Color.black;
    } else {
      value = Math.max(value, this.lowerBound);
      value = Math.min(value, this.upperBound);

      if (colormode) {
        hue = (float) ((this.upperBound - value) / (this.upperBound - this.lowerBound) * 0.666667);
      } else {
        hue = (float) ((value - this.lowerBound) / (this.upperBound - this.lowerBound) * 0.666667);
      }
    }
    return new Color(Color.HSBtoRGB(hue, (float) 1.0, (float) 1.0));
  }
  private static Color createStackColour(int theNum, int theIndex) {
    if (theNum == 0) {
      return Color.black;
    }

    float[] hsbVals = provideNewRainbowHsb();

    double step = 1.0 / theNum;
    for (int i = 0; i < theIndex; i++) {
      hsbVals[0] += step;
      while (hsbVals[0] > 1.0) {
        hsbVals[0] -= 1.0;
      }
    }

    int next = Color.HSBtoRGB(hsbVals[0], hsbVals[1], hsbVals[2]);
    return (new Color(next));
  }
Пример #24
0
  public static BufferedImage doTints(
      BufferedImage img, float tint, float grey, boolean thumb, boolean profPic) {

    int WIDTH = img.getWidth();
    int HEIGHT = img.getHeight();

    for (int x = 0; x < WIDTH; ++x) {
      for (int y = 0; y < HEIGHT; ++y) {
        Color c = new Color(img.getRGB(x, y));
        int red = c.getRed();
        int green = c.getGreen();
        int blue = c.getBlue();

        float[] hsb = new float[3];
        c.RGBtoHSB(red, green, blue, hsb);

        if (tint != -1) {
          hsb[0] = tint; // change this to change the tint
        }

        if (grey != -1) {
          hsb[1] = grey; // change this to change the greyscale
        }
        int rgb;

        rgb = c.HSBtoRGB(hsb[0], hsb[1], hsb[2]);

        img.setRGB(x, y, rgb);
      }
    }

    if (thumb) {
      img = resize(img, Method.SPEED, 250, null, null);
      return pad(img, 1);
    } else {
      if (profPic) {
        img = resize(img, 200, null);
        return pad(img, 1);
      } else {
        return pad(img, 4);
      }
    }
  }
Пример #25
0
  public Painting paint(double width, double height) {
    Painting painting = new Painting(width, height);

    int backgroundColor =
        Color.HSBtoRGB(
            Application.RANDOMGEN.nextFloat(), randomFromTo(90, 100), randomFromTo(75, 100));
    painting.setCanvasColor(new Color(backgroundColor));

    for (int i = 0; i < 10; ++i) {
      // int fillColor = Color.HSBtoRGB(shapeHue(), randomFromTo(90, 100),
      // randomFromTo(75, 100));
      // int fillColor = r.nextInt(255);
      painting.addPaintInstruction(
          new ChangeColor(
              new Color(
                  Application.RANDOMGEN.nextInt(255),
                  Application.RANDOMGEN.nextInt(255),
                  Application.RANDOMGEN.nextInt(255))));

      float alpha = randomFromTo(90, 100) / 100;
      painting.addPaintInstruction(new SetTransparency(alpha));

      if (Application.RANDOMGEN.nextDouble() < 0.5) {

        double x = randomFromTo(width / 8, ((width / 8) * 7));
        double y = randomFromTo(height / 8, ((height / 8) * 7));
        double w = randomFromTo(width / 6, width / 3);
        double h = randomFromTo(height / 6, height / 3);
        Ellipse2D ellipse = new Ellipse2D.Double(x, y, w, h);
        painting.addPaintInstruction(new PaintShape(ellipse));
      } else {
        Polygon quadrilateral = new Polygon();
        for (int j = 0; j < 4; ++j) {
          quadrilateral.addPoint(
              Application.RANDOMGEN.nextInt((int) width),
              Application.RANDOMGEN.nextInt((int) height));
        }
        painting.addPaintInstruction(new PaintShape(quadrilateral));
      }
    }

    return painting;
  }
Пример #26
0
  public static Color[] nColors(int n) {
    Color[] result = new Color[n];
    int nrRounds = (int) Math.ceil(log(3.0, (double) n));
    // System.out.println("nrRounds:" + nrRounds);
    int colorID = 0;

    float jumpSB = 0.5f / n;

    for (int round = 0; round < nrRounds; round++) {
      for (int i = 0; (i + round) < n; i = i + nrRounds) {
        // System.out.println("round:" + round + " i:" + i + " colorID:" + colorID);
        Color color;
        color = new Color(Color.HSBtoRGB(colorID * 1f / n, (1.0f - i * jumpSB), 0.95f));
        // color = new Color(Color.HSBtoRGB(colorID * 1f/classStrings.length, 1.0f, 1.0f));

        result[i + round] = color;
        colorID++;
      }
    }
    return result;
  }
  private static List<Color> createStackColours(int theNum) {
    ArrayList<Color> retVal = new ArrayList<>();
    if (theNum == 0) {
      return retVal;
    }

    retVal.add(RAINBOW_COLOUR_0);
    float[] hsbVals = provideNewRainbowHsb();

    double step = 1.0 / theNum;
    for (int i = 1; i < theNum; i++) {
      hsbVals[0] += step;
      while (hsbVals[0] > 1.0) {
        hsbVals[0] -= 1.0;
      }

      int next = Color.HSBtoRGB(hsbVals[0], hsbVals[1], hsbVals[2]);
      retVal.add(new Color(next));
    }

    return retVal;
  }
Пример #28
0
  @Override
  protected void loop() {
    Display2D display = getDisplay();
    byte userInput = getUserInput();
    Graphics2D g = display.getGraphics();

    switch (gameState) {
      case IDLE:
        animTime = 0;
        animTimeLastStep = 0;
        logoPos = 0;
        gameState = State.IDLE_ANIM;
        break;
      case IDLE_ANIM:
        animTime += timestep;
        if (animTime - animTimeLastStep >= LOGO_ANIM_STEP) {
          animTimeLastStep = animTime;
          logoPos++;
          if (logoPos > 100) gameState = State.IDLE;
        }
        g.drawImage(mitrisLogo, 10 - logoPos, 6, null);

        switch (userInput) {
          case 'L':
          case 'R':
          case 'U':
          case 'D':
            gameState = State.GAME_START;
        }
        break;
      case GAME_START:
        mitrisGame = new MITrisGame(width, height, timestep);
        gameState = State.GAME;
        break;
      case GAME:
        // check validity of user move
        switch (userInput) {
          case 'L':
            mitrisGame.moveLeft();
            break;
          case 'R':
            mitrisGame.moveRight();
            break;
          case 'U':
            mitrisGame.rotatePiece();
            break;
          case 'D':
            mitrisGame.dropPiece();
            break;
          case -1: // there was an error in the network socket or no client connected -- "pause" the
                   // game
            return;
        }
        // move piece down if it's time
        mitrisGame.clockTick();
        gameDisplayTime = mitrisGame.getTime();
        sendBoardToDisplay(mitrisGame.getDisplayBoard(), display);
        if (mitrisGame.isGameOver()) {
          System.out.println(
              String.format(
                  "Game over! Lines cleared: %d  Time: %3.1f",
                  mitrisGame.getDisplayBoard().getNumCleared(), mitrisGame.getTime()));
          gameState = State.GAME_END_1;
          gameOverBoard = mitrisGame.getDisplayBoard();
          animTime = 0;
        }
        break;
      case GAME_END_1:
        animTime += timestep;
        if (animTime > GAME_END_WAIT) {
          gameState = State.GAME_END_2;
          animTime = 0;
          animTimeLastStep = 0;
          if (gameOverBoard.getLevel() >= 8) {
            gameState = State.GAME_END_ALT_2;
            circTime = new double[] {-100, -100, -100, -100, -100};
            circX = new double[5];
            circY = new double[5];
            circHue = new double[5];
            circPos = 0;
          }
        }
        sendBoardToDisplay(gameOverBoard, display);
        break;
      case GAME_END_2:
        animTime += timestep;
        if (animTime - animTimeLastStep >= ANIM_TIME_STEP) {
          animTimeLastStep = animTime;
          if (gameOverBoard.isBoardEmpty()) {
            gameState = State.IDLE;
          }
          gameOverBoard = gameOverBoard.shiftBoardDown();
        }
        sendBoardToDisplay(gameOverBoard, display);
        break;
      case GAME_END_ALT_2:
        animTime += timestep;
        if (animTime >= CIRC_ANIM_FADE_WAIT_TIME) {
          if (animTime <= CIRC_ANIM_FADE_WAIT_TIME + CIRC_ANIM_FADE_TIME) {
            double brightness = 1 - (animTime - CIRC_ANIM_FADE_WAIT_TIME) / CIRC_ANIM_FADE_TIME;
            sendBoardToDisplay(gameOverBoard, display, brightness);
          }
        } else sendBoardToDisplay(gameOverBoard, display);
        if (animTime - animTimeLastStep >= CIRC_ADD_TIME && animTime <= CIRC_ANIM_STOPADD_TIME) {
          animTimeLastStep = animTime;
          circX[circPos] = Math.random() * width;
          circY[circPos] = Math.random() * height;
          circTime[circPos] = animTime - 0.3;
          circHue[circPos] = Math.random();
          circPos = (circPos + 1) % circX.length;
        }
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        for (int k = 0; k < circX.length; k++) {
          g.setColor(new Color(Color.HSBtoRGB((float) circHue[k], 1, 1)));
          g.setStroke(new BasicStroke(1.5f));
          double circH = (animTime - circTime[k]) * 5;
          double circW = circH / display.getPixelAspect();
          g.draw(new Ellipse2D.Double(circX[k] - circW / 2, circY[k] - circH / 2, circW, circH));
        }
        if (animTime >= CIRC_ANIM_TOTAL_TIME) gameState = State.IDLE;
    }
  }
Пример #29
0
  /** Here we actually draw the scene. */
  public void display(GLAutoDrawable drawable) {
    super.display(drawable);

    System.out.println("GLScene - display");
    GL gl = drawable.getGL();

    try {
      // Update velocities and positions of all text
      float deltaT = 0.1f;
      Point2f tmp = new Point2f();

      for (Iterator<TextInfo> iter = textInfo.iterator(); iter.hasNext(); ) {
        TextInfo info = (TextInfo) iter.next();

        // Randomize things a little bit at run time
        if (random.nextInt(1000) == 0) {
          info.angularVelocity = INIT_ANG_VEL_MAG * (randomAngle() - 180);
          info.velocity = randomVelocity(INIT_VEL_MAG, INIT_VEL_MAG);
        }

        // Now update angles and positions
        info.angle += info.angularVelocity * deltaT;
        tmp.set(info.velocity);
        tmp.scale(deltaT);
        info.position.add(tmp);

        // Update color
        info.curTime += deltaT;
        if (info.curTime > 2 * Math.PI) {
          info.curTime -= 2 * Math.PI;
        }
        int rgb =
            Color.HSBtoRGB(info.h, (float) (0.5 * (1 + Math.sin(info.curTime)) * info.s), info.v);
        info.r = ((rgb >> 16) & 0xFF) / 255.0f;
        info.g = ((rgb >> 8) & 0xFF) / 255.0f;
        info.b = (rgb & 0xFF) / 255.0f;

        // Wrap angles and positions
        if (info.angle < 0) {
          info.angle += 360;
        } else if (info.angle > 360) {
          info.angle -= 360;
        }
        // Use maxTextWidth to avoid popping in/out at edges
        // Would be better to do oriented bounding rectangle computation
        if (info.position.x < -maxTextWidth) {
          info.position.x = info.position.x + glComposite.getCanvas().getWidth() + 2 * maxTextWidth;
        } else if (info.position.x > glComposite.getCanvas().getWidth() + maxTextWidth) {
          info.position.x = info.position.x - glComposite.getCanvas().getWidth() - 2 * maxTextWidth;
        }
        if (info.position.y < -maxTextWidth) {
          info.position.y =
              info.position.y + glComposite.getCanvas().getHeight() + 2 * maxTextWidth;
        } else if (info.position.y > glComposite.getCanvas().getHeight() + maxTextWidth) {
          info.position.y =
              info.position.y - glComposite.getCanvas().getHeight() - 2 * maxTextWidth;
        }
      }

      gl.glClear(GL.GL_COLOR_BUFFER_BIT);
      gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glLoadIdentity();
      glComposite
          .getGLU()
          .gluOrtho2D(
              0, glComposite.getCanvas().getWidth(), 0, glComposite.getCanvas().getHeight());
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glLoadIdentity();

      // Draw the background texture
      backgroundTexture.enable();
      backgroundTexture.bind();
      TextureCoords coords = backgroundTexture.getImageTexCoords();
      int w = glComposite.getCanvas().getWidth();
      int h = glComposite.getCanvas().getHeight();
      float fw = w / 100.0f;
      float fh = h / 100.0f;
      gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);
      gl.glBegin(GL.GL_QUADS);
      gl.glTexCoord2f(fw * coords.left(), fh * coords.bottom());
      gl.glVertex3f(0, 0, 0);
      gl.glTexCoord2f(fw * coords.right(), fh * coords.bottom());
      gl.glVertex3f(w, 0, 0);
      gl.glTexCoord2f(fw * coords.right(), fh * coords.top());
      gl.glVertex3f(w, h, 0);
      gl.glTexCoord2f(fw * coords.left(), fh * coords.top());
      gl.glVertex3f(0, h, 0);
      gl.glEnd();
      backgroundTexture.disable();

      // Render all text
      renderer.beginRendering(w, h);

      // Note we're doing some slightly fancy stuff to position the text.
      // We tell the text renderer to render the text at the origin, and
      // manipulate the modelview matrix to put the text where we want.

      gl.glMatrixMode(GL.GL_MODELVIEW);

      // First render drop shadows
      renderer.setColor(0, 0, 0, 0.5f);
      for (Iterator<TextInfo> iter = textInfo.iterator(); iter.hasNext(); ) {
        TextInfo info = (TextInfo) iter.next();
        gl.glLoadIdentity();
        gl.glTranslatef(
            info.position.x + dropShadowDistance, info.position.y - dropShadowDistance, 0);
        gl.glRotatef(info.angle, 0, 0, 1);
        renderer.draw(info.text, 0, 0);
        renderer.flush();
      }

      // Now render the actual text
      for (Iterator<TextInfo> iter = textInfo.iterator(); iter.hasNext(); ) {
        TextInfo info = (TextInfo) iter.next();
        gl.glLoadIdentity();
        gl.glTranslatef(info.position.x, info.position.y, 0);
        gl.glRotatef(info.angle, 0, 0, 1);
        renderer.setColor(info.r, info.g, info.b, 1);
        renderer.draw(info.text, 0, 0);
        renderer.flush();
      }

      renderer.endRendering();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Пример #30
0
  private void rebuildGradient() {
    xKnots[0] = -1;
    xKnots[numKnots - 1] = 256;
    yKnots[0] = yKnots[1];
    yKnots[numKnots - 1] = yKnots[numKnots - 2];

    for (int i = 1; i < numKnots - 1; i++) {
      float spanLength = xKnots[i + 1] - xKnots[i];
      int end = xKnots[i + 1];
      if (i == numKnots - 2) end++;
      for (int j = xKnots[i]; j < end; j++) {
        int rgb1 = yKnots[i];
        int rgb2 = yKnots[i + 1];
        float hsb1[] = Color.RGBtoHSB((rgb1 >> 16) & 0xff, (rgb1 >> 8) & 0xff, rgb1 & 0xff, null);
        float hsb2[] = Color.RGBtoHSB((rgb2 >> 16) & 0xff, (rgb2 >> 8) & 0xff, rgb2 & 0xff, null);
        float t = (j - xKnots[i]) / spanLength;
        int type = getKnotType(i);
        int blend = getKnotBlend(i);

        if (j >= 0 && j <= 255) {
          switch (blend) {
            case CONSTANT:
              t = 0;
              break;
            case LINEAR:
              break;
            case SPLINE:
              //						map[i] = ImageMath.colorSpline(j, numKnots, xKnots, yKnots);
              t = ImageMath.smoothStep(0.15f, 0.85f, t);
              break;
            case CIRCLE_UP:
              t = t - 1;
              t = (float) Math.sqrt(1 - t * t);
              break;
            case CIRCLE_DOWN:
              t = 1 - (float) Math.sqrt(1 - t * t);
              break;
          }
          //					if (blend != SPLINE) {
          switch (type) {
            case RGB:
              map[j] = ImageMath.mixColors(t, rgb1, rgb2);
              break;
            case HUE_CW:
            case HUE_CCW:
              if (type == HUE_CW) {
                if (hsb2[0] <= hsb1[0]) hsb2[0] += 1.0f;
              } else {
                if (hsb1[0] <= hsb2[1]) hsb1[0] += 1.0f;
              }
              float h = ImageMath.lerp(t, hsb1[0], hsb2[0]) % (ImageMath.TWO_PI);
              float s = ImageMath.lerp(t, hsb1[1], hsb2[1]);
              float b = ImageMath.lerp(t, hsb1[2], hsb2[2]);
              map[j] = 0xff000000 | Color.HSBtoRGB(h, s, b); // FIXME-alpha
              break;
          }
          //					}
        }
      }
    }
  }