• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

random bright coloe

i can generate random color but sometimes the  color gets to dark anyway to solve this problem? below is how random color is generated

int red =  ( int ) ( Math.random ( ) * 256 );
etc
etc
0
bobbybuilder
Asked:
bobbybuilder
  • 3
  • 2
1 Solution
 
CEHJCommented:
The idea being, you can leave the brightness high and simply vary the hue and saturation
0
 
bobbybuilderAuthor Commented:
i have tried it, my attempt is below is that the way to go about it. i still have to fiddle with the numbers as i still get dark colors  

int red =  ( int ) ( Math.random ( ) * 256 ); <-should these be float or it does not matter
   int blue =   ( int ) ( Math.random ( ) * 256 );
   int green = ( int ) ( Math.random ( ) * 256 );
     
   Color c = new Color(red,blue,green);  

c.getHSBColor( (float) 500 ,(float) 0.1 , (float) 1.0);
0
 
CEHJCommented:
This is the sort of thing i mean:

import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class RandomColor {

  public static void main(String[] args){
    JFrame f = new JFrame();
    ShapePanel shapePanel = new ShapePanel();
    f.getContentPane().add(shapePanel);
    new Thread(shapePanel).start();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(300, 200);
    f.setVisible(true);
  }

}

class ShapePanel extends JPanel implements Runnable {
  boolean highlighted;
  Shape shape;
  Paint currentPaint;

  public ShapePanel() {
    super();
    shape = new Rectangle2D.Float(0.f, 0.f, 100.f, 50.f);
    currentPaint = Color.red;
  }

  public void paintComponent(Graphics _g) {
    Graphics2D g = (Graphics2D)_g;
    g.setPaint(currentPaint);
    g.fill(shape);
    g.draw(shape);
  }

  public Paint getCurrentPaint() {
    return Color.getHSBColor((float)Math.random(), (float)Math.random(), 1.0f);
  }


  public void run() {
    while (true) {
      try {
        Thread.sleep(1000);
        currentPaint = getCurrentPaint();
        repaint();
      }
      catch(Exception e){
        e.printStackTrace();
      }
    }
  }

}
0
 
bobbybuilderAuthor Commented:
thxs
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now