Advertisement
Crenox

Winston Tutorial 17 Random Coloring Panel

Jul 27th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. package com.samkough.main;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. @SuppressWarnings("serial")
  8. public class Win extends JFrame implements MouseListener
  9. {
  10. private static final int WIDTH = 300;
  11. private static final int HEIGHT = 300;
  12.  
  13. JPanel p1;
  14.  
  15. public Win()
  16. {
  17. p1 = new JPanel();
  18. p1.setBackground(randomColor());
  19. add(p1);
  20. p1.addMouseListener(this);
  21. }
  22.  
  23. // this lets us get a random color
  24. public Color randomColor()
  25. {
  26. int r = (int)(Math.random()*256);
  27. int g = (int)(Math.random()*256);
  28. int b = (int)(Math.random()*256);
  29.  
  30. return(new Color(r,g,b));
  31. }
  32.  
  33. public void mouseClicked(MouseEvent e)
  34. {
  35. p1.setBackground(randomColor());
  36. }
  37.  
  38. public void mouseEntered(MouseEvent e)
  39. {
  40.  
  41. }
  42.  
  43. public void mouseExited(MouseEvent e)
  44. {
  45.  
  46. }
  47.  
  48. public void mousePressed(MouseEvent e)
  49. {
  50.  
  51. }
  52.  
  53. public void mouseReleased(MouseEvent e)
  54. {
  55.  
  56. }
  57.  
  58.  
  59. public static void main(String args[])
  60. {
  61. Win frame = new Win();
  62.  
  63. frame.setVisible(true);
  64. frame.setSize(WIDTH, HEIGHT);
  65. // frame.pack();
  66. frame.setTitle("Random Coloring Panel");
  67. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  68. frame.setLocationRelativeTo(null);
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement