简体   繁体   中英

Java - how to Highlight Squares using a Mouse Listener. (using jframe)

Im creating a program and im stuck at the moment I want the tiles of the droughts board to glow another colour when its highlighted but im not very good with Actionlistners can someone help me out?

I use several class files and here is the code for each.

This is uni work so as I am learning I don't want the code given to me rather a few snippets and helpful advice to go on. the code in the class files is i think uncompilable due to errors because I don't know how to use actionlistners or mouselisteners yet.

To sum up i need an actionlistener for when my mouse hovers over the game board and the tiles change colour.

as I can only post 2 links I will give the 3 shortest class files on here.

EDIT : for ease I have got rid of the links. EDIT 2 : I am also sorry if this seems like a begginer question and well im asking because Im a beginner. EDIT 3 I have edited the Jframe class to accept mouse listener now I need help changing the color of the tiles, how do i get the color to change from this class file?

EDIT 4 ok i edited the color variable to public i think if thats what you meant, and also I added your code but I dont know which class is my mouselistener and I dont know how to add that color in the way you have laied it out to me.

EDIT 5 ok i tied to make this SSCCE not sure if i was able to do it or not, is this what you meant? if so would it be possible to help me?

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class Board extends JPanel 
{

private void setupPieces(int numRows)
{
int numPieces = ((numRows * numRows) - (2 * numRows)) / 4;
for (int i = 0; i < numPieces; i++)


{
    DraughtsPiece p = new DraughtsPiece(DraughtsPiece.LIGHT_PIECE_COLOUR);
    lightPieces.add(p);

    p = new DraughtsPiece(DraughtsPiece.DARK_PIECE_COLOUR);
    darkPieces.add(p);




    DraughtsPiece p = new DraughtsPiece(DraughtsPiece.LIGHT_PIECE_COLOUR);
    lightPieces.add(p);

     p = new DraughtsPiece(DraughtsPiece.DARK_PIECE_COLOUR);
     P.addMouseListener(new <Board.class>);
    darkPieces.add(p);

     }

     public void mouseEntered(MouseEvent m){
      ((DraughtsPiece) m.getSource()).color=<highlight color goes here>;
  }

  public void mouseExited(MouseEvent m){
      ((DraughtsPiece) m.getSource()).color=<normal color goes here>;
  }
  }

  darkPieces.add(p);
}

}

}

For hovering you do not want an ActionListener. ActionListeners are only for clicks on JButton, JMenuItem, etc. If you implement MouseListener then the mouseEntered(MouseEvent) and the mouseExited(MouseEvent) methods will get called whenever the mouse enters or exits one of your pieces.

EDIT: In response to your comments, you can try using this code in your DraughtsBoard.setupPieces method:

EDIT 2: I think you will need a repaint after mouse hovering, but I'm not sure because you removed a lot of your code. I have added code to make the board repaint after a piece color changes.

      DraughtsPiece p = new DraughtsPiece(DraughtsPiece.LIGHT_PIECE_COLOUR);
      lightPieces.add(p);

      p = new DraughtsPiece(DraughtsPiece.DARK_PIECE_COLOUR);
      p.addMouseListener(new MouseAdapter(){
          public void mouseEntered(MouseEvent m){
              ((DraughtsPiece) m.getSource()).color=<highlight color goes here>;
              DraughtsBoard.this.repaint(); // see my note
          }
          public void mouseExited(MouseEvent m){
              ((DraughtsPiece) m.getSource()).color=<normal color goes here>;
              DraughtsBoard.this.repaint();
          }
      });
      darkPieces.add(p);

And make DraughtsPiece.color public.

NOTE: You can only use DraughtsBoard.this in an inline class like this one. If you decide not to be lazy and actually have DraughtsPiece implement MouseListener, then you will need some other way to access DraughtsBoard such as a public static variable holding the instance of it

MouseAdapter is an extension of MouseListener which makes all extending classes not have to implement all of the methods from MouseListener. This means I can implement just mouseEntered and mouseExited and leave out the other methods from MouseListener which I don't need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM