编程语言
首页 > 编程语言> > java-如何在线描程序中拖动鼠标时使用户看到形状?

java-如何在线描程序中拖动鼠标时使用户看到形状?

作者:互联网

我希望程序能够正常工作,以便在面板上拖动鼠标时,形状应该显示出来.每次拖动鼠标时,形状都应该改变大小,最后应该显示的形状就是所显示的形状此刻释放了鼠标.
当前发生的情况是,用鼠标拖动绘制时该线不可见,仅在释放鼠标时才会显示

//DrawPanel
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JFrame;

public class DrawLine extends JPanel
{
  private LineClass lines[];
  private int lineCount;
  private LineClass currentLine;
  public JLabel statusLabel;
  private int currShapeX1,currShapeY1;

  public DrawLine()
  {
statusLabel = new JLabel("(0,0)");
lines = new LineClass[100];
lineCount = 0;
currentLine = null;

MouseHandler handler = new MouseHandler();
addMouseListener(handler);
addMouseMotionListener(handler);


  }


  public void paintComponent(Graphics g)
  {
super.paintComponent(g);

for(int count = 0; count < lineCount; ++count)
{
  lines[count].draw(g);
} 

  }

  public static void main(String args[])
  {
JFrame frame = new JFrame();
DrawLine panel = new DrawLine();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setSize(400,400);
frame.setVisible(true);
  }

  private class MouseHandler extends MouseAdapter implements   MouseMotionListener
  {
    public void mousePressed(MouseEvent event)
{
  //it assigns currentShape a new shape and initializes both points to the mouse position.
  currShapeX1 = event.getX();
  currShapeY1 = event.getY();           
}
public void mouseReleased(MouseEvent event)
{
  //finish drawing the current shape and place it in the array
  //Set the second point of currentShape to the current mouse position
    currentLine = new LineClass(currShapeX1,currShapeY1,event.getX(),event.getY());

  // and add currentShape to the array.
  //Instance variable shapeCount determines the insertion index. Set     currentShape to null and call method repaint to update the drawing with the new shape.
  lines[lineCount] = currentLine;
  lineCount++;

  currentLine = null;
  repaint();
}
public void mouseDragged(MouseEvent event)
{
  //currently not working
  /*What is desired:
   * As you drag the mouse across the panel, the shape should be showing 
   * The shape should change in size each time you drag the mouse
   * Only one shape should be shown as the mouse is being dragged
   * The shape that should be displayed finally is that which was being displayed at the moment the mouse was released
   * */
  //it sets the second point of the currentShape to the current mouse position and calls method repaint

  //finish drawing the current shape and place it in the array
  //Set the second point of currentShape to the current mouse position
    currentLine = new LineClass(currShapeX1,currShapeY1,event.getX(),event.getY());

  // and add currentShape to the array.
  //Instance variable shapeCount determines the insertion index. Set currentShape to null and call method repaint to update the drawing with the new shape.
  lines[lineCount] = currentLine;

  currentLine = null;
  repaint();
  statusLabel.setText(String.format("(%d,%d)",event.getX(),event.getY()));
}

public void mouseMoved(MouseEvent event)
{
  //to set the text of the statusLabel so that it displays the mouse coordinates—this will update the label with the coordinates every time the user moves 
  //(but does not drag) the mouse within the DrawPanel
  statusLabel.setText(String.format("(%d,%d)",event.getX(),event.getY()));
}
}
}

//LineClass
class LineClass
{
private int x1;
private int y1;
private int x2;
private int y2;

public LineClass(int x1, int y1, int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}

public void draw(Graphics g)
{
g.drawLine(x1,y1,x2,y2);
}
}

解决方法:

您的问题似乎是您没有绘制要拖动的最后一行.

在mouseDragged()中,您可以这样:

currentLine = new LineClass(currShapeX1,currShapeY1,event.getX(),event.getY());  
lines[lineCount] = currentLine;
currentLine = null;

这会将索引为lineCount的行设置为新行.

然后在渲染时执行以下操作:

for(int count = 0; count < lineCount; ++count)
{
  lines[count].draw(g);
}

您正在绘制除索引lineCount中的那条线以外的所有线.

在mouseReleased()中,您可以得到lineCount;这就是释放鼠标后该行显示的原因.

为了解决这个问题,我不会在拖动时将当前拖动的线添加到行中.而是只需在mouseDragged中对其进行更新.然后在mouseReleased中将其添加到数组,并将currentLine设置为null.

因此绘画将如下所示:

for(int count = 0; count < lineCount; ++count) {
  lines[count].draw(g);
}

if( currentLine != null ) {
  //you could set different rendering options here, e.g. a different color
  currentLine.draw(g); 
}

最后,代替使用数组,最好使用List< LineClass>.这样,您不必跟踪当前的行数,不必限于100行,也不必自己调整数组的大小.

由于列表将仅包含非空行,因此渲染可能如下所示:

lines.forEach( line -> line.draw(g) );

if( currentLine != null ) {
  currentLine.draw(g);
}

标签:swing,mouseevent,java
来源: https://codeday.me/bug/20191211/2105505.html