如何拖动对象 [英] how to drag object

查看:223
本文介绍了如何拖动对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拖一箱,我想我的鼠标光标移动到在那里我在一个矩形点击,而IM draging矩形。我想这code和我有一堆错误,当我试图拖动。

 进口objectdraw *。进口java.awt中的*。公共类TouchWindow扩展WindowController
{    私人FilledRect一个;
    私人布尔B:
    私人位置c;
    私人诠释X;
    私人诠释Ÿ;
    公共无效开始()
    {
        B = FALSE;
        一个=新FilledRect(0,0,50,50,帆布);
    }
    公共无效onMouseClick(位置PT)
    {
        如果(a.contains(PT))
        {
        B =真实的;
        PT = C;
        }
    }
    公共无效onMouseDrag(位置PT2)
    {
        如果(二==真)
        {
        X =(int)的c.getX() - (int)的pt2.getX();
        Y =(INT)c.getY() - (INT)pt2.getY();
        a.move(X,Y);
        }
    }
}


解决方案

嗯这里是我已经躺在身边的例子,虽然不是一个小程序就可以非常简单地移植。

 进口java.awt.Color中;
进口java.awt.Dimension中;
进口java.awt.Graphics;
进口java.awt.Graphics2D中;
进口java.awt.Rectangle中;
进口java.awt.event.MouseAdapter;
进口java.awt.event.MouseEvent中;
进口javax.swing.JFrame中;
进口javax.swing.JPanel中;
进口javax.swing.SwingUtilities中;公共类ShapeMover {    公共ShapeMover(){
        JFrame的帧=新的JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle(形状捷运);        的initComponents(架);        frame.pack();
        frame.setVisible(真);
    }    公共静态无效的主要(一个String []){
        SwingUtilities.invokeLater(Runnable的新(){
            @覆盖
            公共无效的run(){
                新ShapeMover();
            }
        });    }    私人无效的initComponents(JFrame的帧){
        。frame.getContentPane()增加(新DragPanel());
    }
}类DragPanel继承JPanel {    矩形RECT =新的Rectangle(0,0,100,50);
    INT $ P $像素,preY;
    布尔isFirstTime = TRUE;
    矩形区域;
    布尔pressOut = FALSE;
    私人DIM维=新尺寸(400,300);    公共DragPanel(){
        的setBackground(Color.white);
        addMouseMotionListener(新MyMouseAdapter());
        addMouseListener将(新MyMouseAdapter());
    }    @覆盖
    公共尺寸的get preferredSize(){
        返回暗淡;
    }    @覆盖
    公共无效的paintComponent(图形G){
        super.paintComponent方法(G);        Graphics2D的G2D =(Graphics2D的)克;
        如果(isFirstTime){
            面积=新的Rectangle(DIM);
            rect.setLocation(50,50);
            isFirstTime = FALSE;
        }        g2d.setColor(Color.black);
        g2d.fill(RECT);
    }    布尔checkRect(){
        如果(区== NULL){
            返回false;
        }        如果(area.contains(rect.x,rect.y,rect.getWidth(),rect.getHeight())){
            返回true;
        }        INT一个new_x = rect.x;
        INT new_y = rect.y;        如果((rect.x + rect.getWidth())> area.getWidth()){
            一个new_x =(int)的area.getWidth() - (int)的(rect.getWidth() - 1);
        }
        如果(rect.x℃,){
            一个new_x = -1;
        }
        如果((rect.y + rect.getHeight())> area.getHeight()){
            new_y =(int)的area.getHeight() - (int)的(rect.getHeight() - 1);
        }
        如果(rect.y℃,){
            new_y = -1;
        }
        rect.setLocation(一个new_x,new_y);
        返回false;
    }    私有类MyMouseAdapter扩展MouseAdapter {        @覆盖
        公共无效鼠标pressed(的MouseEvent E){
            $ P $ =的pX rect.x - e.getX();
            preY = rect.y - e.getY();            如果(rect.contains(e.getX(),e.​​getY())){
                updateLocation(E);
            }其他{
                pressOut = TRUE;
            }
        }        @覆盖
        公共无效的mouseDragged(的MouseEvent E){
            如果(!pressOut){
                updateLocation(E);
            }其他{
            }
        }        @覆盖
        公共无效的mouseReleased(的MouseEvent E){
            如果(rect.contains(e.getX(),e.​​getY())){
                updateLocation(E);
            }其他{
                pressOut = FALSE;
            }
        }        公共无效updateLocation(的MouseEvent E){
            rect.setLocation($ P $的pX + e.getX(),preY + e.getY());
            checkRect();            重绘();
        }
    }
}

i want to drag a box and i want my mouse cursor to be where i clicked in a rectangle while im draging rectangle. i tried this code and i got bunch of errors when i tried to drag.

import objectdraw.*;

import java.awt.*;

public class TouchWindow extends WindowController
{

    private FilledRect a;
    private boolean b;
    private Location c;
    private int x;
    private int y;
    public void begin()
    {
        b=false;
        a=new FilledRect(0,0,50,50,canvas);
    }
    public void onMouseClick(Location pt)
    {
        if(a.contains(pt))
        {
        b=true;
        pt=c;
        }
    }
    public void onMouseDrag(Location pt2)
    {
        if(b==true)
        {
        x=(int)c.getX()-(int)pt2.getX();
        y=(int)c.getY()-(int)pt2.getY();
        a.move(x,y);
        }
    }
}

解决方案

Hmm here is an example I had lying around, although not an applet it can be very simply ported.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ShapeMover {

    public ShapeMover() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Shape Mover");

        initComponents(frame);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String s[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ShapeMover();
            }
        });

    }

    private void initComponents(JFrame frame) {
        frame.getContentPane().add(new DragPanel());
    }
}

class DragPanel extends JPanel {

    Rectangle rect = new Rectangle(0, 0, 100, 50);
    int preX, preY;
    boolean isFirstTime = true;
    Rectangle area;
    boolean pressOut = false;
    private Dimension dim = new Dimension(400, 300);

    public DragPanel() {
        setBackground(Color.white);
        addMouseMotionListener(new MyMouseAdapter());
        addMouseListener(new MyMouseAdapter());
    }

    @Override
    public Dimension getPreferredSize() {
        return dim;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        if (isFirstTime) {
            area = new Rectangle(dim);
            rect.setLocation(50, 50);
            isFirstTime = false;
        }

        g2d.setColor(Color.black);
        g2d.fill(rect);
    }

    boolean checkRect() {
        if (area == null) {
            return false;
        }

        if (area.contains(rect.x, rect.y, rect.getWidth(), rect.getHeight())) {
            return true;
        }

        int new_x = rect.x;
        int new_y = rect.y;

        if ((rect.x + rect.getWidth()) > area.getWidth()) {
            new_x = (int) area.getWidth() - (int) (rect.getWidth() - 1);
        }
        if (rect.x < 0) {
            new_x = -1;
        }
        if ((rect.y + rect.getHeight()) > area.getHeight()) {
            new_y = (int) area.getHeight() - (int) (rect.getHeight() - 1);
        }
        if (rect.y < 0) {
            new_y = -1;
        }
        rect.setLocation(new_x, new_y);
        return false;
    }

    private class MyMouseAdapter extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            preX = rect.x - e.getX();
            preY = rect.y - e.getY();

            if (rect.contains(e.getX(), e.getY())) {
                updateLocation(e);
            } else {
                pressOut = true;
            }
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            if (!pressOut) {
                updateLocation(e);
            } else {
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (rect.contains(e.getX(), e.getY())) {
                updateLocation(e);
            } else {
                pressOut = false;
            }
        }

        public void updateLocation(MouseEvent e) {
            rect.setLocation(preX + e.getX(), preY + e.getY());
            checkRect();

            repaint();
        }
    }
}

这篇关于如何拖动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆