我已经扩展了jEditorPane以包含并显示内存中的bufferedimage,但它没有正确绘制。我究竟做错了什么?

时间:2022-04-02 23:41:33

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?

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

public class JEditorPaneImg extends 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;
}

}

1 个解决方案

#1


0  

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.

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

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:

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

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.

如果您需要更多帮助,请在SSCCE上发布问题。

#1


0  

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.

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

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:

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

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.

如果您需要更多帮助,请在SSCCE上发布问题。