简体   繁体   English

保存鼠标拖曳的所有x和y点

[英]Saving all x and y points from mousedragged

I have this where I drag my mouse and it basically paints in the shape of small ovals or rectangles, but I'm unsure of how to make it save all of the points to repaint them when the window is resized... I know it has something to do with paintComponent() but not quite sure how to go about doing it, any help would be greatly appreciated 我在这里拖动鼠标,它基本上以小椭圆或矩形的形状绘制,但是我不确定如何保存它的所有要点,以便在调整窗口大小时重新绘制它们……我知道与paintComponent()有关,但不确定如何去做,任何帮助将不胜感激

Here is the bit that does the drawing 这是做图的部分

public void mouseDragged(MouseEvent evt){
        Point pt = evt.getPoint();
        x = pt.x;
        y = pt.y;
        Graphics g = getGraphics();
        g.setColor(color);
        if(Shape == "Oval"){
            g.drawOval(x, y, 10, 10);
            }
        if(Shape == "Rectangle"){
            g.drawRect(x, y, 10, 10);
            } 
    }

public void paintComponent(Graphics g){
    super.paintComponent(g);

    }

You can create a list (external to your method) that stores all the points. 您可以创建一个列表(在您的方法外部)来存储所有点。

List<Point> list = new LinkedList<>() ;
public void mouseDragged(MouseEvent evt){
    Point pt = evt.getPoint();
    list.add(pt) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM