Фоновое изображение JOptionPane - PullRequest
4 голосов
/ 13 апреля 2010

Как установить фоновое изображение JOptionPane? Я хочу показать другое изображение на фоне JOptionPane.

1 Ответ

1 голос
/ 13 апреля 2010

Вы можете расширить класс JOptionPane и переопределить метод рисования.

Edit: В зависимости от разрешения и качества изображения вы можете растянуть его с помощью AffineTransform во время событий WindowResize без особых искажений. Это позволит вам обработать несоответствие размера панели JOptionPane и изображения, указанное ниже.

   class ImageBackgroundPane extends JOptionPane
    {
         private BufferedImage img;

         public ImageBackgroundPane (BufferedImage image)
         {
            this.img = image;
         }

         @Override
         public void paint(Graphics g)
         {
           //Pick one of the two painting methods below.

           //Option 1:
           //Define the bounding region to paint based on image size.
           //Be careful, if the image is smaller than the JOptionPane size you
           //will see a solid white background where the image does not reach.
           g.drawImage(img, 0, 0, img.getWidth(), img.getHeight());

           //Option 2:
           //If the image can be guaranteed to be larger than the JOptionPane's size
           Dimension curSize = this.getSize();
           g.drawImage(img, 0, 0, curSize.width, curSize.height, null);


           //Make sure to paint all the other properties of Swing components.
           super.paint(g);
         }
    }
...