不使用Lines2D的橡皮筋

时间:2019-12-04 21:51:42

标签: java swing arraylist

我正尝试做橡皮筋,让我解释更多,每当我画一条线并画另一条线时,它会清除面板并且从不显示第一个画线,我想保存所有画出的线而没有使用Lines2D,我想使用ArrayList,一个用于first点,第二个用于second点。

这是我的代码:

package rubberlines;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class RubberLinesPanel extends JPanel {
    private Point point1 = null, point2 = null;
    private ArrayList<Point> pt1 = new ArrayList<Point>();
    private ArrayList<Point> pt2 = new ArrayList<Point>();
//    private ArrayList<Point> pt3 = new ArrayList<Point>();


    RubberLinesPanel(){
        LineListener listener = new LineListener();
        addMouseListener(listener);
        addMouseMotionListener(listener);
        setBackground(Color.black);
    }

    public void paintComponent(Graphics page){
        super.paintComponent(page);
        page.setColor(Color.green);
        for(int i=0; i<pt1.size(); i++){
        if(point1 != null && point2 != null){
            page.drawLine(point1.x, point1.y, point2.x, point2.y);
        }
        }
    }

    private class LineListener implements MouseListener, MouseMotionListener{
        public void mousePressed(MouseEvent event){
            point1 = event.getPoint();
            pt1.add(point1);
        }

        public void mouseDragged(MouseEvent event){
//            point2 = event.getPoint();
//            repaint();
        }

    public void mouseClicked(MouseEvent event){}
    public void mouseReleased(MouseEvent event){
        point2 = event.getPoint();
        pt2.add(point2);
        repaint();
//            for(int i=0; i<pt2.size(); i++){
//            pt2.get(i);
//            }            
        }
    public void mouseEntered(MouseEvent event){}
    public void mouseExited(MouseEvent event){}
    public void mouseMoved(MouseEvent event){}

    }

}

2 个答案:

答案 0 :(得分:1)

  

我想对第一点使用ArrayList,对第二点使用第二个。

那么,在尝试在两点之间绘制一条线之前,您实际上需要使用get(...)方法从ArrayList中获取每个点。

类似的东西:

for(int i = 0; i < pt1.size(); i++)
{
    Point point1 = pt1.get(i);
    Point point2 = pt2.get(i);

    if(point1 != null && point2 != null)
    {
        page.drawLine(point1.x, point1.y, point2.x, point2.y);
    }
}

答案 1 :(得分:1)

因此,您基本上需要一个List个点。然后,在调用paintComponent时,您只想遍历这些点,在每个点之间画一条线...

基本概念可能看起来像...

List<Point> drawPoints = new ArrayList<>(points);
Point startPoint = drawPoints.remove(0);
while (!drawPoints.isEmpty()) {
    Point nextPoint = drawPoints.remove(0);
    g2d.draw(new Line2D.Double(startPoint, nextPoint));
    startPoint = nextPoint;
}

这基本上是从一个起始点开始,得到下一个点,在它们之间画一条线。然后将下一个点分配给初始点并重复。

可运行的示例...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new RubberLinesPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class RubberLinesPanel extends JPanel {

        private ArrayList<Point> points = new ArrayList<Point>();
        private Point dragPoint;

        RubberLinesPanel() {
            LineListener listener = new LineListener();
            addMouseListener(listener);
            addMouseMotionListener(listener);
            setBackground(Color.black);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        @Override
        protected void paintComponent(Graphics page) {
            super.paintComponent(page);
            if (points.isEmpty()) {
                return;
            }
            Graphics2D g2d = (Graphics2D) page.create();
            g2d.setColor(Color.green);

            List<Point> drawPoints = new ArrayList<>(points);
            Point startPoint = drawPoints.remove(0);
            while (!drawPoints.isEmpty()) {
                Point nextPoint = drawPoints.remove(0);
                g2d.draw(new Line2D.Double(startPoint, nextPoint));
                startPoint = nextPoint;
            }

            if (dragPoint != null) {
                g2d.setColor(Color.LIGHT_GRAY);
                g2d.draw(new Line2D.Double(startPoint, dragPoint));
            }

            g2d.dispose();
        }

        private class LineListener extends MouseAdapter {

            @Override
            public void mousePressed(MouseEvent event) {
                points.add(event.getPoint());
                repaint();
            }

            @Override
            public void mouseDragged(MouseEvent event) {
                dragPoint = event.getPoint();
                repaint();
            }

            @Override
            public void mouseReleased(MouseEvent event) {
                points.add(event.getPoint());
                dragPoint = null;
                repaint();
            }

        }

    }
}