[英]Get Icon position inside JLabel
我正在嘗試在JLabel的圖標上方顯示網格(和其他項目),但是我不知道如何根據其布局獲取圖標坐標。
PS因為我還需要鼠標支持,所以我不能只使用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)
{
// ...
}
}
我不知道如何根據其布局獲取圖標坐標。
我相信您可以使用SwingUtiltities.layoutCompoundLabel(...)
方法。
您可以:
JLabel
並重寫paintComponent()
方法以繪制網格,或者 JLayer
。 由於我對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();
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.