Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. package padrinho;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics2D;
  5. import java.awt.RenderingHints;
  6. import java.util.HashMap;
  7. import java.util.Random;
  8.  
  9. import javax.swing.JFrame;
  10. import javax.swing.JPanel;
  11.  
  12. public class Padrinho {
  13.  
  14. private static HashMap<Character, Color> map;
  15. private static String pedido;
  16. private static char[] letras;
  17.  
  18. private static void init() {
  19. map = new HashMap<Character, Color>();
  20. pedido = "senhor veterano, aceita ser meu padrinho?";
  21. letras = pedido.toCharArray();
  22.  
  23. for (char l : letras) {
  24.  
  25. if (!map.containsKey(l)) {
  26. map.put(l, getRandomColor());
  27. }
  28.  
  29. }
  30.  
  31. JFrame frame = new JFrame();
  32. frame.setSize(500, 500);
  33. frame.setVisible(true);
  34. JPanel j = new JPanel() {
  35. public void paint(java.awt.Graphics g1) {
  36.  
  37. Graphics2D g = (Graphics2D) g1;
  38. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  39. RenderingHints.VALUE_ANTIALIAS_ON);
  40.  
  41. int x = 10;
  42. int y = 10;
  43.  
  44.  
  45.  
  46. for (char l : letras) {
  47.  
  48. if (!(l+"").equals(" ")) {
  49. Character c = (Character) l;
  50.  
  51. g.setColor(Color.BLACK);
  52. g.fillOval(x - 1, y - 1, 42, 42);
  53. g.setColor(map.get(c));
  54. g.fillOval(x, y, 40, 40);
  55.  
  56. x += 50;
  57.  
  58. if (x > 450) {
  59. x = 10;
  60. y += 50;
  61. }
  62.  
  63. }
  64. }
  65.  
  66. };
  67. };
  68. frame.setContentPane(j);
  69. }
  70.  
  71. private static Color getRandomColor() {
  72. Random rng = new Random();
  73. return new Color(rng.nextInt(255), rng.nextInt(255), rng.nextInt(255));
  74. }
  75.  
  76. public static void main(String[] args) {
  77. init();
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement