0

我是 Java 新手。我想创建一个程序,在其中我需要通过拖动鼠标在 JPanel 上自由绘制。我通过了绘画功能的基础知识并能够实现这一点。

public class DrawLine extends JPanel {  
public void paint(Graphics g)
{

g.drawLine(0, 0, 50, 50);   
}
public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable(){
        public void run()
        {
            JFrame frame=new JFrame("Top Level Demo");
            frame.setSize(300, 250);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel myPanel=new JPanel();
            myPanel.setLayout(null);
            frame.add(myPanel);
            frame.add(new DrawLine());
            frame.setVisible(true);

        }});
    }}

但这会产生这样的输出,其中直线由坐标确定。 在此处输入图像描述

请有人帮我在 JPanel 中实现免费绘图。

4

3 回答 3

2

覆盖 JPanel 的 paintComponent() 而不是 paint() 方法。

创建要在paintComponent() 中使用的点列表。

在循环中迭代列表并为列表调用中的每一对点

g.drawLine(currentPoint.x,currentPoint.y, nextPoint.x,nextPoint.y); 

添加拖动监听以在列表中存储拖动点。

于 2013-11-05T10:26:19.890 回答
1

尝试使用MouseListenerpaintComponent方法。

尝试下一个简单的示例来绘制线条。

public class Test extends JPanel{

public static int xS = 0;
public static int yS = 0;
public static int xF = 0;
public static int yF = 0;

public static void main(String[] args){

    JFrame frame = new JFrame("Movement of 2d Shapes");
    frame.setSize(400,400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Test t = new Test();
    t.setOpaque(true);
    t.addMouseListener(getMouseListener(t));
    frame.getContentPane().add(t);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

private static MouseListener getMouseListener(final Test t) {
    return new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent arg0) {
            xS = arg0.getPoint().x;
            yS = arg0.getPoint().y;
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            xF = arg0.getPoint().x;
            yF = arg0.getPoint().y;
            t.repaint();
        }
    };
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawLine(xS,yS, xF, yF);
}
}
于 2013-11-05T10:27:59.987 回答
0
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class DrawLine extends JFrame implements MouseMotionListener {
    int x1,y1,x,y;
    private boolean first = true;
     public DrawLine() {
         super("Top Level Demo");
         setSize(300, 250);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setVisible(true);
         setBackground(Color.white);
         addMouseMotionListener(this);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        x =x1; y = y1;
        x1 = e.getX();
        y1 = e.getY();
        if(first){
            x = x1;
            y = y1;
            first  = false;
        }
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {

    }

    public void paint(Graphics graphics){
        graphics.drawLine(x, y, x1, y1);
    }

    public static void main(String[] args) {
        new DrawLine();
    }
}
于 2013-11-05T10:31:20.263 回答