JTable 单元格无法正确渲染形状

发布于 2024-09-04 16:58:51 字数 1129 浏览 7 评论 0原文

我正在尝试使用子类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久随 2024-09-11 16:58:51

正如 @Gnoupi 通过 level 观察到的那样,目前尚不清楚 widthheight 是如何初始化的。为了满足类似的需求,此示例扩展了DefaultTableCellRenderer并实现了Icon更容易控制几何形状。它也可以在没有文本的情况下工作。

As @Gnoupi observes with level, it's not clear how width and height are initialized. To meet a similar need, this example extends DefaultTableCellRenderer and implements Icon for easier control over the geometry. It works without text as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文