Java气泡图像适应文本大小 [英] Java bubble image adapt to text size

查看:181
本文介绍了Java气泡图像适应文本大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建某种信使服务,但我遇到了渲染文本泡泡的问题。我开始将气泡分成9个图像,四个角落是静态的。我有问题的地方是NORTH,SOUTH,EAST和WEST图像。

I am trying to build some sort of messenger service but I am having issues with the rendering of a text bubble. I started by splitting a bubble into 9 images, The four corner would be static. Where I have an issue is the NORTH, SOUTH, EAST and WEST images.

我希望他们重复自己类似于CSS选项重复。基本上,将气泡调整为内容。

I would like them to repeat themselves similar to the CSS option repeat. Basically, adapting the bubble to the content.

我环顾四周,但很多是Android或IOS解决方案。 Java中是否存在重复功能,并且可以在不重新调整paintComponent中的图像的情况下执行此操作,这会导致图像失真。

I looked around but a lot are Android or IOS solutions. Is there a repeat functionality in Java and can do this without re-sizing the image inside the paintComponent which causes the image to be distorted.

这是我得到的我尝试生成泡沫的那一刻:

Here is what I got for the moment when I try to generate a bubble:

欢迎任何想法!

推荐答案

您有(至少)两种选择。您可以根据需要平铺图像元素,也可以根据需要对其进行缩放。

You have (at least) two options available to you. You can tile the image elements as required or you can scale them to fit.

虽然我毫不怀疑缩放将起作用,它的外观有多好取决于它的好坏源图像是并且可能会在缩放图像时产生不需要的伪像。

While I have no doubt scaling will work, how good it looks will depend on how good the source image is and may produce undesirable artifacts as the image is scaled.

或者,您可以平铺图像以填充可用空间。根据图像的大小和复杂程度以及倾斜的区域,渲染器可能需要一些时间。

Alternatively, you could tile the images to fill the available space. Based on the size and complexity of the image and the area being tilted, this may take some time to renderer.

显然,平铺/缩放和图像的时间可以通过以下方式减轻缓冲结果并仅在输出发生变化时重新生成输出。

Obviously the time to tile/scale and image can mitigated by buffering the result and only re-generating the output when it changes.

最终的解决方案将归结为您希望实现的目标。

The final solution will come down to exactly what it is you hope to achieve.

以下是平铺的示例

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class BubbleTextTest {

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

    public BubbleTextTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

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

    public class TestPane extends JPanel {

        private JLabel label;

        public TestPane() {

            String text = "<html>I am the very model of a modern Major-General,<br>";
            text += "I've information vegetable, animal, and mineral,<br>";
            text += "I know the kings of England, and I quote the fights historical<br>";
            text += "From Marathon to Waterloo, in order categorical;a<br>";
            text += "I'm very well acquainted, too, with matters mathematical,<br>";
            text += "I understand equations, both the simple and quadratical,<br>";
            text += "About binomial theorem I'm teeming with a lot o' news, (bothered for a rhyme)<br>";
            text += "With many cheerful facts about the square of the hypotenuse.<br>";

            label = new JLabel(text);

            setBackground(new Color(209, 209, 209));

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            try {
                add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/TopLeft.png")))), gbc);
                gbc.gridx = 2;
                add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/TopRight.png")))), gbc);

                gbc.gridy = 2;
                gbc.gridx = 0;
                add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/BottomLeft.png")))), gbc);
                gbc.gridx = 2;
                add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/BottomRight.png")))), gbc);

                gbc.gridx = 1;
                gbc.gridy = 0;
                gbc.weightx = 1;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                add(new FillerPane(ImageIO.read(getClass().getResource("/Top.png")), FillDirection.HORIZONTAL), gbc);
                gbc.gridy = 2;
                add(new FillerPane(ImageIO.read(getClass().getResource("/Bottom.png")), FillDirection.HORIZONTAL), gbc);

                gbc.gridx = 0;
                gbc.gridy = 1;
                gbc.weighty = 1;
                gbc.weightx = 0;
                gbc.fill = GridBagConstraints.VERTICAL;
                add(new FillerPane(ImageIO.read(getClass().getResource("/Left.png")), FillDirection.VERTICAL), gbc);
                gbc.gridx = 2;
                add(new FillerPane(ImageIO.read(getClass().getResource("/Right.png")), FillDirection.VERTICAL), gbc);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(label, gbc);
        }

    }

    public enum FillDirection {
        HORIZONTAL,
        VERTICAL
    }

    public class FillerPane extends JPanel {

        private BufferedImage img;
        private FillDirection fillDirection;

        public FillerPane(BufferedImage img, FillDirection fillDirection) {
            this.img = img;
            this.fillDirection = fillDirection;
        }

        @Override
        public Dimension getPreferredSize() {
            return img == null ? super.getPreferredSize() : new Dimension(img.getWidth(), img.getHeight());
        }

        @Override
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); 
            if (img != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                int x = 0;
                int y = 0;
                int xDelta = 0;
                int yDelta = 0;
                switch (fillDirection) {
                    case HORIZONTAL:
                        xDelta = img.getWidth();
                        break;
                    case VERTICAL:
                        yDelta = img.getHeight();
                        break;
                }
                while (x < getWidth() && y < getHeight()) {
                    g2d.drawImage(img, x, y, this);
                    x += xDelta;
                    y += yDelta;
                }
                g2d.dispose();
            }
        }

    }

}

这篇关于Java气泡图像适应文本大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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