0

I am trying to create a simple drawing application for my assignment where the user can select one of three colors to draw in. To activate the "pen", the user needs to click on the window, then drag the mouse around (NOT pressed down) to draw lines on the screen. I'm having a hard time with a few things. One of them is when changing the colors, I'd like to change the color from that point on (after the user selects a different color) instead of all of the previous points (drawing) the user made. I'm also having a hard time implementing the mouse listeners correctly. I'm not sure how to activate the mouseMoved function after the user clicks on the screen (mousePressed?) and deactivate mouseMoved when the user clicks a second time. Here is what I have so far:

package project.pkg6;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class DrawingTester extends JPanel{
    //linked list holds points
    private LinkedList<Point> pointList;
    //default color
    private Color selectedColor = Color.red;
    //colors
    private final Color red;
    private final Color blue;
    private final Color yellow;
    //eraser
    private final Color white;
    //radio buttons
    private final JRadioButton redButton;
    private final JRadioButton blueButton;
    private final JRadioButton yellowButton;
    private final JRadioButton eraserButton;
    //group radio buttons
    private final ButtonGroup colorRadioGroup;
    //size of dot 
    private final static int DIAMETER = 10;

    public DrawingTester(){
        setLayout(new FlowLayout());
        setBackground(Color.WHITE);
        pointList = new LinkedList<Point>();

        this.addMouseMotionListener(new MouseDrawListener());
        this.addMouseListener(new MouseDrawListener());

        //create radio buttons
        redButton = new JRadioButton("Red", true);
        blueButton = new JRadioButton("Blue", false);
        yellowButton = new JRadioButton("Yellow", false);
        eraserButton = new JRadioButton("Eraser", false);
        add(redButton);
        add(blueButton);
        add(yellowButton);
        add(eraserButton);

        //add radio buttons to group
        colorRadioGroup = new ButtonGroup();
        colorRadioGroup.add(redButton);
        colorRadioGroup.add(blueButton);
        colorRadioGroup.add(yellowButton);
        colorRadioGroup.add(eraserButton);

        //create color objects
        red = Color.RED;
        blue = Color.BLUE;
        yellow = Color.YELLOW;
        white = Color.WHITE;

        //register events
        redButton.addItemListener(new RadioButtonHandler(red));
        blueButton.addItemListener(new RadioButtonHandler(blue));
        yellowButton.addItemListener(new RadioButtonHandler(yellow));
        eraserButton.addItemListener(new RadioButtonHandler(white));
    }

    @Override
    public void paintComponent(Graphics pen){
        super.paintComponent(pen);

        //draw points
        for(Point point : pointList){
            int x = (int) point.getX();
            int y = (int) point.getY();
            pen.setColor(selectedColor);
            pen.fillOval(x, y, DIAMETER, DIAMETER);

        }
    }
    //event handlers
    private class RadioButtonHandler implements ItemListener{
        private Color color;

        public RadioButtonHandler(Color c){
            color = c;
        }

        @Override
        public void itemStateChanged(ItemEvent e){
            if (e.getSource() == redButton){
                selectedColor = Color.RED;
            }
            else if(e.getSource() == blueButton){
                selectedColor = Color.BLUE;

            }
            else if(e.getSource() == yellowButton){
                selectedColor = Color.YELLOW;

            }
            else if (e.getSource() == eraserButton){
                selectedColor = Color.WHITE;
            }
        }
    }
    private class MouseDrawListener extends MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e){
            //repaint();
            draw(e);
        }
        @Override
        public void mouseMoved(MouseEvent e){
            /*Point point = e.getPoint();

            pointList.add(point);
            repaint();
           draw(e);*/
        }
        void draw(MouseEvent e){
            Point point = e.getPoint();

            pointList.add(point);
            repaint();
        }
    }

    public static void main(String[] args){
       EventQueue.invokeLater(new Runnable(){
           public void run(){
               JFrame drawPanel = new JFrame("Drawing");
               drawPanel.setSize(400,400);
               DrawingTester drawing = new DrawingTester();
               drawPanel.getContentPane().add(drawing);
               drawPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               drawPanel.setVisible(true);
           }
       });
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 2
    *"To activate the "pen", the user needs to click on the window, then drag the mouse around (NOT pressed down) to draw lines on the screen"* - You need to investigate the `mosueDragged` method of the `MouseMotionListener` - Have a look at [How to write a mouse motion listener](https://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html) for more details – MadProgrammer Apr 30 '17 at 23:47
  • 2
    *"I'd like to change the color from that point"* - You need to store color and point information together in your `pointList` - this is going to require a custom object with `Point` and `Color` properties – MadProgrammer Apr 30 '17 at 23:48
  • I have an example sort of like this [here](http://stackoverflow.com/a/21322873/2891664). I'm drawing directly to an image there rather than adding points to a list but it shows an example of using a MouseMotionListener like MadProgrammer is talking about. – Radiodef Apr 30 '17 at 23:59

0 Answers0