How to do this relief when hovering on a JLabel?

2

The board is 500x500 and each square of it is 50px, so the variables NovaPosicaoX and Y always take the border of the square where the mouse pointer is.

The problem is that the relief only works in the squares of the window border, and when I move the mouse, this does not change. You have to take your mouse out of the window and get back to updating.

ImageIcon tabuleiro = new ImageIcon(getClass().getResource("tabuleiro.png"));
JLabel v = new JLabel(new ImageIcon(getClass().getResource("vazio.png"))); 
//Imagem que quero fazer o efeito de relevo ao passar o mouse sobre t

JLabel t = new JLabel(tabuleiro);

//Aqui seria o código onde coloco as posições de cada JLabel...
public void mouseEntered(MouseEvent arg0) {

double x = t.getMousePosition().getX();

double y = t.getMousePosition().getY();

int novaPosicaoX, novaPosicaoY;

novaPosicaoX = (int) x - (int)x % 50;

novaPosicaoY = (int) y - (int)y % 50;   

v.setBounds(novaPosicaoX-3, novaPosicaoY, 50,50);

v.setVisible(true);

}

Override
public void mouseExited(MouseEvent arg0) {
v.setVisible(false);
}

The red area does not work.

    
asked by anonymous 29.12.2016 / 18:30

1 answer

4

Well, your code is not a Minimum, Full, and Verifiable example , and depends on external sources (images), so do not I was able to reproduce the solution on top of the displayed code.

But I think the approach I've taken below can be adapted to your code, if the intention is to simulate relief in JLabels:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.MatteBorder;

/**
 *
 * @author diego
 */
public class MouseOverTabuleiro extends JFrame {

    JLabel[] labels;

    public void start() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(200, 200));
        setLayout(new GridLayout(4, 4));

        labels = new JLabel[16];

        for (int i = 0; i < labels.length; i++) {
            JLabel label = new JLabel();
            label.setOpaque(true);
            label.setBackground(Color.CYAN);
            label.setBorder(new MatteBorder(1, 1, 1, 1, Color.BLACK));
            label.addMouseListener(getMouseEvent());
            labels[i] = label;
            add(label);
        }

        pack();
        setVisible(true);
    }

    private MouseAdapter getMouseEvent() {

        MouseAdapter adapter = new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                JLabel label = (JLabel) e.getSource();
                label.setBorder(new MatteBorder(1, 1, 3, 3, Color.BLACK));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                JLabel label = (JLabel) e.getSource();
                label.setBorder(new MatteBorder(1, 1, 1, 1, Color.BLACK));
            }

        };
        return adapter;
    }

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

And the result:

Thekeytotheapproachistousethe MatteBorder to "simulate" the relief in the labels, changing the size of the bottom and right edges when the mouse is over this component through the mouseEntered event, and return the values of these two edges to similar to the other two when the mouse is out, through the mouseExited event.

    
31.12.2016 / 16:02