绘制另一条线时,我绘制的线条被移除

时间:2016-03-12 02:59:42

标签: java swing awt

绘制另一条线时,我绘制的线条将被删除。我正在使用鼠标事件绘制线条,但是当我绘制第二条线时,第一条线被移除。我相信它与我的点有关,它不断改变坐标的位置,我想画线,因为我的点击不断改变点,但我不确定是什么原因;请提前帮助谢谢

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

} }

2 个答案:

答案 0 :(得分:3)

如果你想要线条持续存在,那么你需要绘制所有需要看到的线条。这通常以两种方式之一完成:

  • 创建一个包含行数据的ArrayList,例如ArrayList<Line2D>,当你想添加一个新行时添加它,然后在paintComponent中,遍历列表绘制每一行。
  • 或者将每一行绘制到BufferedImage,并使用Graphics#drawImage(...)在paintComponent中绘制BufferedImage。

例如 - 使用BufferedImage,我们从BufferedImage获取Graphics对象,使用它绘制到图像,处理对象,然后调用repaint()以要求GUI重绘自己。 JPanel的paintComponent方法将绘制图像:

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

答案 1 :(得分:1)

结帐Custom Painting Approaches

它提供了两种常用增量绘画方法的实例:

  1. 从对象列表中绘制

  2. 在BufferedImage上绘图。

  3. 您使用哪种方法取决于您的确切要求。

相关问题