java Swing 裁剪问题 [英] java swing clipping problem

查看:34
本文介绍了java Swing 裁剪问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个摆动组件,它绘制一个固定的大(但固定)矢量图像,并用文本覆盖图像的一部分,这些文本应该相对于视口(不是绝对位置)显示——想想 excel 中的冻结行标签(如下图):

I have a swing component which draws a fixed large (but fixed) vector image and overlays parts of the image with text which should display relative to the view port (not the absolute position) -- think frozen row labels in excel (illustrated below):

Header
 -- [some stuff] ----- [ some stuff] ----
Header2
 ----- [some stuff] ----- [ some stuff] ----

这很好用,除非从左到右滚动.我尝试在 paintComponent() 方法中将剪辑边界设置为可见区域,以便始终绘制整个视口——但这似乎不起作用:

This works fine except when scrolling left to right. I try to set the clip bounds to the visible region in the paintComponent() method so that the entire viewport is always drawn -- however this does not appear to work:

public void paintComponent(Graphics graphics) {
  Graphics2D g = (Graphics2D)graphics;
  Shape oldClip = g.getClip();

  Rectangle clipBounds = getVisibleRect();
  g.setClip(clipBounds);

  drawMyImage();
  drawMyHeaders();

  g.setClip(oldClip);
}

然而,这似乎不起作用,我看到可见区域是正确的形状,但设置剪辑没有效果.我能做什么?

However, this does not seem to work, I see the visible region is the right shape, but setting the clip has no effect. What can I do?

clip: java.awt.Rectangle[x=1762,y=0,width=57,height=182]    // clipped while scrolling
 vis: java.awt.Rectangle[x=1762,y=0,width=582,height=182]   // what I want to paint

推荐答案

您没有将图形对象传递到标题中,因此它们必须通过其他可能不会设置剪切形状的东西进行绘制.

You aren't passing the graphics object into your headers, so they must be painting through something else which probably wont have your clipping shape set.

正如评论中所说,不要在paintComponent 中使用setClip!剪切区域由 Swing 使用.

As commented, don't use setClip in paintComponent! The clipping region is used by Swing.

看来您想要做的是在滚动组件上方放置一个叠加组件.

It seems what you want to do is have an overlay component layered above the scrolling component.

这篇关于java Swing 裁剪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆