我已经扩展了 jEditorPane 以包含并显示内存中的缓冲图像,但它无法正确绘制。我做错了什么?

发布于 2024-09-13 13:54:10 字数 502 浏览 1 评论 0原文

我已经扩展了 jEditorPane,如下所示(减去实例化代码)。但是,当我设置图像并在对象上调用更新时,它只绘制图像的一小部分(相当于一行文本所在的位置)。有人可以告诉我我在这里做错了什么吗?

公共类 JEditorPaneImg 扩展 JEditorPane {

private BufferedImage bi = null;

public JEditorPaneImg() {
    initComponents();
}

@Override
public void paint(Graphics g) {
    super.paint(g);
    if (bi != null) {
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(bi, 0, 0, this);
    }
}

public void setImage(BufferedImage image){
    bi = image;
}

}

I have extended jEditorPane as shown below (minus the instantiation code). However, when I set the image and call update on the object, it only draws a small portion of the image (equivalent to where one line of text would go). Can somene tell me what I am doing wrong here?

public class JEditorPaneImg extends JEditorPane {

private BufferedImage bi = null;

public JEditorPaneImg() {
    initComponents();
}

@Override
public void paint(Graphics g) {
    super.paint(g);
    if (bi != null) {
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(bi, 0, 0, this);
    }
}

public void setImage(BufferedImage image){
    bi = image;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

故事和酒 2024-09-20 13:54:10

我不明白你想做什么。看起来您正在尝试在编辑器窗格中的文本顶部绘制图像。

首先,你永远不应该调用 update()。 Swing 将确定何时需要进行绘制。

如果您想在编辑器窗格顶部绘制图像,则无需将自定义绘制添加到编辑器窗格。您要做的就是创建一个 JLabel 并向该标签添加一个 ImageIcon。然后将标签添加到编辑器窗格。确保使用:

label.setSize( label.getPreferredSize() );

并且标签将简单地绘制为编辑器窗格的子组件。

如果您需要更多帮助,请发布显示问题的 SSCCE

I don't understand what you are attempting to do. It looks like you are trying to paint an image on top fo the the text in the editor pane.

First of all you should never invoke update(). Swing will determine when painting needs to be done.

If you want to paint an image on top of the editor pane, then there is no need to add custom painting to the editor pane. All you do is is create a JLabel and add an ImageIcon to the label. Then add the label to the editor pane. Make sure you use:

label.setSize( label.getPreferredSize() );

and the label will simply be painted as a child component of the editor pane.

If you need more help post your SSCCE showing the problem.

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