user3883076
user3883076

Reputation: 3

I want to drag one object at a time but this code drags both the objects at a time

I want to drag one object at a time but this code drags both the objects at a time

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);
      }

   }
   }

Upvotes: 0

Views: 657

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

The first thing you need is some way to tell the difference between your "objects".

The next thing you need is some way to reference this objects in a convenient method. You could use an array, but I'm lazy and prefer to use a List

Once you have these two things, painting the boxes is just a matter of looping through the List and determining which object was clicked is becomes basically the same process...

To that end, I wrapped a java.awt.Rectangle around a custom Box class, which also maintains color information, this makes it much easier to manage as Rectangle has some very useful methods and can be painted easily.

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;
        }

    }

}

Take a look at Performing Custom Painting and 2D Graphics for more details

Upvotes: 1

Related Questions