Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MouseClickListener implements MouseListener
- {
- public void mouseClicked(MouseEvent event)
- {
- int x = event.getX();
- int y = event.getY();
- System.out.println(x + " " + y);
- drawingPanel.drawPoints(x, y);
- }
- // Donothing methods
- public void mouseReleased(MouseEvent event) {}
- public void mousePressed(MouseEvent event) {}
- public void mouseEntered(MouseEvent event) {}
- public void mouseExited(MouseEvent event) {}
- }
- class ButtonListener implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- if (event.getSource()== changeColorButton){
- int r1 = Integer.parseInt(radiusField1.getText());
- int r2 = Integer.parseInt(radiusField2.getText());
- drawingPanel.changeColor();
- drawingPanel.changeRadius(r1, r2);
- }
- if (event.getSource()== clearButton){
- radiusField1.setText("10");
- radiusField2.setText("10");
- errorDisplay.setText("");
- drawingPanel.clearAll();
- }
- }
- }
- MouseListener listener1 = new MouseClickListener();
- drawingPanel.addMouseListener(listener1);
- ActionListener listener = new ButtonListener();
- changeColorButton.addActionListener(listener);
- radiusField1.addActionListener(listener);
- radiusField2.addActionListener(listener);
- clearButton.addActionListener(listener);
- frame.setVisible(true);
- }
- }
- public class PanelDraw extends JPanel{
- /*******Use the ArrayList for storing all the points drawn on the
- screen******/
- ArrayList<Point> pointArray = new ArrayList<Point>();
- ArrayList<Color> colorArray = new ArrayList<Color>();
- ArrayList<Point> radiusArray = new ArrayList<Point>();
- public PanelDraw(){
- }
- //this method automatically gets called when an object is created from
- this class
- //this method also gets called whenever the repaint() method is called
- from another method
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D myDrawing = (Graphics2D) g;
- /*****Write a loop here to draw all the points in the ArrayList******/
- for (int i = 0; i < pointArray.size(); i++) {
- myDrawing.setColor(colorArray.get(i));
- myDrawing.fillOval(pointArray.get(i).x,pointArray.get(i).y,10,10);
- }
- }
- public void drawPoints(int mouseX, int mouseY){
- /******Write code here to add a new Point to the ArrayList*******/
- //create Point where mouse was clicked
- Point p = new Point(mouseX,mouseY);
- //add the Point to the ArrayList
- pointArray.add(p);
- //System.out.println(this.getForeground());
- //add the current color to the ArrayList
- colorArray.add(this.getForeground());
- repaint();
- }
- public void changeColor(){
- //change drawing color of the panel color every time this method is
- called
- int red = (int) (Math.random() * 256);
- int green = (int) (Math.random() * 256);
- int blue = (int) (Math.random() * 256);
- this.setForeground(new Color(red,green,blue));
- }
- public void changeRadius(int r1, int r2){
- Point r = new Point(r1,r1);
- radiusArray.add(r);
- }
- public void clearAll(){
- pointArray.clear();
- colorArray.clear();
- radiusArray.clear();
- repaint();
- }
- }
Add Comment
Please, Sign In to add comment