[英]Error with drawing multiple lines on JPanel
I'm trying to draw multiple lines on a JPanel
. 我正在尝试在JPanel
上绘制多行。 My code adds each line to an ArrayList
and then a for loop is supposed to iterate through it to draw each line. 我的代码将每一行添加到ArrayList
,然后for循环应该遍历它以绘制每一行。 But I get this odd output instead. 但我得到了这个奇怪的输出。
This is my code: 这是我的代码:
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);
}
}
}
And here's what it draws on the panel when I move the cursor in a circle. 这是我将光标移动到圆圈时在面板上绘制的内容。 It's a bunch of lines connected at a center point. 这是一串连接在中心点的线。 But I want it to be able to draw multiple separate lines. 但我希望它能够绘制多个单独的行。 [ [
And this is the type of line I'd like to draw, but multiple of them without previous drawn lines disappearing. 这是我想绘制的线条类型,但是没有先前绘制线条的多条线条消失了。 Hence, why I implemented an ArrayList to redraw lines to keep them on the panel. 因此,为什么我实现了一个ArrayList来重绘线条以保持它们在面板上。
[ [
I think the reason the lines are disappearing is not shown in this code. 我认为线条消失的原因未在此代码中显示。 Here is how I would update it. 这是我如何更新它。
@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);
}
}
This will draw one line between the two points, and it will not store the line until the mouse is released. 这将在两个点之间绘制一条线,并且在释放鼠标之前不会存储该线。 If you actually want it to draw like a pencil, add the new line in the mouse dragged method too. 如果您确实希望它像铅笔一样绘制,也可以在鼠标拖动方法中添加新行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.