Guest User

Untitled

a guest
Apr 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. class MouseClickListener implements MouseListener
  2. {
  3. public void mouseClicked(MouseEvent event)
  4. {
  5. int x = event.getX();
  6. int y = event.getY();
  7. System.out.println(x + " " + y);
  8. drawingPanel.drawPoints(x, y);
  9.  
  10. }
  11. // Do­nothing methods
  12. public void mouseReleased(MouseEvent event) {}
  13. public void mousePressed(MouseEvent event) {}
  14. public void mouseEntered(MouseEvent event) {}
  15. public void mouseExited(MouseEvent event) {}
  16. }
  17. class ButtonListener implements ActionListener
  18. {
  19. public void actionPerformed(ActionEvent event)
  20. {
  21.  
  22. if (event.getSource()== changeColorButton){
  23. int r1 = Integer.parseInt(radiusField1.getText());
  24. int r2 = Integer.parseInt(radiusField2.getText());
  25. drawingPanel.changeColor();
  26. drawingPanel.changeRadius(r1, r2);
  27. }
  28. if (event.getSource()== clearButton){
  29. radiusField1.setText("10");
  30. radiusField2.setText("10");
  31. errorDisplay.setText("");
  32. drawingPanel.clearAll();
  33. }
  34.  
  35. }
  36. }
  37. MouseListener listener1 = new MouseClickListener();
  38. drawingPanel.addMouseListener(listener1);
  39.  
  40. ActionListener listener = new ButtonListener();
  41. changeColorButton.addActionListener(listener);
  42. radiusField1.addActionListener(listener);
  43. radiusField2.addActionListener(listener);
  44. clearButton.addActionListener(listener);
  45. frame.setVisible(true);
  46. }
  47. }
  48.  
  49. public class PanelDraw extends JPanel{
  50. /*******Use the ArrayList for storing all the points drawn on the
  51. screen******/
  52. ArrayList<Point> pointArray = new ArrayList<Point>();
  53.  
  54. ArrayList<Color> colorArray = new ArrayList<Color>();
  55. ArrayList<Point> radiusArray = new ArrayList<Point>();
  56. public PanelDraw(){
  57.  
  58. }
  59. //this method automatically gets called when an object is created from
  60. this class
  61. //this method also gets called whenever the repaint() method is called
  62. from another method
  63. @Override
  64. public void paintComponent(Graphics g) {
  65.  
  66. super.paintComponent(g);
  67. Graphics2D myDrawing = (Graphics2D) g;
  68.  
  69. /*****Write a loop here to draw all the points in the ArrayList******/
  70. for (int i = 0; i < pointArray.size(); i++) {
  71. myDrawing.setColor(colorArray.get(i));
  72. myDrawing.fillOval(pointArray.get(i).x,pointArray.get(i).y,10,10);
  73.  
  74. }
  75. }
  76. public void drawPoints(int mouseX, int mouseY){
  77. /******Write code here to add a new Point to the ArrayList*******/
  78. //create Point where mouse was clicked
  79. Point p = new Point(mouseX,mouseY);
  80.  
  81. //add the Point to the ArrayList
  82. pointArray.add(p);
  83. //System.out.println(this.getForeground());
  84. //add the current color to the ArrayList
  85. colorArray.add(this.getForeground());
  86.  
  87. repaint();
  88. }
  89.  
  90. public void changeColor(){
  91. //change drawing color of the panel color every time this method is
  92. called
  93. int red = (int) (Math.random() * 256);
  94. int green = (int) (Math.random() * 256);
  95. int blue = (int) (Math.random() * 256);
  96.  
  97. this.setForeground(new Color(red,green,blue));
  98. }
  99. public void changeRadius(int r1, int r2){
  100. Point r = new Point(r1,r1);
  101. radiusArray.add(r);
  102. }
  103. public void clearAll(){
  104. pointArray.clear();
  105. colorArray.clear();
  106. radiusArray.clear();
  107. repaint();
  108. }
  109. }
Add Comment
Please, Sign In to add comment