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

标签 java swing mouseevent

我希望程序能这样工作,当你在面板上拖动鼠标时,应该显示形状,每次拖动鼠标时形状应该改变大小,最后应该显示的形状是松开鼠标时正在显示。 目前发生的情况是,在用鼠标拖动绘制时,线条是不可见的,只有在释放鼠标时才会出现

//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++;这就是松开鼠标后出现这条线的原因。

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

绘画因此看起来像这样:

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

关于java - 如何让用户在画线程序中拖动鼠标时看到形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55356152/

相关文章:

java - 在绘图顶部显示 JLabel 文本

javascript - 如果元素被 css 隐藏,则 MouseEvent 被吞下

java - 多个线程读取和导入目录中的文件

java - 反射访问最终静态变量而不进行初始化

java - 在 JList 中使用自定义 JPanel 组件

javascript - 如何将元素设置为默认状态(hide ())以及单击时(show())

java - 在 JtextPane 中更改鼠标指针

java - 如何在 Freemarker 模板中创建列表

java - 为什么我的 BufferedReader 无法正确读取我的文件?

java - 当显示 HTML 文本时,禁用时 JLabel 不会变灰