gpt4 book ai didi

java - 在 JPanel 上绘制多条线时出错

转载 作者:搜寻专家 更新时间:2023-11-01 03:34:04 24 4
gpt4 key购买 nike

我正在尝试在 JPanel 上绘制多条线。我的代码将每一行添加到一个 ArrayList 中,然后一个 for 循环应该遍历它以绘制每一行。但我却得到了这个奇怪的输出。

这是我的代码:

public class DrawPanel extends JPanel {

/** Generated serial ID for the program. */
private static final long serialVersionUID = 1697489704611349844L;

/** The width of the panel. */
private static final int WIDTH = 600;

/** The height of the panel. */
private static final int HEIGHT = 300;

/** The stroke width in pixels. */
private static final int STROKE_WIDTH = 1;

/** x-coordinate when mouse is first clicked. */
private int myX;

/** y-coordinate when mouse is first clicked. */
private int myY;

/** x-coordinate when mouse is clicked for a second time. */
private int myXEnd;

/** y-coordinate when mouse clicked for a second time. */
private int myYEnd;

/** ArrayList of lines drawn. */
private List<Line2D> myLines = new ArrayList<Line2D>();

/** ArrayList of coordinates to draw with a pencil. */
private List<MouseEvent> myPoints = new ArrayList<MouseEvent>();


/**
* Constructs a new ellipse panel.
*/
public DrawPanel() {
super();
setBackground(Color.WHITE);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
addMouseListener(myMouseHandler);
addMouseMotionListener(myMouseMotionHandler);
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}

/**
* MouseMotionListener for drawing a shape.
*/
private final MouseMotionListener myMouseMotionHandler = new MouseMotionAdapter() {

@Override
public void mouseDragged(final MouseEvent theEvent) {
myXEnd = theEvent.getX();
myYEnd = theEvent.getY();
myPoints.add(theEvent);
repaint();
}

// @Override
// public void mouseMoved(MouseEvent e) {
// }

};

/**
* MouseListener for drawing a shape.
*/
private final MouseListener myMouseHandler = new MouseAdapter() {
@Override
public void mousePressed(final MouseEvent theEvent) {
myX = theEvent.getX();
myY = theEvent.getY();
myXEnd = theEvent.getX();
myYEnd = theEvent.getY();
repaint();

}

@Override
public void mouseReleased(final MouseEvent theEvent) {
myXEnd = theEvent.getX();
myYEnd = theEvent.getY();

myPoints.add(theEvent);
repaint();
}
};

/**
* Draws line with drawLine method.
*/
@Override
public void paintComponent(final Graphics theGraphics) {
super.paintComponent(theGraphics);
final Graphics2D g2d = (Graphics2D) theGraphics;

// for better graphics display
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

g2d.setPaint(new Color(51, 0, 111));
g2d.setStroke(new BasicStroke(STROKE_WIDTH));

myLines.add(new Line2D.Double(myX, myY, myXEnd, myYEnd));

for (Line2D l : myLines) {
g2d.draw(l);
}
}
}

这是当我将光标移动一圈时它在面板上绘制的内容。它是在一个中心点连接的一串线。但我希望它能够绘制多条单独的线。[ 1]

这是我想要绘制的线条类型,但其中有多个线条没有消失。因此,为什么我实现了一个 ArrayList 来重绘线条以将它们保留在面板上。

[ 2]

最佳答案

我认为这段代码中没有显示线条消失的原因。以下是我将如何更新它。

   @Override
public void mouseDragged(final MouseEvent theEvent) {
myXEnd = theEvent.getX();
myYEnd = theEvent.getY();
repaint();
}

@Override
public void mousePressed(final MouseEvent theEvent) {
myX = theEvent.getX();
myY = theEvent.getY();
}

@Override
public void mouseReleased(final MouseEvent theEvent) {
myXEnd = theEvent.getX();
myYEnd = theEvent.getY();
myLines.add(new Line2D.Double(myX, myY, myXEnd, myYEnd));
repaint();
}
};

/**
* Draws line with drawLine method.
*/
@Override
public void paintComponent(final Graphics theGraphics) {
super.paintComponent(theGraphics);
final Graphics2D g2d = (Graphics2D) theGraphics;

// for better graphics display
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

g2d.setPaint(new Color(51, 0, 111));
g2d.setStroke(new BasicStroke(STROKE_WIDTH));

g2d.draw(new Line2D.Double(myX, myY, myXEnd, myYEnd));

for (Line2D l : myLines) {
g2d.draw(l);
}
}

这将在两点之间绘制一条线,并且在释放鼠标之前不会存储这条线。如果您确实希望它像铅笔一样绘制,也可以在鼠标拖动方法中添加新线。

关于java - 在 JPanel 上绘制多条线时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37196006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com