这是我的代码...

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;

public class LineDrawing {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createWindow();
            }
        });
    }

    private static void createWindow() {
        JFrame mainFrame = new JFrame();

        mainFrame.add(new myPanel());
        mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        mainFrame.setBackground(Color.white);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);
    }
}

class myPanel extends JPanel {
    int startX;
    int startY;
    int endX;
    int endY;

    public myPanel() {

        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                startX = e.getX();
                startY = e.getY();
                repaint(startX, startY, endX, endY);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                endX = e.getX();
                endY = e.getY();
                repaint(startX, startY, endX, endY);
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                startX = e.getX();
                endX = startX;
                startY = e.getY();
                endY = startY;
                repaint(startX, startY, endX, endY);
            }
        });

        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                endX = e.getX();
                endY = e.getY();
                repaint(startX, startY, endX, endY);
            }
        });
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.black);
        g.drawLine(startX, startY, endX, endY);
        repaint();
    }
}


我正在尝试绘制多条线,同时使前一条线仍然存在于屏幕上(框架)...
 但是当我再次单击时,前一行将被擦掉或擦除...

我正在尝试设计一个图形分析应用程序(它将告诉输入的图形是否同构...

我的问题是我不能画多条线,而只能画一条线。

最佳答案

我的问题是我不能画多条线,而只能画一条线。


它仅绘制1条线,因为仅在paintComponent中绘制了1条线。
注意,这些附图不是累积的。

protected void paintComponent(Graphics g)
{
    super.paintComponent(g);
    g.setColor(Color.black);
    g.drawLine(startX,startY,endX,endY);  //this will draw only one line..
    repaint();                            //do not do this!
}


要绘制多条线(例如图形),您需要一个数据结构来保存每个图的数据。然后,您可以对其进行迭代并从那里绘制多条线。

因此,仅维护一组图(startX, startY, endX, endY)。您可以为Plot创建一个类:

public class Plot
{
    private int startX;
    private int startY;
    private int endX;
    private int endY;

    public Plot(int startX, int startY, int endX, int endY){
        this.startX = startX;
        this.startY = startY;
        this.endX = endX ;
        this.endY = endY ;
    }

    public void draw(Grpahics g){
        g.setColor(Color.black);
        g.drawLine(startX,startY,endX,endY);
    }
}


然后在显示面板中,保留“绘图”列表:

class MyPanel extends JPanel
{
    private ArrayList<Plot> plots;

    public MyPanel(){
        plots = new ArrayList<Plot>();
    }

    public void addPlot(int x1, int y1, int x2, int y2){
         plots.add(new Plot(x1, y1, x2, y2))
    }

    //Invoke addPlot() in your mouseListener after you get startXY & endXY
    //Invoke repaint() after addPlot()
    //Don't have to do this: "repaint(startX,startY,endX,endY);"
}


最后,在您的paintComponent()中:

@Override
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    for(Plot p : plots)
        p.draw(g);        //Each plot will draw itself
}


此外,您不应在repaint()中调用paintComponent()。需要刷新屏幕时,可以在paintComponent()外调用它。

关于java - 无法一次绘制多条线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46733539/

10-10 11:47