Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. import java.awt.Graphics;
  2. import java.awt.Color;
  3. import javax.swing.JComponent;
  4. import javax.swing.JFrame;
  5. import java.awt.image.BufferedImage;
  6.  
  7. public class Test {
  8. public static void main(String[] args){
  9. int n = 100;
  10. float[][] map = new float[n][n];
  11.  
  12. for(int i=0; i<n;i++){
  13. for(int j=0; j<n;j++){
  14. map[i][j] = (float) Math.random();
  15. }
  16. }
  17. showImage(map);
  18. }
  19.  
  20. public static void showImage(float[][] map){
  21.  
  22.  
  23.  
  24.  
  25. float maxHeight = Float.MIN_VALUE;
  26. float minHeight = Float.MAX_VALUE;
  27. for (float[] row : map){
  28. for (float height : row){
  29. if (height > maxHeight){
  30. maxHeight = height;
  31. }
  32. if (height < minHeight){
  33. minHeight = height;
  34. }
  35. }
  36. }
  37. int size = map.length;
  38. final BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
  39. for (int x = 0; x < size; x++){
  40. for (int y = 0; y < size; y++){
  41. Color color = Color.getHSBColor((float)((map[x][y] - minHeight)/(maxHeight - minHeight)), 1, 1);
  42. image.setRGB(x,y,color.getRGB());
  43. }
  44. }
  45. JFrame frame = new JFrame("Picture");
  46. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47. frame.add(new JComponent(){
  48.  
  49. @Override
  50. public void paint(Graphics g){
  51. g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  52. }
  53.  
  54. });
  55. frame.setVisible(true);
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement