gpt4 book ai didi

java - 获取 JLabel 中的图标位置

转载 作者:行者123 更新时间:2023-11-30 08:18:37 25 4
gpt4 key购买 nike

我试图在 JLabel 的图标上显示网格(和其他项目),但我不知道如何根据其布局获取图标坐标。

附言因为我还需要鼠标支持,所以我不能只使用 setIcon() 在图标内修改我的绘画操作

public class ExtendedJLabel extends JLabel implements MouseMotionListener
{
private final int GRID_STEP = 16;

public ExtendedJLabel()
{
super();
setOpaque(true);
setBackground(UIManager.getColor("controlShadow");
addMouseMotionListener(this);
}

@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);

if(icon == null)
return;

// Icon Origin
int ox = ...;
int oy = ...;

// Draw grid
g.setColor(UIManager.getColor("controlShadow"));

for(int row = 0; row < getIcon().getIconHeight() / GRID_STEP; row++)
g.drawLine(ox, oy + row * GRID_STEP, ox + getIcon().getIconWidth(), oy + row * GRID_STEP);

for(int col = 0; col < getIcon().getIconWidth() / GRID_STEP; col++)
g.drawLine(ox + col * GRID_STEP, oy, ox + col * GRID_STEP, oy + getIcon().getIconHeight());
}

@Override
public void mouseMoved(MouseEvent me)
{
// ...
}

@Override
public void mouseDragged(MouseEvent me)
{
// ...
}
}

最佳答案

i can't figure out how to get the icon coordinates according to its layout.

我相信您可以使用 SwingUtiltities.layoutCompoundLabel(...) 方法。

您可以:

  1. 创建自定义 JLabel 并覆盖 paintComponent() 方法来绘制网格,或者
  2. 使用 JLayer

因为我没怎么玩过JLayer,下面是一个使用这种方法的例子:

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;

public class LabelIconGridUI extends LayerUI<JComponent>
{
@Override
public void paint(Graphics g, JComponent c)
{
super.paint(g, c);

Graphics2D g2 = (Graphics2D) g.create();
g2.setColor( Color.ORANGE );

JLayer jlayer = (JLayer)c;
JLabel label = (JLabel)jlayer.getView();

Rectangle viewR = new Rectangle();
viewR.width = label.getSize().width;
viewR.height = label.getSize().height;
Rectangle iconR = new Rectangle();
Rectangle textR = new Rectangle();

String clippedText = SwingUtilities.layoutCompoundLabel
(
label,
g2.getFontMetrics(),
label.getText(),
label.getIcon(),
label.getVerticalAlignment(),
label.getHorizontalAlignment(),
label.getVerticalTextPosition(),
label.getHorizontalTextPosition(),
viewR,
iconR,
textR,
label.getIconTextGap()
);

int gridSize = 10;
int start = iconR.x + gridSize;
int end = iconR.x + iconR.width;

for (int i = start; i < end; i += gridSize)
{
g2.drawLine(i, iconR.y, i, iconR.y + iconR.height);
}

g2.dispose();
}

private static void createAndShowUI()
{
JLabel label = new JLabel("Some Text" );
label.setIcon( new ImageIcon( "DukeWaveRed.gif" ) );

label.setVerticalAlignment( JLabel.CENTER );
label.setHorizontalAlignment( JLabel.CENTER );
label.setVerticalTextPosition( JLabel.BOTTOM );
label.setHorizontalTextPosition( JLabel.CENTER );

LayerUI<JComponent> layerUI = new LabelIconGridUI();
JLayer<JComponent> layer = new JLayer<JComponent>(label, layerUI);

JFrame frame = new JFrame("Label Icon Grid");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( layer );
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}

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

关于java - 获取 JLabel 中的图标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27421324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com