2

背景に画像を描画していJTextAreaます。他のルック アンド フィール (Metal、Windows など) を使用して描画されていますが、Nimbusルック アンド フィールを使用すると画像が描画されません。これが私が使用しているコードです

画像 TextArea クラス

public class ImageTextArea extends JTextArea{
    File image;
    public ImageTextArea(File image)
    {
        setOpaque(false);
        this.image=image;
    }

    @Override
    public void paintComponent(final Graphics g)
    {
        try
        {
            // Scale the image to fit by specifying width,height
            g.drawImage(new ImageIcon(image.getAbsolutePath()).getImage(),0,0,getWidth(),getHeight(),this);
            super.paintComponent(g);
        }catch(Exception e){}
    }
}

そしてテストクラス

public class TestImageTextArea extends javax.swing.JFrame {

    private ImageTextArea tx;

    public TestImageTextArea() {
        tx = new ImageTextArea(new File("img.jpg"));
        setTitle("this is a jtextarea with image");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel mainp = new JPanel(new BorderLayout());
        add(mainp);
        mainp.add(new JScrollPane(tx), BorderLayout.CENTER);
        setSize(400, 400);
    }

    public static void main(String args[]) {
/*
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception ex) {
            System.out.println("Unable to use Nimbus LnF: "+ex);
        }
*/
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TestImageTextArea().setVisible(true);
            }
        });
    }

}

コメントを削除すると、画像が描画されません。

4

1 に答える 1

6

基本的に、 を呼び出すsuper.paintComponentと、UI デリゲートのupdateメソッドが呼び出されます。ここで魔法が起こります。

以下はNimbusのSynthTextAreaUI実装です

public void update(Graphics g, JComponent c) {
    SynthContext context = getContext(c);

    SynthLookAndFeel.update(context, g);
    context.getPainter().paintTextAreaBackground(context,
                      g, 0, 0, c.getWidth(), c.getHeight());
    paint(context, g);
    context.dispose();
}

ご覧のとおり、コンポーネントの不透明な状態に関係なく、実際に背景をペイントし、 を呼び出しますpaint。これにより、BasicTextUI.paint( を介してsuper.paint)メソッドが呼び出されます。

BasicTextUI.paint実際にテキストを描画するため、これは重要です。

それで、それは私たちをどのように助けますか?通常、私は電話をかけない人を十字架につけsuper.paintComponentますが、これはまさに私たちがやろうとしていることですが、私たちがどのような責任を負っているのかを事前に知っておいてください.

まず、 の責任を引き継ぎ、update背景を塗りつぶし、背景をペイントpaintしてから、UI デリゲートを呼び出します。

ここに画像の説明を入力

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class NimbusTest {

    public static void main(String[] args) {
        new NimbusTest();
    }

    public NimbusTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(new TestTextArea()));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestTextArea extends JTextArea {

        private BufferedImage bg;

        public TestTextArea() {
            try {
                bg = ImageIO.read(new File("C:\\Users\\swhitehead\\Documents\\My Dropbox\\Ponies\\Rainbow_Dash_flying_past_3_S2E16.png"));
            } catch (IOException ex) {
                Logger.getLogger(NimbusTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g.create();
            // Fill the background, this is VERY important
            // fail to do this and you will have major problems
            g2d.setColor(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());
            // Draw the background
            g2d.drawImage(bg, 0, 0, this);
            // Paint the component content, ie the text
            getUI().paint(g2d, this);
            g2d.dispose();
        }

    }
}

間違えないでください。これを正しく行わないと、このコンポーネントだけでなく、おそらく画面上の他のほとんどのコンポーネントが台無しになります。

于 2013-07-26T11:09:13.997 に答える