我想一次拖动一个对象,但是这段代码一次拖动两个对象

时间:2014-08-01 05:44:13

标签: java drag-and-drop applet awt mouse

我想一次拖动一个对象,但是这段代码一次拖动两个对象

package javaPgm;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import javax.swing.ImageIcon;

import java.awt.Color;
import java.awt.Graphics.*;
public class Drag extends Applet
   implements MouseListener, MouseMotionListener {

   int width, height;
   int x, y;    // coordinates of upper left corner of the box
   int mx, my;  // the most recently recorded mouse coordinates
   boolean isMouseDraggingBox = false;
   public void init() {

      width = getSize().width;
      height = getSize().height;

      x = width/2 - 20;
      y = height/2 - 20;

      addMouseListener( this );
      addMouseMotionListener( this );






   }

public void mouseEntered( MouseEvent e ) { }
   public void mouseExited( MouseEvent e ) { }
   public void mouseClicked( MouseEvent e ) { }
   public void mousePressed( MouseEvent e ) {
      mx = e.getX();
      my = e.getY();
      if ( x < mx && mx < x+40 && y < my && my < y+40 ) {
         isMouseDraggingBox = true;
      }
      e.consume();// consumes the current event
   }
   public void mouseReleased( MouseEvent e ) {
      isMouseDraggingBox = false;
      e.consume();
   }
   public void mouseMoved( MouseEvent e ) { }
   public void mouseDragged( MouseEvent e ) {
      if ( isMouseDraggingBox ) {
                  int new_mx = e.getX();
      int new_my = e.getY();
         x += new_mx - mx;
         y += new_my - my;

           mx = new_mx;
           my = new_my;

         repaint();
         e.consume();
      }
   }

   public void paint( Graphics g ) {
      g.drawRect( 20, 40, 50, 85 );
      g.drawString("circle",20 , 38);
      Font f = new Font("TimesRoman", Font.PLAIN, 20);// to set font style and size
      g.setFont(f);
      g.drawString("RED=Positive charge", 175, 188);// to write a text
      g.drawString("BLUE=negative charge", 199, 210);
     g.drawRect(100,140,60,85);
      g.setColor(Color.red);
      g.fillOval(x,y,15,15);
      g.setColor(Color.blue);
      g.fillOval(x-110,y-135,15,15);
        setBackground(Color.yellow);
   for(int i=0;i<=950;i+=30){// to draw horizontal and vertical lines
     g.setColor(Color.orange);
          g.drawLine(i,0,i,950);
      }
      for(int j=0;j<=950;j+=30){
          g.drawLine(0, j, 950, j);
      }

   }
   }

1 个答案:

答案 0 :(得分:1)

你需要的第一件事是告诉你的&#34;对象&#34;之间的区别。

您需要的下一件事是以方便的方法引用此对象的某种方式。您可以使用数组,但我很懒,更喜欢使用List

一旦你掌握了这两件事,绘制方框只是循环遍历List并确定点击哪个对象变得基本相同...

为此,我在自定义java.awt.Rectangle类周围包裹了Box,该类还维护了颜色信息,这使得管理更加容易,因为Rectangle有一些非常有用的方法,可以很容易地画出来。

What a drag

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JApplet;
import javax.swing.JPanel;

public class WhatADag extends JApplet {

    @Override
    public void init() {
        add(new DragPane());
    }

    public class DragPane extends JPanel {

        private List<Box> boxes;

        public DragPane() {
            boxes = new ArrayList<>(25);
            boxes.add(new Box(0, 0, 50, 50, Color.RED));
            boxes.add(new Box(150, 150, 50, 50, Color.BLUE));

            MouseAdapter handler = new MouseAdapter() {

                private Box hitBox;
                private Point offset;

                @Override
                public void mousePressed(MouseEvent e) {
                    Point mp = e.getPoint();
                    for (Box box : boxes) {
                        if (box.getBounds().contains(mp)) {
                            hitBox = box;

                            offset = new Point();
                            offset.x = mp.x - box.getBounds().x;
                            offset.y = mp.y - box.getBounds().y;
                        }
                    }
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    hitBox = null;
                }

                @Override
                public void mouseDragged(MouseEvent e) {
                    if (hitBox != null) {
                        Point mp = e.getPoint();
                        Rectangle bounds = hitBox.getBounds();
                        bounds.x = mp.x - offset.x;
                        bounds.y = mp.y - offset.y;
                        repaint();
                    }
                }

            };

            addMouseListener(handler);
            addMouseMotionListener(handler);

        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            for (Box rect : boxes) {
                g2d.setColor(rect.getColor());
                g2d.fill(rect.getBounds());
            }
            g2d.dispose();
        }

    }

    public class Box {

        private Color color;
        private Rectangle rectangle;

        public Box(int x, int y, int width, int height, Color color) {
            rectangle = new Rectangle(x, y, width, height);
            setColor(color);
        }

        public void setColor(Color color) {
            this.color = color;
        }

        public Color getColor() {
            return color;
        }

        public Rectangle getBounds() {
            return rectangle;
        }

    }

}

请查看Performing Custom Painting2D Graphics了解详情