Java:mouseDragged并在图形界面中移动

时间:2013-03-22 15:27:11

标签: java swing drag-and-drop awt paintcomponent

新手程序员在这里。

我正在制作一个程序,用于在笛卡尔坐标系中呈现用户输入的方程式。目前我遇到一些让用户在坐标中自由移动视图的问题。目前使用mouseDragged,用户可以稍微拖动视图,但是一旦用户释放鼠标并尝试再次移动视图,原点就会快速回到鼠标光标的当前位置。让用户自由移动的最佳方法是什么?提前谢谢!

这是绘图区的代码。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.JPanel;
public class DrawingArea extends JPanel implements MouseMotionListener {

private final int x_panel = 350; // width of the panel
private final int y_panel = 400; // height of the panel
private int div_x; // width of one square
private int div_y; // height of one square

private int real_y;
private int real_x;
private Point origin; // the origin of the coordinate
private Point temp; // temporary point

private static int y = 0;
private static int x = 0;

DrawingArea() {
    setBackground(Color.WHITE);

    real_x = x_panel;
    real_y = y_panel;

    setDivisionDefault();
    setOrigin(new Point((real_x / 2), (real_y / 2)));

    setSize(x_panel, y_panel);
    addMouseMotionListener(this);

}

DrawingArea(Point origin, Point destination) {
     this.origin = origin;
     this.destination = destination;
     panel = new JPanel();
     panel.setSize(destination.x, destination.y);
     panel.setLocation(origin);
     this.panel.setBackground(Color.red);
     panel.setLayout(null);


}

@Override
public void paintComponent(Graphics g) {

    super.paintComponent(g);

    Graphics2D line = (Graphics2D) g;


    temp = new Point(origin.x, origin.y);

    line.setColor(Color.red);
    drawHelpLines(line);

    line.setColor(Color.blue);
    drawOrigin(line);

    line.setColor(Color.green);
    for (int i = 0; i < 100; i++) { // This is a test line
        //temp = this.suora();

        temp.x++;
        temp.y++;

        line.drawLine(temp.x, temp.y, temp.x, temp.y);

    }



}

public void setOrigin(Point p) {
    origin = p;


}



public void drawOrigin(Graphics2D line) {
    line.drawLine(origin.x, 0, origin.x, y_panel);
    line.drawLine(0, origin.y, x_panel, origin.y);
}

public void drawHelpLines(Graphics2D line) {

    int xhelp= origin.x;
    int yhelp= origin.y;
    for (int i = 0; i < 20; i++) {
        xhelp+= div_x;
        line.drawLine(xhelp, 0, xhelp, y_panel);
    }
    xhelp= origin.x;
    for (int i = 0; i < 20; i++) {
        xhelp-= div_x;

        line.drawLine(xhelp, 0, xhelp, y_panel);
    }

    for (int i = 0; i < 20; i++) {
        yhelp-= div_y;
        line.drawLine(0, yhelp,x_panel, yhelp);
    }
    yhelp= origin.y;
    for (int i = 0; i < 20; i++) {
        yhelp+= div_y;
        line.drawLine(0, yhelp, x_panel, yhelp);
    }

}

public void setDivisionDefault() {
    div_x = 20;
    div_y = 20;

}

@Override
public void mouseDragged(MouseEvent e) {


    //Point temp_point = new Point(mouse_x,mouse_y);
    Point coords = new Point(e.getX(), e.getY());



    setOrigin(coords);

    repaint();

}

@Override
public void mouseMoved(MouseEvent e) {
}
}

1 个答案:

答案 0 :(得分:8)

基于此example,以下程序允许用户将轴的交点拖动到从面板中心开始的任意点origin

iamge

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @see https://stackoverflow.com/a/15576413/230513
 * @see https://stackoverflow.com/a/5312702/230513
 */
public class MouseDragTest extends JPanel {

    private static final String TITLE = "Drag me!";
    private static final int W = 640;
    private static final int H = 480;
    private Point origin = new Point(W / 2, H / 2);
    private Point mousePt;

    public MouseDragTest() {
        this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                mousePt = e.getPoint();
                repaint();
            }
        });
        this.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                int dx = e.getX() - mousePt.x;
                int dy = e.getY() - mousePt.y;
                origin.setLocation(origin.x + dx, origin.y + dy);
                mousePt = e.getPoint();
                repaint();
            }
        });
    }

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

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawLine(0, origin.y, getWidth(), origin.y);
        g.drawLine(origin.x, 0, origin.x, getHeight());
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame(TITLE);
                f.add(new MouseDragTest());
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }
}