当绘制另一条线时,我绘制的线将被删除 [英] The lines that I draw get removed when another line is drawn
问题描述
当绘制另一条线时,我绘制的线将被删除.我正在使用鼠标事件绘制线条,但是每当我绘制第二条线条时,第一条线条就会被删除.我认为这与我的点有关,这些点会不断更改我想画线的坐标的位置,因为单击会不断更改点,但是我不确定这就是原因.请提前帮助谢谢
The lines that I draw get removed when another line is drawn. I am using mouse event to draw lines but when ever i draw the second line the first one get removed. I believe it has something to do with my points which keep changing the location of the coordinates where i want to draw the line as my click keep changing the points but i am not sure that's the reason; please help thanks in advance
public class DrawOnComponent
{
public static void main(String args[]) throws Exception {
JFrame f = new JFrame("Draw a Red Line");
f.setSize(300, 300);
f.setLocation(300, 300);
f.setResizable(false);
JPanel p = new JPanel() {
Point pointStart = null;
Point pointEnd = null;
{
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
pointStart = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
pointStart = null;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
pointEnd = e.getPoint();
}
public void mouseDragged(MouseEvent e) {
pointEnd = e.getPoint();
repaint();
}
});
}
public void paint(Graphics g) {
super.paint(g);
if (pointStart != null) {
g.setColor(Color.RED);
g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
}
}
};
f.add(p);
f.setVisible(true);
} }
推荐答案
如果您要保留线条,则需要绘制所有需要查看的线条.通常可以通过以下两种方式之一来完成此操作:
If you want lines to persist, then you need to draw all the lines that need to be seen. This is often done in one of two ways:
- 创建一个包含某些行数据的ArrayList,例如
ArrayList<Line2D>
,在要添加新行时将其添加到其中,然后在paintComponent中,遍历列表以绘制每行. - 或将每行绘制到一个BufferedImage,然后使用
Graphics#drawImage(...)
在paintComponent中绘制BufferedImage.
- Create an ArrayList of something to hold line data, such as an
ArrayList<Line2D>
, add to it when you want to add a new line, and then in paintComponent, iterate through the list drawing each line. - Or drawing each line to a BufferedImage, and drawing the BufferedImage in paintComponent using
Graphics#drawImage(...)
.
例如-使用BufferedImage,我们将从BufferedImage获取Graphics对象,使用该对象绘制图像,处理该对象,然后调用repaint()
要求GUI重新绘制自身. JPanel的paintComponent方法将绘制图像:
For example -- using a BufferedImage, we'd get the Graphics object from the BufferedImage, draw onto the image using it, dispose of the object, and then call repaint()
to ask the GUI to repaint itself. The JPanel's paintComponent method would draw the image:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;
@SuppressWarnings("serial")
public class DrawLines extends JPanel {
// size of things
private static final int BI_W = 600;
private static final int BI_H = BI_W;
private static final Color BACKGROUND_COLOR = Color.WHITE;
// properties of the temporary and permanent lines
private static final Color LINE_COLOR = new Color(200, 200, 255);
public static final Color SAVED_LINES_COLOR = Color.BLUE;
public static final Stroke SAVED_LINES_STROKE = new BasicStroke(5f);
// image to hold permanent lines
private BufferedImage img = new BufferedImage(BI_W, BI_H, BufferedImage.TYPE_INT_ARGB);
// of true, draw temporary line
private boolean drawTempLine = false;
private int x1 = 0;
private int y1 = 0;
private int x2 = 0;
private int y2 = 0;
public DrawLines() {
setBackground(BACKGROUND_COLOR);
MyMouse myMouse = new MyMouse();
addMouseListener(myMouse);
addMouseMotionListener(myMouse);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
if (drawTempLine) {
g.setColor(LINE_COLOR);
g.drawLine(x1, y1, x2, y2);
}
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
int w = img.getWidth();
int h = img.getHeight();
return new Dimension(w, h);
}
private class MyMouse extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
}
@Override
public void mouseDragged(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
drawTempLine = true;
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
// draw to buffered image with its graphics object
Graphics2D g2 = img.createGraphics();
// for smoother drawing
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// set the color and thickness of the saved line
g2.setColor(SAVED_LINES_COLOR);
g2.setStroke(SAVED_LINES_STROKE);
// draw the saved line
g2.drawLine(x1, y1, x2, y2);
// we're done with this Graphics object -- dispose of it
g2.dispose();
// tell gui not to draw the temporary drawing line
drawTempLine = false;
// ask GUI to repaint itself
repaint();
}
}
private static void createAndShowGui() {
DrawLines mainPanel = new DrawLines();
JFrame frame = new JFrame("Draw Lines");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
这篇关于当绘制另一条线时,我绘制的线将被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!