0

Okay so I added the recommended changes, however it (draggedMouse) still doesn't seem to be be connecting with the canvas even though I thought I am doing it right. I suppose it is not attached to the canvas, however I do not know how to go about doing this. I apologize in advance for my incompetence! I also included my Line class

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

public class WholePanel extends JPanel
{
private Color currentColor;
private CanvasPanel canvas;
private JPanel leftPanel;
private JButton undo,erase;
private ArrayList<Line> lineList;
private Point ptStart,ptEnd, ptDrag;
private JRadioButton black,red,blue,green,orange;
private ArrayList<Line> drag;




 public WholePanel()
 {

  currentColor = Color.black; 
  lineList = new ArrayList();
  drag = new ArrayList();

  undo = new JButton ("Undo"); // undo button
  erase = new JButton("Erase"); // Erase button
  black = new JRadioButton("Black"); black.setSelected(true); // setting   black to the default line color
  red = new JRadioButton("Red");
  blue = new JRadioButton("Blue");
  green = new JRadioButton("Green");
  orange = new JRadioButton("Orange");

  ButtonGroup group = new ButtonGroup(); // added buttons to group so only one can be selected at a time
  group.add(black);
  group.add(red);
  group.add(blue);
  group.add(green);
  group.add(orange);

  leftPanel = new JPanel(); // creates new JPanel that I can use to set the    grid layout in and add the radio buttons
  leftPanel.setLayout(new GridLayout(7,1));
  leftPanel.add(black);
  leftPanel.add(red);
  leftPanel.add(blue);
  leftPanel.add(green);
  leftPanel.add(orange);

  leftPanel.add(undo); // adds the undo button to the left panel above the   erase button
  leftPanel.add(erase); // adds the erase button to the left panel at the  bottom


  canvas = new CanvasPanel(); // creates the canvas panel



  JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel,   canvas); // splits the applet layout into two panels
  setLayout(new BorderLayout());
  add(sp);

  undo.addActionListener( new ButtonListener()); // adding listener action  for undo and erase buttons
  erase.addActionListener( new ButtonListener());

  black.addActionListener( new ComboListener()); // adding listener actions for radio buttons
  red.addActionListener( new ComboListener());
  blue.addActionListener( new ComboListener());
  green.addActionListener( new ComboListener());
  orange.addActionListener( new ComboListener());

  //canvas.addMouseListener(new PointListener()); 
  //canvas.addMouseMotionListener(new PointListener()); 

  PointListener pl = new PointListener(canvas.getGraphics());
  canvas.addMouseListener(pl);
  canvas.addMouseMotionListener(pl);
   }

  //CanvasPanel is the panel where shapes will be drawn
   private class CanvasPanel extends JPanel
  {
  //this method draws all shapes specified by a user
  public void paintComponent(Graphics page)
   {
    super.paintComponent(page);
    setBackground(Color.WHITE);


    for(int i = 0; i< lineList.size(); i++){
        (lineList.get(i)).draw(page);
        }
   }
     } //end of CanvasPanel class


  //ButtonListener defined actions to take in case
  //"Undo", or "Erase" is chosen.
  private class ButtonListener implements ActionListener
   {
  public void actionPerformed (ActionEvent event)
  {
      JButton source = (JButton)event.getSource();
      String name = source.getText();
      if (name.equals("Undo"))
      {
          if(lineList.size() > 0)
          {
              lineList.remove(lineList.size()-1);  
          }

      }
      else if (name.equals("Erase"))
      {
          lineList.clear();
      }

      repaint();

  }
 } // end of ButtonListener


    // listener class to set the color chosen by a user using
   // the color radio buttons
  private class ComboListener implements ActionListener
   {
    public void actionPerformed(ActionEvent event)
     {

        JRadioButton src = (JRadioButton)event.getSource();
        String name = src.getText();
        if(name.equals("Black"))
        {
            currentColor = Color.BLACK;
        }
        else if (name.equals("Red"))
        {
            currentColor = Color.RED;
        }
        else if (name.equals("Blue"))
        {
            currentColor = Color.BLUE;
        }
        else if (name.equals("Green"))
        {
            currentColor = Color.GREEN;
        }
        else if (name.equals("Orange"))
        {
            currentColor = Color.ORANGE;
        }

     }
   } 


  // listener class that listens to the mouse

 public class PointListener implements MouseListener, MouseMotionListener {

    Graphics g;

    public PointListener(Graphics g){
        this.g = g;
    }

//{
 //in case that a user presses using a mouse,
 //record the point where it was pressed.
 public void mousePressed (MouseEvent event)
  {
      ptStart = event.getPoint();
  }

 //mouseReleased method takes the point where a mouse is released,
 //using the point and the pressed point to create a line,
 //add it to the ArrayList "lineList", and call paintComponent method.
 public void mouseReleased (MouseEvent event)
  {
       ptEnd = event.getPoint();
       Line line = new   Line(ptStart.x,ptStart.y,ptEnd.x,ptEnd.y,currentColor);
       lineList.add(line);
       repaint();

  }
 public void mouseClicked (MouseEvent event) {}
 public void mouseEntered (MouseEvent event) {}
 public void mouseExited (MouseEvent event) {}

 //mouseDragged method takes the point where a mouse is dragged
 //and call paintComponent method

 public void mouseDragged(MouseEvent event)
    {

      ptDrag = event.getPoint();
      Line dragLine = new   Line(ptStart.x,ptStart.y,ptDrag.x,ptDrag.y,currentColor);
      dragLine.draw(g);
      repaint();
    }

 public void mouseMoved(MouseEvent event) {}


} // end of PointListener

 } // end of Whole Panel Class

And this is my Line class import java.awt.*;

public class Line {
    private int x1,x2,y1,y2;
    private Color color;

   public Line(int px1, int py1, int px2, int py2, Color pColor) //    constructor that sets the color of the line as well as the coordinates
{
    x1 = px1;
    y1 = py1;
    x2 = px2;
    y2 = py2;
    color = pColor;

}

public void draw(Graphics page)
{
    page.setColor(color);// insert user color
    page.drawLine(x1, y1, x2, y2);

}


  }
  • I should think the `mouseDragged` method would be nearly identical to `mouseReleased`, minus the part where you add it to the list. – Calvin P. Feb 27 '16 at 00:01
  • For [example](http://stackoverflow.com/questions/24374970/why-is-my-line-not-drawing/24376444#24376444)? – MadProgrammer Feb 27 '16 at 01:05
  • @CalvinP. So I was able to get the actual paint working, however the mouseDragged method is what I am stuck on. I created a Line object (where my draw method is) however I dont know how to send it to the canvas... – Zach Lamonte Lee Feb 27 '16 at 01:16
  • Check out [Custom Painting Approaches](https://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/) for a working example. The example draws Rectangles but the concept would be the same for a line. – camickr Feb 28 '16 at 19:22

1 Answers1

0

You could add a Graphics variable and a constructor for PointListener such as

public class PointListener implements MouseListener, MouseMotionListener {
    Graphics g;
    //declare point variables
    Point ptStart, ptEnd, ptDrag;
    public PointListener(Graphics g){
        this.g = g;
    }
}

Edit: by declaring your Points this way, you make them (ptStart in particular) visible to mouseReleased and mouseDragged

This would enable you to draw your line in mouseDragged more easily because you can use

g.drawLine(ptStart.x, ptStart.y, ptDrag.x, ptDrag.y, currentColor);

Then when you add point listener, you just need to change the code to

//added PointListener object because you previously
//created new object for each statement
//meaning separate objects are listening for Mouse and MouseMotion
PointListener pl = new PointListener(canvas.getGraphics());
//this should work but one way or another
//you need to pass the Graphics object
canvas.addMouseListener(pl);
canvas.addMouseMotionListener(pl);
Calvin P.
  • 1,116
  • 2
  • 8
  • 13
  • I made the edits you recommended, although once again, I am brand new to this and messed it up in a royal way. Perhaps you could look at it and tell me where I went wrong? I really appreciate your help. – Zach Lamonte Lee Feb 27 '16 at 03:32
  • I suspect any issues you're having are due to variables being out of scope. You need to declare Point variables at the start of the class so they can be utilized by the other action methods. I will update my answer to show what I mean – Calvin P. Feb 27 '16 at 03:46