JPanel里面的免费绘图

时间:2013-11-05 09:55:17

标签: java swing drawing jpanel paint

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

        }});
    }}

但是这会产生这样的输出,其中直线由坐标确定。 enter image description here

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

3 个答案:

答案 0 :(得分:2)

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

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

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

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

添加拖动,侦听列表中的商店拖动点。

答案 1 :(得分: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);
}
}

答案 2 :(得分: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();
    }
}