Java 如何更改鼠标悬停的组件的背景?
我有一个尺寸为7 x 6的国际象棋网格,每个正方形都有一个背景,我希望在我悬停在它上面时背景颜色变为红色,然后在我悬停在其他物体上时恢复正常,我如何使用鼠标侦听器实现这一点 这是我的鼠标拖动侦听器:Java 如何更改鼠标悬停的组件的背景?,java,swing,hover,listener,mouse,Java,Swing,Hover,Listener,Mouse,我有一个尺寸为7 x 6的国际象棋网格,每个正方形都有一个背景,我希望在我悬停在它上面时背景颜色变为红色,然后在我悬停在其他物体上时恢复正常,我如何使用鼠标侦听器实现这一点 这是我的鼠标拖动侦听器: public void mouseDragged(MouseEvent me) { if (chessPiece == null) return; // The drag location should be within the bounds of the che
public void mouseDragged(MouseEvent me) {
if (chessPiece == null)
return;
// The drag location should be within the bounds of the chess board
int x = me.getX() + xAdjustment;
int xMax = chessBoard.getWidth() - chessPiece.getWidth();
x = Math.min(x, xMax);
x = Math.max(x, 0);
int y = me.getY() + yAdjustment;
int yMax = chessBoard.getHeight() - chessPiece.getHeight();
y = Math.min(y, yMax);
y = Math.max(y, 0);
chessPiece.setLocation(x, y); //drags the piece as i move, i want to
//change the hue of the square/Jpanel
//under my cursor as i move.
Component c= chessBoard.getComponentAt(chessPiece.getX(),chessPiece.getY());
}
Square是BackgroundPanel
package model.gui;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
/*
* Support custom painting on a panel in the form of
*
* a) images - that can be scaled, tiled or painted at original size
* b) non solid painting - that can be done by using a Paint object
*
* Also, any component added directly to this panel will be made
* non-opaque so that the custom painting can show through.
*/
public class BackgroundPanel extends JPanel
{
public static final int SCALED = 0;
public static final int TILED = 1;
public static final int ACTUAL = 2;
private Paint painter;
private Image image;
private int style = SCALED;
private float alignmentX = 0.5f;
private float alignmentY = 0.5f;
private boolean isTransparentAdd = true;
Graphics g;
/*
* Set image as the background with the SCALED style
*/
public BackgroundPanel(Image image)
{
this(image, SCALED);
}
/*
* Set image as the background with the specified style
*/
public BackgroundPanel(Image image, int style)
{
setImage( image );
setStyle( style );
setLayout( new BorderLayout() );
}
/*
* Set image as the backround with the specified style and alignment
*/
public BackgroundPanel(Image image, int style, float alignmentX, float alignmentY)
{
setImage( image );
setStyle( style );
setImageAlignmentX( alignmentX );
setImageAlignmentY( alignmentY );
setLayout( new BorderLayout() );
}
/*
* Use the Paint interface to paint a background
*/
public BackgroundPanel(Paint painter)
{
setPaint( painter );
setLayout( new BorderLayout() );
}
/*
* Set the image used as the background
*/
public void setImage(Image image)
{
this.image = image;
repaint();
}
/*
* Set the style used to paint the background image
*/
public void setStyle(int style)
{
this.style = style;
repaint();
}
/*
* Set the Paint object used to paint the background
*/
public void setPaint(Paint painter)
{
this.painter = painter;
repaint();
}
/*
* Specify the horizontal alignment of the image when using ACTUAL style
*/
public void setImageAlignmentX(float alignmentX)
{
this.alignmentX = alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f : alignmentX;
repaint();
}
/*
* Specify the horizontal alignment of the image when using ACTUAL style
*/
public void setImageAlignmentY(float alignmentY)
{
this.alignmentY = alignmentY > 1.0f ? 1.0f : alignmentY < 0.0f ? 0.0f : alignmentY;
repaint();
}
/*
* Override method so we can make the component transparent
*/
public void add(JComponent component)
{
add(component, null);
}
/*
* Override to provide a preferred size equal to the image size
*/
@Override
public Dimension getPreferredSize()
{
if (image == null)
return super.getPreferredSize();
else
return new Dimension(image.getWidth(null), image.getHeight(null));
}
/*
* Override method so we can make the component transparent
*/
public void add(JComponent component, Object constraints)
{
if (isTransparentAdd)
{
makeComponentTransparent(component);
}
super.add(component, constraints);
}
/*
* Controls whether components added to this panel should automatically
* be made transparent. That is, setOpaque(false) will be invoked.
* The default is set to true.
*/
public void setTransparentAdd(boolean isTransparentAdd)
{
this.isTransparentAdd = isTransparentAdd;
}
/*
* Try to make the component transparent.
* For components that use renderers, like JTable, you will also need to
* change the renderer to be transparent. An easy way to do this it to
* set the background of the table to a Color using an alpha value of 0.
*/
private void makeComponentTransparent(JComponent component)
{
component.setOpaque( false );
if (component instanceof JScrollPane)
{
JScrollPane scrollPane = (JScrollPane)component;
JViewport viewport = scrollPane.getViewport();
viewport.setOpaque( false );
Component c = viewport.getView();
if (c instanceof JComponent)
{
((JComponent)c).setOpaque( false );
}
}
}
/*
* Add custom painting
*/
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
// Invoke the painter for the background
if (painter != null)
{
Dimension d = getSize();
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(painter);
g2.fill( new Rectangle(0, 0, d.width, d.height) );
}
// Draw the image
if (image == null ) return;
switch (style)
{
case SCALED :
drawScaled(g);
break;
case TILED :
drawTiled(g);
break;
case ACTUAL :
drawActual(g);
break;
default:
drawScaled(g);
}
}
/*
* Custom painting code for drawing a SCALED image as the background
*/
private void drawScaled(Graphics g)
{
Dimension d = getSize();
g.drawImage(image, 0, 0, d.width, d.height, null);
}
/*
* Custom painting code for drawing TILED images as the background
*/
private void drawTiled(Graphics g)
{
Dimension d = getSize();
int width = image.getWidth( null );
int height = image.getHeight( null );
for (int x = 0; x < d.width; x += width)
{
for (int y = 0; y < d.height; y += height)
{
g.drawImage( image, x, y, null, null );
}
}
}
/*
* Custom painting code for drawing the ACTUAL image as the background.
* The image is positioned in the panel based on the horizontal and
* vertical alignments specified.
*/
private void drawActual(Graphics g)
{
Dimension d = getSize();
Insets insets = getInsets();
int width = d.width - insets.left - insets.right;
int height = d.height - insets.top - insets.left;
float x = (width - image.getWidth(null)) * alignmentX;
float y = (height - image.getHeight(null)) * alignmentY;
g.drawImage(image, (int)x + insets.left, (int)y + insets.top, this);
}
}
package model.gui;
导入java.awt.*;
导入java.awt.image.*;
导入javax.swing.*;
/*
*支持在面板上以以下形式进行自定义绘制:
*
*a)图像-可以按原始尺寸缩放、平铺或绘制
*b)非固体喷漆-可通过使用喷漆对象完成
*
*此外,将制作直接添加到此面板的任何组件
*非不透明,以便自定义绘画可以显示出来。
*/
公共类背景面板扩展JPanel
{
公共静态最终整型=0;
公共静态最终整块=1;
公共静态最终int实际值=2;
私人油漆匠;
私有图像;
私有整数样式=缩放;
专用浮球线形X=0.5f;
专用浮动路线Y=0.5f;
私有布尔值isTransparentAdd=true;
图形g;
/*
*将图像设置为具有缩放样式的背景
*/
公众背景面板(图片)
{
这(图像,缩放);
}
/*
*将图像设置为具有指定样式的背景
*/
公共背景面板(图像、int样式)
{
设置图像(图像);
设置样式(样式);
setLayout(新的BorderLayout());
}
/*
*将图像设置为具有指定样式和对齐方式的背面
*/
公共背景面板(图像、int样式、浮点对齐X、浮点对齐Y)
{
设置图像(图像);
设置样式(样式);
setImageAlignmentX(alignmentX);
setImageAlignmentY(alignmentY);
setLayout(新的BorderLayout());
}
/*
*使用“绘制”界面绘制背景
*/
公共背景面板(油漆工)
{
油漆工;
setLayout(新的BorderLayout());
}
/*
*设置用作背景的图像
*/
公共void setImage(图像)
{
这个图像=图像;
重新油漆();
}
/*
*设置用于绘制背景图像的样式
*/
公共void设置样式(int样式)
{
这个。风格=风格;
重新油漆();
}
/*
*设置用于绘制背景的绘制对象
*/
公共空间设置油漆(油漆工)
{
这个画家=画家;
重新油漆();
}
/*
*使用实际样式时,指定图像的水平对齐方式
*/
公共void setImageAlignmentX(浮动对齐X)
{
this.alignmentX=alignmentX>1.0f?1.0f:alignmentX<0.0f?0.0f:alignmentX;
重新油漆();
}
/*
*使用实际样式时,指定图像的水平对齐方式
*/
公共void setImageAlignmentY(浮动对齐)
{
this.alignmentY=alignmentY>1.0f?1.0f:alignmentY<0.0f?0.0f:alignmentY;
重新油漆();
}
/*
*重写方法,以便使组件透明
*/
公共无效添加(JComponent组件)
{
添加(组件,空);
}
/*
*替代以提供与图像大小相等的首选大小
*/
@凌驾
公共维度getPreferredSize()
{
if(image==null)
返回super.getPreferredSize();
其他的
返回新维度(image.getWidth(null)、image.getHeight(null));
}
/*
*重写方法,以便使组件透明
*/
公共void添加(JComponent组件、对象约束)
{
如果(isTransparentAdd)
{
使组件透明(组件);
}
super.add(组件、约束);
}
/*
*控制是否自动删除添加到此面板的组件
*设置为透明。也就是说,将调用set不透明(false)。
*默认设置为true。
*/
public void setTransparentAdd(布尔值isTransparentAdd)
{
this.isTransparentAdd=isTransparentAdd;
}
/*
*尝试使组件透明。
*对于使用渲染器的组件,如JTable,您还需要
*将渲染器更改为透明。这是一种简单的方法
*使用alpha值0将表格背景设置为颜色。
*/
私有void使组件透明(JComponent组件)
{
组件。set不透明(false);
if(JScrollPane的组件实例)
{
JScrollPane scrollPane=(JScrollPane)组件;
JViewport viewport=scrollPane.getViewport();
viewport.setOpaque(false);
组件c=viewport.getView();
if(JComponent的c实例)
{
((JComponent)c).set不透明(false);
}
}
}
/*
*添加自定义绘画
*/
@凌驾
受保护组件(图形g)
{
超级组件(g);
//调用画家作为背景
if(painter!=null)
{
维度d=getSize();
图形2d g2=(图形2d)g;
g2.设置油漆(油漆工);
g2.填充(新矩形(0,0,d.宽度,d.高度));
}
//画图像
if(image==null)返回;
开关(样式)
{
案例规模:
抽油量(g);
打破
案例平铺:
拉瓦(g);
打破
实际案例:
实际提款(g);
打破
违约:
抽油量(g);
}
}
/*
*用于绘制缩放图像作为背景的自定义绘制代码