Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.ArrayList;
  6.  
  7. public class ChoiseColor extends Component {
  8. protected ActionListener actionListener;
  9. protected Color color = Color.white;
  10. protected boolean brush = false;
  11. protected ArrayList<Info> xy = new ArrayList<>();
  12.  
  13. public ChoiseColor() {
  14. enableEvents(AWTEvent.MOUSE_EVENT_MASK);
  15. enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
  16. }
  17.  
  18. public void paint(Graphics g) {
  19. int width = getSize().width - 1;
  20. int height = getSize().height - 1;
  21. g.setColor(Color.black);
  22. g.fillRect(0,0,width-1,height-1);
  23. for(Info info : xy){
  24. g.setColor(info.getColor());
  25. if(!brush) {
  26. g.fillOval(info.getX() - 5, info.getY() - 5, 10, 10);
  27. }else{
  28. g.fillRect(info.getX() - 5, info.getY() - 5, 10, 10);
  29. }
  30. }
  31. }
  32.  
  33. public void clear(){
  34. xy.clear();
  35. repaint();
  36. }
  37.  
  38. @Override
  39. public Dimension getPreferredSize() {
  40. return new Dimension(300 , 300);
  41. }
  42.  
  43. public Dimension getMinimumSize() {
  44. return getPreferredSize();
  45. }
  46.  
  47. public void setColor(Color color) {
  48. this.color = color;
  49. }
  50. public void setBrush(){
  51. brush = !brush;
  52. }
  53.  
  54. @Override
  55. protected void processMouseMotionEvent(MouseEvent e) {
  56. switch (e.getID()) {
  57. case MouseEvent.MOUSE_DRAGGED:
  58. xy.add(new Info(e.getX() , e.getY() , color));
  59. repaint();
  60. break;
  61. }
  62. super.processMouseMotionEvent(e);
  63. }
  64.  
  65. public void addActionListener(ActionListener listener) {
  66. actionListener = AWTEventMulticaster.add(actionListener, listener);
  67. enableEvents(AWTEvent.MOUSE_EVENT_MASK);
  68. enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
  69. }
  70.  
  71. public void removeActionListener(ActionListener listener) {
  72. actionListener = AWTEventMulticaster.remove(actionListener, listener);
  73. }
  74.  
  75. class Info{
  76. private Color color;
  77. private int x;
  78. private int y;
  79. Info(int x , int y , Color color){
  80. this.x = x;
  81. this.y = y;
  82. this.color = color;
  83. }
  84.  
  85. public int getX() {
  86. return x;
  87. }
  88.  
  89. public int getY() {
  90. return y;
  91. }
  92.  
  93. public Color getColor() {
  94. return color;
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement