JTable 单元格无法正确渲染形状
我正在尝试使用子类 JPanel 渲染 JTable 单元格,并且单元格应显示为彩色矩形,并在其上绘制一个圆圈。当表格最初显示时,一切看起来都正常,但是当单元格被删除时,在单元格上显示对话框或其他内容时,已覆盖的单元格无法正确呈现,并且圆圈被破坏等。然后我必须移动滚动条或扩展窗口以使它们正确重画。
我用来渲染单元格的组件的 PaintComponent 方法如下:
protected void paintComponent(Graphics g) {
setOpaque(true);
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gradientPaint = new GradientPaint(new Point2D.Double(0, 0), Color.WHITE, new Point2D.Double(0,
getHeight()), paintRatingColour);
g2d.setPaint(gradientPaint);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
Rectangle clipBounds = g2d.getClipBounds();
int x = new Double(clipBounds.getWidth()).intValue() - 15;
int y = (new Double(clipBounds.getHeight()).intValue() / 2) - 6;
if (level != null) {
g2d.setColor(iconColour);
g2d.drawOval(x, y, width, height);
g2d.fillOval(x, y, width, height);
}
}
I'm trying to render my JTable cells with a subclassed JPanel and the cells should appear as coloured rectangles with a circle drawn on them. When the table displays initially everything looks OK but then when a dialog or something is displayed over the cells when it is removed the cells that have been covered do not rendered properly and the circles are broken up etc. I then have to move the scroll bar or extend the window to get them to redraw properly.
The paintComponent method of the component I'm using to render the cells is below:
protected void paintComponent(Graphics g) {
setOpaque(true);
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gradientPaint = new GradientPaint(new Point2D.Double(0, 0), Color.WHITE, new Point2D.Double(0,
getHeight()), paintRatingColour);
g2d.setPaint(gradientPaint);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
Rectangle clipBounds = g2d.getClipBounds();
int x = new Double(clipBounds.getWidth()).intValue() - 15;
int y = (new Double(clipBounds.getHeight()).intValue() / 2) - 6;
if (level != null) {
g2d.setColor(iconColour);
g2d.drawOval(x, y, width, height);
g2d.fillOval(x, y, width, height);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 @Gnoupi 通过
level
观察到的那样,目前尚不清楚width
和height
是如何初始化的。为了满足类似的需求,此示例扩展了DefaultTableCellRenderer
并实现了Icon
更容易控制几何形状。它也可以在没有文本的情况下工作。As @Gnoupi observes with
level
, it's not clear howwidth
andheight
are initialized. To meet a similar need, this example extendsDefaultTableCellRenderer
and implementsIcon
for easier control over the geometry. It works without text as well.