繁体   English   中英

如何使用鼠标事件拖动来画线?

[英]How do I use mouse event dragging to draw line?

好的,所以我添加了建议的更改,但是,即使我认为我做得对,它(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);

}


  }

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

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

编辑:通过这样声明您的Point,可以使它们(尤其是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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM