Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 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 de barba rija e bem cuidada, aceita ser meu padrinho?";
  21. letras = pedido.toCharArray();
  22.  
  23. for (char l : letras) {
  24.  
  25. if (isSpaceChar(l)) {
  26. map.put(l, new Color(238, 238, 238));
  27. } else {
  28. if (!map.containsKey(l)) {
  29. map.put(l, getRandomColor());
  30. }
  31. }
  32.  
  33. }
  34.  
  35. JFrame frame = new JFrame();
  36. frame.setSize(500, 500);
  37. frame.setVisible(true);
  38. JPanel panel = new JPanel() {
  39. public void paint(java.awt.Graphics g1) {
  40.  
  41. Graphics2D g = (Graphics2D) g1;
  42. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  43. RenderingHints.VALUE_ANTIALIAS_ON);
  44.  
  45. int x = 10;
  46. int y = 10;
  47.  
  48. for (char l : letras) {
  49.  
  50. Character c = (Character) l;
  51.  
  52. if (!isSpaceChar(l)) {
  53. g.setColor(Color.BLACK);
  54. g.fillOval(x - 1, y - 1, 42, 42);
  55. }
  56.  
  57. g.setColor(map.get(c));
  58. g.fillOval(x, y, 40, 40);
  59.  
  60. x += 50;
  61.  
  62. if (x > 450) {
  63. x = 10;
  64. y += 50;
  65. }
  66.  
  67. }
  68.  
  69. };
  70. };
  71. frame.setContentPane(panel);
  72. }
  73.  
  74. private static boolean isSpaceChar(char l){
  75. return (l+"").equals(" ") ? true : false;
  76. }
  77.  
  78. private static Color getRandomColor() {
  79. Random rng = new Random();
  80. return new Color(rng.nextInt(255), rng.nextInt(255), rng.nextInt(255));
  81. }
  82.  
  83. public static void main(String[] args) {
  84. init();
  85. }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement