Advertisement
kajacx

Circle Painter

Jun 22nd, 2014
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. package so;
  2.  
  3. import java.awt.*;
  4. import java.util.Random;
  5. import java.util.function.*;
  6. import javax.swing.*;
  7.  
  8. public class CirclePainter {
  9.  
  10.     public static void main(String[] args) {
  11.         JFrame frame = new JFrame("Circle Painter");
  12.         frame.setLayout(new FlowLayout());
  13.        
  14.         //java 8 style
  15.         Function<Integer, int[]> fiftyShadesOfGray = (i -> new int[]{4*i,4*i,4*i});
  16.        
  17.         //pre-java 8 style
  18.         DiameterToColor anotherFunc = new DiameterToColor() {
  19.             @Override
  20.             public int[] getColor(int i) {
  21.                 return new int[]{20+4*i,255-4*i,100+2*i};
  22.             }
  23.         };
  24.        
  25.         JPanel gray = new MyPanel(fiftyShadesOfGray);
  26.         JPanel colorful = new MyPanel(anotherFunc);
  27.        
  28.         frame.add(gray);
  29.         frame.add(colorful);
  30.        
  31.         frame.pack();
  32.         frame.setLocationRelativeTo(null);
  33.        
  34.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.         frame.setVisible(true);
  36.     }
  37.    
  38.     private static class MyPanel extends JPanel {
  39.         //better than magic constants hard-coded deep inside method
  40.         private static final int X = 0, Y = 1, DIAM = 2,
  41.                 COLOR_R = 3, COLOR_G = 4, COLOR_B = 5;
  42.        
  43.  
  44.         private int width = 500, height = 500;
  45.         private Dimension prefSize = new Dimension(width, height);
  46.  
  47.         /**
  48.          * array of information about circles, in format
  49.          * x|y|diameter|colorR|colorG|colorG
  50.          */
  51.         private int[][] circles;
  52.  
  53.         private int maxDiameter = 50;
  54.         private int count = 100;
  55.  
  56.         //use this if you have java 8
  57.         public MyPanel(Function<? super Integer, ? extends int[]> diamToColor) {
  58.             //optional: seed your random generator for repeatable testing
  59.             long seed = 6194466113533747327L;
  60.             Random ran = new Random(seed);
  61.  
  62.             circles = new int[count][];
  63.             for (int i = 0; i < count; i++) {
  64.                 int x = ran.nextInt(width);
  65.                 int y = ran.nextInt(height);
  66.                 int d = ran.nextInt(maxDiameter);
  67.                 int[] col = diamToColor.apply(d);
  68.                 circles[i] = new int[]{x, y, d, col[0], col[1], col[2]};
  69.             }
  70.         }
  71.  
  72.         //move body of previous constructor here
  73.         //if you don't have java 8
  74.         public MyPanel(DiameterToColor diamToColor) {
  75.             this((Function<Integer, int[]>) diamToColor::getColor);
  76.         }
  77.  
  78.         @Override
  79.         public Dimension getPreferredSize() {
  80.             return prefSize;
  81.         }
  82.  
  83.         @Override
  84.         protected void paintComponent(Graphics g) {
  85.             super.paintComponent(g);
  86.             for(int[] circle: circles) {
  87.                 g.setColor(new Color(circle[COLOR_R], circle[COLOR_G], circle[COLOR_B]));
  88.                 g.fillOval(circle[X], circle[Y], circle[DIAM], circle[DIAM]);
  89.             }
  90.         }
  91.  
  92.     }
  93.  
  94.     //use java 8 Function instead if avaliable
  95.     private static interface DiameterToColor {
  96.  
  97.         public int[] getColor(int diameter);
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement