paintComponent()正在绘制其他组件 [英] paintComponent() is drawing on other components
问题描述
我正在使用自定义类,根据此答案中的代码,绘制一个形状像会话框。每当我调整应用程序窗口的大小足以使组件在顶部或底部突出时,所述组件的轮廓将在 JScrollPane
之外的其他组件之外绘制;在这种情况下 JPanel
。
I'm using a custom class, based on the code in this answer, to draw a background shaped like a speech bubble. Whenever I resize the window of my application enough to make a component poke out at the top or bottom, the outlines of the said component is drawn outside the JScrollPane
on top of other components; in this case the JPanel
.
在左侧图像中,组件底部的边框由于组件仍然可见,因此绘制了 JScrollPane
;而在右侧图像中,所提到的组件不再可见,一切看起来都是预期的。
In the left-side image, the border of the component at the bottom of the JScrollPane
is drawn, due to the component still being visible; while in the right-side image, the mentioned component is no longer visible and everything looks as intended.
我认为这与我是这样的事实有关使用 JScrollPane
来包含组件,从而允许组件在 JPanel
下滑动。我如何防止这种情况?
I believe it has something to do with the fact that I'm using a JScrollPane
to contain the components and thus allowing the component to slide under the JPanel
. How do I prevent this?
< img src =https://i.stack.imgur.com/0TMen.pngalt =图片>
主要:
public class Main {
public static void main(String[] args) {
JPanel panel = new JPanel(), panelbar = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panelbar.setLayout(new FlowLayout());
JScrollPane scroll = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JFrame frame = new JFrame("");
frame.setLayout(new BorderLayout());
frame.setSize(200, 223);
for (int i = 0; i < 6; i++) {
JLabel label = new JLabel("JLabel");
label.setBorder(new CustomBorder());
label.setOpaque(true);
label.setBackground(Color.ORANGE);
panel.add(label);
}
panelbar.add(new JLabel("JPanel"));
frame.add(scroll, BorderLayout.CENTER);
frame.add(panelbar, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
自定义类:
public class CustomBorder extends AbstractBorder {
private static final long serialVersionUID = 1L;
Insets i;
CustomBorder() {
i = new Insets(10, 20, 10, 20);
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
super.paintBorder(c, g, x, y, width, height);
Polygon bubble = new Polygon();
bubble.addPoint(x + 10, y + 5);
bubble.addPoint(x + width - 10, y + 5);
bubble.addPoint(x + width - 10, y + height / 3);
bubble.addPoint(x + width, y + height / 2);
bubble.addPoint(x + width - 10, y + height * 2 / 3);
bubble.addPoint(x + width - 10, y - 5 + height);
bubble.addPoint(x + 10, y - 5 + height);
Graphics2D g2d = (Graphics2D) g;
Area rect = new Area(new Rectangle(x, y, width, height));
rect.subtract(new Area(bubble));
g2d.setClip(rect);
g2d.setColor(c.getParent().getBackground());
g2d.fillRect(0, 0, width, height);
g2d.setClip(null);
g2d.setColor(Color.BLACK);
g2d.draw(bubble);
}
@Override
public Insets getBorderInsets(Component c) {
return i;
}
@Override
public Insets getBorderInsets(Component c, Insets insets) {
return i;
}
}
推荐答案
那里剪辑代码有两个问题:
There are two problems with the clipping code:
- 在减去气泡时,你不会从原始剪辑开始(导致组件成为在滚动窗格外绘制)
- 在绘制气泡之前,您不会恢复原始剪辑:
更改将是:
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
super.paintBorder(c, g, x, y, width, height);
Polygon bubble = new Polygon();
bubble.addPoint(x + 10, y + 5);
bubble.addPoint(x + width - 10, y + 5);
bubble.addPoint(x + width - 10, y + height / 3);
bubble.addPoint(x + width, y + height / 2);
bubble.addPoint(x + width - 10, y + height * 2 / 3);
bubble.addPoint(x + width - 10, y - 5 + height);
bubble.addPoint(x + 10, y - 5 + height);
Graphics2D g2d = (Graphics2D) g;
//Area rect = new Area(new Rectangle(x, y, width, height));
Shape clip = g2d.getClip();
Area rect = new Area(clip);
rect.subtract(new Area(bubble));
g2d.setClip(rect);
g2d.setColor(c.getParent().getBackground());
g2d.fillRect(0, 0, width, height);
//g2d.setClip(null);
g2d.setClip(clip);
g2d.setColor(Color.BLACK);
g2d.draw(bubble);
}
这篇关于paintComponent()正在绘制其他组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!