與透明組件的問題是,基本上,幾乎不可能實際點擊它們;)
另一個解決方案重刑可能是在組件上使用MouseListener
從點擊點繪製地圖和簡單的使譯文爲「虛擬網格」,然後你就可以渲染器在上面,例如...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class BaseBallMap {
public static void main(String[] args) {
new BaseBallMap();
}
public BaseBallMap() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class TestPane extends JPanel {
public static final int GRID_COUNT = 20;
private BufferedImage map;
private List<Point> cells;
public TestPane() {
cells = new ArrayList<>(400);
try {
map = ImageIO.read(new File("Map.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Point p = e.getPoint();
if (getMapBounds().contains(p)) {
p.x -= getXOffset();
p.y -= getYOffset();
int col = p.x/getColumnWidth();
int row = p.y/getRowHeight();
System.out.println(col + "x" + row);
Point cell = new Point(col, row);
if (cells.contains(cell)) {
cells.remove(cell);
} else {
cells.add(cell);
}
repaint();
}
}
});
}
@Override
public Dimension getPreferredSize() {
return map == null ? new Dimension(200, 200) : new Dimension(map.getWidth(), map.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (map != null) {
Graphics2D g2d = (Graphics2D) g.create();
int xOffset = getXOffset();
int yOffset = getYOffset();
int x = xOffset;
int y = yOffset;
g2d.drawImage(map, x, y, this);
int colWidth = getColumnWidth();
int rowHeight = map.getHeight()/GRID_COUNT;
g2d.setColor(new Color(255, 0, 0, 128));
for (Point p : cells) {
x = xOffset + (p.x * colWidth);
y = yOffset + (p.y * rowHeight);
g2d.fillRect(x, y, colWidth, rowHeight);
}
g2d.setColor(new Color(128, 128, 128, 64));
for (int col = 0; col < GRID_COUNT; col++) {
x = xOffset + (col * colWidth);
g2d.drawLine(x, yOffset, x, yOffset + map.getHeight());
}
for (int row = 0; row < GRID_COUNT; row++) {
y = yOffset + (row * rowHeight);
g2d.drawLine(xOffset, y, xOffset + map.getWidth(), y);
}
g2d.drawRect(xOffset, yOffset, map.getWidth(), map.getHeight());
g2d.dispose();
}
}
protected int getColumnWidth() {
return map == null ? 0 : map.getWidth()/GRID_COUNT;
}
protected int getRowHeight() {
return map == null ? 0 : map.getHeight()/GRID_COUNT;
}
protected int getXOffset() {
return map == null ? 0 : (getWidth() - map.getWidth())/2;
}
protected int getYOffset() {
return map == null ? 0 : (getHeight() - map.getHeight())/2;
}
protected Rectangle getMapBounds() {
return map == null ? new Rectangle(0, 0, 0, 0) : new Rectangle(getXOffset(), getYOffset(), map.getWidth(), map.getHeight());
}
}
}
你可以使用'MouseListener'手動執行此操作。 – MadProgrammer