如何使用鼠标事件拖动绘制线条?

时间:2016-02-26 23:56:34

标签: java swing canvas drawing mouseevent

好吧所以我添加了推荐的更改,但是它(draggedMouse)似乎仍然没有与画布连接,即使我认为我做得对。我想它没有贴在画布上,但是我不知道如何去做。我为自己的无能而提前道歉!我还包括了我的Line类

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

这是我的Line课程     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);

}


  }

1 个答案:

答案 0 :(得分:0)

您可以为Graphics添加PointListener变量和构造函数,例如

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

编辑:通过这种方式声明您的积分,您可以将它们(特别是ptStart)显示为mouseReleasedmouseDragged

这样您就可以更轻松地在mouseDragged中绘制线条了,因为您可以使用

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

然后,当您添加点侦听器时,您只需将代码更改为

//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);