我有一个JPanel实现自定义绘图绘制背景。在此之上,应用程序可以放置JButton来检测对JPanel某些区域的点击。但是,使用鼠标突出显示(非透明)按钮时,底层的JPanel图形会变得非常糟糕。图形故障与JPanel的自定义绘图JButtons
这些是9个JPanels与自定义图中,它们中的每2个填充Jbutton将(R和L)。右上角的块和看起来像是“新鲜”的块。右下方有两个按钮高亮显示,中间一个只有“R”等
创建我的按钮,像这样:
rotatePanel = new JPanel();
rotatePanel.setOpaque(false);
GridLayout rotateLayout = new GridLayout(1, 2);
rotatePanel.setLayout(rotateLayout);
rotateRight = new JButton("R");
rotateRight.addActionListener(this);
rotateRight.setOpaque(false);
rotateRight.setContentAreaFilled(false);
rotateRight.setBorderPainted(false);
rotatePanel.add(rotateRight);
rotateLeft = new JButton("L");
rotateLeft.addActionListener(this);
rotateLeft.setOpaque(false);
rotateLeft.setContentAreaFilled(false);
rotateLeft.setBorderPainted(false);
rotatePanel.add(rotateLeft);
,这是我的绘制代码:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle clipRect = g.getClipBounds();
clipRect.grow(-4, -4);
int thirdWidth = clipRect.width/3;
int thirdHeight = clipRect.height/3;
for (int x = 0; x < Board.DIM; x++) {
//Draw the columns
for (int y = 0; y < Board.DIM; y++) {
//Draw the rows
g.drawRect(thirdWidth * x, thirdHeight * y, thirdWidth, thirdHeight);
}
}
g.setColor(Color.BLACK);
g.drawRect(0, 0, clipRect.width, clipRect.height);
}
啊,当然。现在我也学到了一些关于java绘画的新东西;)谢谢! – 2012-03-23 20:25:08
+1我不知道g.getClipBounds()在做什么。 – david 2012-03-23 20:35:28