user3560893
user3560893

Reputation:

Java - Access datas of a MouseListener from another Component

I'm currently working on a level editor for a game. Currently, to create an Entity, you have to click on a creation JButton, then a form appears, where you enter its coordinate and its size.

I would like to implement a mouse fonction. The user click on the creation JButton, then have to press the mouseButton somewhere on the JPanel where the level is previewed, then drag to set the size of the object, and finally release the button. The object is then created where the button was pressed.

I add a MouseListener to the previewPanel (to get the correct coordinates).

My problem is : what should I do when I click on the button ?
Inside the actionPerformed method ?

bascially, the procedure would be :
1) Wait the button to be pressed
2) get coordinates
3) Wait the button to be released
4) Get new coordinates to make the size of the object
5) Create object

How should I proceed to do it properly ?

Thanks in advance

Upvotes: 2

Views: 627

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285450

My problem is : what should I do when I click on the button ? Inside the actionPerformed method ?

Activate the MouseListener. This can be by adding the MouseListener and MouseMotionListener (a MouseAdapater can do both) to the drawing JPanel on button click, or by changing the state of an already added MouseAdapater (my preference), again one that has already been added to the JPanel. This could be as simple as switching a boolean variable to true, and then have the mousePressed, mouseDragged, mouseReleased methods check the state of this boolean before doing any of their actions.

Note that if you go the first route -- by adding a MouseListener/MouseMotionListener on button press, you risk adding multiple listeners if you don't take care to remove them when you're through. That is why I prefer the 2nd approach.

e.g.,

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.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

public class ActivateMouse extends JPanel {
   private static final int PREF_W = 400;
   private static final int PREF_H = PREF_W;
   private static final Color DRAW_RECT_COLOR = Color.pink.brighter();
   private static final Color DRAW_ALL_RECTS_COLOR = Color.red;
   private boolean mouseActive = false;
   private Shape drawRect = null;
   private List<Shape> shapeList = new ArrayList<>();
   private ButtonAction buttonAction = new ButtonAction("Create New Rectangle", KeyEvent.VK_C);

   public ActivateMouse() {
      MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
      addMouseListener(myMouseAdapter);
      addMouseMotionListener(myMouseAdapter);

      add(new JButton(buttonAction));
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      if (drawRect != null) {
         g2.setColor(DRAW_RECT_COLOR);
         g2.draw(drawRect);
      }

      g2.setColor(DRAW_ALL_RECTS_COLOR);
      for (Shape shape : shapeList) {
         g2.draw(shape);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   private class MyMouseAdapter extends MouseAdapter {
      private Point firstPt;

      @Override
      public void mousePressed(MouseEvent e) {
         if (mouseActive && e.getButton() == MouseEvent.BUTTON1) {
            firstPt = e.getPoint();
         }
      }

      @Override
      public void mouseDragged(MouseEvent e) {
         if (!mouseActive || firstPt == null) {
            return;
         }
         drawRect = createRect(e.getPoint());
         repaint();
      }

      @Override
      public void mouseReleased(MouseEvent e) {
         if (!mouseActive || firstPt == null) {
            return;
         }
         shapeList.add(createRect(e.getPoint()));
         repaint();
         mouseActive = false;
      }

      private Shape createRect(Point p) {
         int x = Math.min(firstPt.x, p.x);
         int y = Math.min(firstPt.y, p.y);
         int width = Math.abs(firstPt.x - p.x);
         int height = Math.abs(firstPt.y - p.y);
         return new Rectangle(x, y, width, height);
      }
   }

   private class ButtonAction extends AbstractAction {
      public ButtonAction(String name, int mnemonic) {
         super(name);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         mouseActive = true;
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("ActivateMouse");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new ActivateMouse());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Upvotes: 1

Related Questions