Ejemplo n.º 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);
  }
Ejemplo n.º 2
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();
    }
  }
Ejemplo n.º 3
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()));
 }
Ejemplo n.º 4
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();
 }
Ejemplo n.º 5
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;
  }
Ejemplo n.º 6
0
  /** Regenerates the image. */
  private synchronized void regenerateImage() {
    int size =
        Math.min(
            MAX_SIZE,
            Math.min(
                getWidth() - imagePadding.left - imagePadding.right,
                getHeight() - imagePadding.top - imagePadding.bottom));

    if (mode == ColorPicker.BRI || mode == ColorPicker.SAT) {
      float bri2 = this.bri;
      float sat2 = this.sat;
      float radius = ((float) size) / 2f;
      float hue2;
      float k = 1.2f; // the number of pixels to antialias
      for (int y = 0; y < size; y++) {
        float y2 = (y - size / 2f);
        for (int x = 0; x < size; x++) {
          float x2 = (x - size / 2f);
          double theta = Math.atan2(y2, x2) - 3 * Math.PI / 2.0;
          if (theta < 0) theta += 2 * Math.PI;

          double r = Math.sqrt(x2 * x2 + y2 * y2);
          if (r <= radius) {
            if (mode == ColorPicker.BRI) {
              hue2 = (float) (theta / (2 * Math.PI));
              sat2 = (float) (r / radius);
            } else { // SAT
              hue2 = (float) (theta / (2 * Math.PI));
              bri2 = (float) (r / radius);
            }
            row[x] = Color.HSBtoRGB(hue2, sat2, bri2);
            if (r > radius - k) {
              int alpha = (int) (255 - 255 * (r - radius + k) / k);
              if (alpha < 0) alpha = 0;
              if (alpha > 255) alpha = 255;
              row[x] = row[x] & 0xffffff + (alpha << 24);
            }
          } else {
            row[x] = 0x00000000;
          }
        }
        image.getRaster().setDataElements(0, y, size, 1, row);
      }
    } else if (mode == ColorPicker.HUE) {
      float hue2 = this.hue;
      for (int y = 0; y < size; y++) {
        float y2 = ((float) y) / ((float) size);
        for (int x = 0; x < size; x++) {
          float x2 = ((float) x) / ((float) size);
          row[x] = Color.HSBtoRGB(hue2, x2, y2);
        }
        image.getRaster().setDataElements(0, y, image.getWidth(), 1, row);
      }
    } else { // mode is RED, GREEN, or BLUE
      int red2 = red;
      int green2 = green;
      int blue2 = blue;
      for (int y = 0; y < size; y++) {
        float y2 = ((float) y) / ((float) size);
        for (int x = 0; x < size; x++) {
          float x2 = ((float) x) / ((float) size);
          if (mode == ColorPicker.RED) {
            green2 = (int) (x2 * 255 + .49);
            blue2 = (int) (y2 * 255 + .49);
          } else if (mode == ColorPicker.GREEN) {
            red2 = (int) (x2 * 255 + .49);
            blue2 = (int) (y2 * 255 + .49);
          } else {
            red2 = (int) (x2 * 255 + .49);
            green2 = (int) (y2 * 255 + .49);
          }
          row[x] = 0xFF000000 + (red2 << 16) + (green2 << 8) + blue2;
        }
        image.getRaster().setDataElements(0, y, size, 1, row);
      }
    }
    repaint();
  }
Ejemplo n.º 7
0
 private void setHSBValue(float h, float s, float b, int opacity) {
   //noinspection UseJBColor
   Color rgb = new Color(Color.HSBtoRGB(h, s, b));
   setColor(ColorUtil.toAlpha(rgb, opacity), this);
 }
Ejemplo n.º 8
0
class PanelForSquare extends JPanel implements Runnable, MouseListener, ActionListener {

  /** */
  private static final long serialVersionUID = 1L;

  Random randomNumber = new Random();
  // Int variable maxChange is the largest amount by which dX and dY
  // can change by when they collide.  This speed limiter is to prevent
  // Them from bouncing around the screen in an absurd manner, which
  // would prevent them from being clicked properly
  int maxChange = 3;

  // Starting parameters for the first MyYinYang object
  int yin1X = 100;
  int yin1Y = 100;
  int yin1Width = 150;
  int yin1Height = 150;

  // The four random directions in which the yinYangs will travel.
  // The deltas for X and Y 1 are multiplied by -1 so that it goes to the
  // top left to prevent them from colliding and scaling too early
  int deltaX1 = (randomNumber.nextInt(maxChange) + 1) * -1;
  int deltaY1 = (randomNumber.nextInt(maxChange) + 1) * -1;
  int deltaX2 = randomNumber.nextInt(maxChange) + 1;
  int deltaY2 = randomNumber.nextInt(maxChange) + 1;

  // Initial parameters for the second yinYang object
  int yin2X = 600;
  int yin2Y = 600;
  int yin2Width = 90;
  int yin2Height = 90;

  // Four int variables which track if the mouse click is
  // inside of either shape
  int x1Clicked, y1Clicked, x2Clicked, y2Clicked;

  // Points currently scored in the game
  int points = 0;

  // Remaining amount of times you can miss a target before the game ends
  int misses = 3;

  // New font object to alter how the text is displayed on the panel
  Font font = new Font("Sans-Serif", Font.BOLD, 35);

  // The two MyYinYang objects being initially created with the
  // previously defined stats
  MyYinYang yin1 = new MyYinYang(yin1X, yin1Y, yin1Width, yin1Height);
  MyYinYang yin2 = new MyYinYang(yin2X, yin2Y, yin2Width, yin2Height);

  // Frame for game over screen
  final JFrame frame = new JFrame();
  JPanel panel = new JPanel();

  // Two color objects which start out being randomly generated
  Color color1 =
      new Color(
          Color.HSBtoRGB(
              randomNumber.nextFloat(), randomNumber.nextFloat(), randomNumber.nextFloat()));
  Color color2 =
      new Color(
          Color.HSBtoRGB(
              randomNumber.nextFloat(), randomNumber.nextFloat(), randomNumber.nextFloat()));

  // Panel size
  public static final int PANEL_WIDTH = 1000;
  public static final int PANEL_HEIGHT = 800;

  // Variable to display how much time is left in seconds as the time value is in the thousands
  Double timeDisplay = 0.0;

  // Degrees by which to rotate the shape
  double degrees = 0;

  // How much time is left in the game
  Integer timeLeft = 12000;

  private void initTimer() {
    Timer timer;
    timer = new Timer(timeLeft, this);
    timer.setInitialDelay(0);
    timer.start();
  }

  public PanelForSquare() {
    setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
    setBackground(Color.white);

    Thread thread = new Thread(this);
    initTimer();
    thread.start();
  }

  @Override
  // TODO Auto-generated method stub
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

    // Draws the two initial two YinYang symbols on the frame
    g2.setColor(color1);
    if (timeLeft < 6000) {
      g2.rotate(degrees, yin1X + (yin1Width / 2), yin1Y + (yin1Height / 2));
    }
    g2.fill(new MyYinYang(yin1X, yin1Y, yin1Width, yin1Height));
    if (timeLeft < 6000) {
      g2.rotate(-degrees, yin1X + (yin1Width / 2), yin1Y + (yin1Height / 2));
    }
    g2.setColor(color2);
    if (timeLeft < 6000) {
      g2.rotate(degrees, yin2X + (yin2Width / 2), yin2Y + (yin2Height / 2));
    }
    g2.fill(new MyYinYang(yin2X, yin2Y, yin2Width, yin2Height));
    if (timeLeft < 6000) {
      g2.rotate(-degrees, yin2X + (yin2Width / 2), yin2Y + (yin2Height / 2));
    }

    // Alters and prints the text on the screen
    g2.setColor(Color.BLACK);
    g2.setFont(font);
    g2.drawString("Score", 700, 25);
    g2.drawString(Integer.toString(points), 700, 75);
    g2.drawString("Misses", 800, 25);
    g2.drawString(Integer.toString(misses), 800, 75);
    g2.drawString("Time Left", 700, 110);
    timeDisplay = timeLeft / 100.0;
    g2.drawString(timeDisplay.toString(), 700, 160);
  }

  public void run() {
    // Mouse listener to check if the mouse has been clicked
    addMouseListener(this);
    while (true) {
      // Changes the next X and Y value for each
      // shape according the their defined delta values
      yin1X += deltaX1;
      yin1Y += deltaY1;
      yin2X += deltaX2;
      yin2Y += deltaY2;

      // Makes the shapes bounce faster the longer the game elapses
      // Also decreases the score appropriately
      if (timeLeft < 9000) {
        maxChange = 4;
        points -= 1;
      } else if (timeLeft < 7500) {
        maxChange = 6;
        points -= 2;
      } else if (timeLeft < 6000) {
        maxChange = 7;
        points -= 3;
      } else if (timeLeft < 4500) {
        maxChange = 8;
        points -= 4;
      } else if (timeLeft < 3000) {
        maxChange = 10;
        points -= 5;
      }

      if (timeLeft == 0 || misses <= 0) {
        JOptionPane.showMessageDialog(frame, "Game over. Your final score is:  " + points);
        int status = 0;
        System.exit(status);
      }

      // Method to check if either shape has hit the edge of the panel yet
      calculateEdges();

      // Method to check if the two objects have collided with each other
      checkForCollision();

      degrees += .2;
      timeLeft--;
      // Calls PaintComponent again to redraw the screen
      repaint();
      try {
        Thread.sleep(10);
      } catch (InterruptedException ex) {
      }
    }
  }

  // Method to check if the two YinYang shapes have collided with each other,
  // and subsequently perform scaling operations if they have
  private void checkForCollision() {
    // Creates two MyYinYang objects with the already used coordinates
    // for use in the changing of color, direction, and scale once they
    // have collided
    yin1 = new MyYinYang(yin1X, yin1Y, yin1Width, yin1Height);
    yin2 = new MyYinYang(yin2X, yin2Y, yin2Width, yin2Height);

    if (hasCollided(yin1)) {
      // If the two shapes have collided, then the
      // shapes will reverse direction to go on until they
      // hit the edges of the screen
      // The reason the directions are not random
      // is because if they don't bounce away
      // from each other, they keep colliding
      // until they fly off of the screen

      deltaX1 = deltaX1 * -1;
      deltaY1 = deltaY1 * -1;
      deltaX2 = deltaX2 * -1;
      deltaY2 = deltaY2 * -1;

      // Commands to scale the two Yin Yang shapes based on the current
      // size when being compared to each other
      // IF SECTION IS IF YIN1 IS LARGER, ELSE IS IF THE SECOND IS LARGER
      double scale = 0;
      if (yin1.getWidth() >= yin2.getWidth()) {
        // Uses a do while loop so that it executes once, but if
        // the scale ends up being 0, it does it again
        do {
          // Scales by a random double between .5 and 1
          scale = ((randomNumber.nextDouble() + .5) - randomNumber.nextDouble());
        } while (scale <= 0);

        // Yin1Width is now set to it's original value times the scale
        // which has been set
        yin1Width = (int) (scale * yin1Width);
        // Simple equality so that the height and width are the same, since it is a circular object
        yin1Height = yin1Width;

        do {
          // Scales by a factor between 1 and 2
          scale = randomNumber.nextDouble() + 1;
        } while (scale <= 0);
        yin2Width = (int) (yin2Width * scale);
        yin2Height = yin2Width;
      } else {
        do {
          scale = randomNumber.nextDouble() + 1;
        } while (scale <= 0);
        yin1Width = (int) (yin1Width * scale);
        yin1Height = yin1Width;

        do {
          scale = ((randomNumber.nextDouble() + .5) - randomNumber.nextDouble());
        } while (scale <= 0);
        yin2Width = (int) (yin2Width * scale);
        yin2Height = yin2Width;
      }
    }
  }

  public boolean hasCollided(MyYinYang y1) {
    // Checks to see if the bounds of the first yinYang intersect anywhere within
    // the bounds of the second yinYang
    return y1.getBounds().intersects(yin2X, yin2Y, yin2Width, yin2Height);
  }

  private void calculateEdges() {

    // Calculates the new X and Y values to become the
    // destination once the edges are hit, taking
    // into account that they are randomly
    // scaled so that the speed changes
    // once the edges are hit

    int destX1 = randomNumber.nextInt(maxChange);
    int destY1 = randomNumber.nextInt(maxChange);
    int destX2 = randomNumber.nextInt(maxChange);
    int destY2 = randomNumber.nextInt(maxChange);

    // Checks to see if the right side of yin1
    // has hit right edge.  Bounces off on
    // the inverse angle in a different color if it has
    if ((yin1X + yin1Width) >= PANEL_WIDTH) {
      destX1 = randomNumber.nextInt(maxChange);
      deltaX1 = destX1 * -1;
      color1 = changeColor();
    }

    // Checks if the right side of yin2 has hit the right edge
    if ((yin2X + yin2Width) >= PANEL_WIDTH) {
      destX2 = randomNumber.nextInt(maxChange);
      deltaX2 = destX2 * -1;
      color2 = changeColor();
    }

    // Checks if the left side of yin1 has hit the left edge
    if (yin1X <= 0) {
      destX1 = randomNumber.nextInt(maxChange);
      deltaX1 = destX1;
      color1 = changeColor();
    }

    // Checks if the left side of yin2 has hit the left edge
    if (yin2X <= 0) {
      destX2 = randomNumber.nextInt(maxChange);
      deltaX2 = destX2;
      color2 = changeColor();
    }

    // Checks if the bottom side of yin1 has hit the bottom edge
    if ((yin1Y + yin1Height) >= PANEL_HEIGHT) {
      destY1 = randomNumber.nextInt(maxChange);
      deltaY1 = destY1 * -1;
      color1 = changeColor();
    }

    // Checks if the bottom side of yin2 has hit the bottom edge
    if ((yin2Y + yin2Height) >= PANEL_HEIGHT) {
      destY2 = randomNumber.nextInt(maxChange);
      deltaY2 = destY2 * -1;
      color2 = changeColor();
    }

    // Checks if the top side of yin1 has hit the top edge
    if (yin1Y <= 0) {
      destY1 = randomNumber.nextInt(maxChange);
      deltaY1 = destY1;
      color1 = changeColor();
    }

    // Checks if the top side of yin2 has hit the top edge
    if (yin2Y <= 0) {
      destY2 = randomNumber.nextInt(maxChange);
      deltaY2 = destY2;
      color2 = changeColor();
    }
  }

  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()));
  }

  @Override
  public void mouseClicked(MouseEvent event) {}

  @Override
  public void mouseEntered(MouseEvent event) {}

  @Override
  public void mouseExited(MouseEvent arg0) {}

  @Override
  public void mousePressed(MouseEvent event) {
    x1Clicked = event.getX();
    y1Clicked = event.getY();
    x2Clicked = event.getX();
    y2Clicked = event.getY();

    // Conditions to update the score depending on speed and size of shapes
    if (yin1.getBounds().contains(x1Clicked, y1Clicked)) {
      if (yin1.getWidth() > 250) {
        if (deltaX1 < 2) {
          points += 1;
        } else if (deltaX1 < 3) {
          points += 3;
        } else if (deltaX1 < 6) {
          points += 5;
        } else {
          points += 7;
        }
      } else if (yin1.getWidth() > 150) {
        if (deltaX1 < 2) {
          points += 15;
        } else if (deltaX1 < 3) {
          points += 35;
        } else if (deltaX1 < 6) {
          points += 55;
        } else {
          points += 75;
        }
      } else {
        if (deltaX1 < 2) {
          points += 125;
        } else if (deltaX1 < 3) {
          points += 225;
        } else if (deltaX1 < 6) {
          points += 325;
        } else {
          points += 425;
        }
      }
    } else if (yin2.getBounds().contains(x2Clicked, y2Clicked)) {
      if (yin2.getWidth() > 250) {
        if (deltaX2 < 2) {
          points += 1;
        } else if (deltaX2 < 3) {
          points += 3;
        } else if (deltaX2 < 6) {
          points += 5;
        } else {
          points += 7;
        }
      } else if (yin2.getWidth() > 150) {
        if (deltaX2 < 2) {
          points += 15;
        } else if (deltaX2 < 3) {
          points += 35;
        } else if (deltaX2 < 6) {
          points += 55;
        } else {
          points += 75;
        }
      } else {
        if (deltaX2 < 2) {
          points += 125;
        } else if (deltaX2 < 3) {
          points += 225;
        } else if (deltaX2 < 6) {
          points += 325;
        } else {
          points += 425;
        }
      }
    } else {
      misses--;
    }
  }

  @Override
  public void mouseReleased(MouseEvent arg0) {}

  @Override
  public void actionPerformed(ActionEvent arg0) {}
}