java - 如何在java jFrame中动态添加图片和文字?

标签 java swing jframe paintcomponent custom-painting

我有一个类似于 html5 标签的选取框,文本从右向左水平移动,完美无误。

public class texscroll extends JPanel {
    private int x = 510, y = 25;
    private String string = "Text in moving similary to the tags HTML5 Marquesina (<marquee>).";

    public texscroll() {
        Font font = new Font("Arial", Font.BOLD + Font.PLAIN, 15);
        setFont(font);
        setForeground(Color.BLACK);
        setOpaque(false);
        Timer timer = new Timer(14, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                x -= 1;
                if (x == -10 * string.length()) {
                    x = 510;
                }
                repaint();
            }            
        });
        timer.start();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(720, 480);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
        Graphics2D g2 = (Graphics2D) g;
        g2.drawString(string, x, y);
    }        
}

但现在我希望能够添加或组合运动中的文本和图像。

private String string = "Text in moving similary to the tags HTML5 Marquesina (<marquee>).";

如何调用图像 (img/img.png) 以便它们可以在运动中可视化?

最佳答案

在绘制文本时简单地绘制图像:

class Texscroll extends JPanel {

    private int x = 510, y = 25;
    private final String string = "Text in moving similary to the tags HTML5 Marquesina (<marquee>).";
    private final Image image;

    public Texscroll() {
        Font font = new Font("Arial", Font.BOLD + Font.PLAIN, 15);
        setFont(font);
        setForeground(Color.BLACK);
        image = getImage();
        Timer timer = new Timer(14, arg0 -> {
            x -= 1;
            if (x == -10 * string.length()) {
                x = 510;
            }
            repaint();
        });
        timer.start();
        setPreferredSize(new Dimension(720, 480));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.drawString(string, x, y);
        g2.drawImage(image, x, y + 100, getFocusCycleRootAncestor());
    }

    //use any image
    private BufferedImage getImage() {
        int iconWidth =100;
        BufferedImage img = new BufferedImage(iconWidth , iconWidth,
                                            BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = img.createGraphics();
        g2.setColor(Color.RED);
        g2.fillOval(1, 1, iconWidth - 2, iconWidth - 2);
        g2.dispose();
        return img;
    }
}

关于java - 如何在java jFrame中动态添加图片和文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55057110/

相关文章:

java - 使用 JTable 将 JButton 添加到框架中

java - 如何在 Java 中搜索多行并行文本?

java - 延迟后调用方法

java - 将一个 json 属性映射到 java 中的两个不同字段 (jackson)

java - “log4j:configuration” 必须匹配 “(renderer….loggerFactory)?)”

Linux 上的全屏 Java - 如何覆盖任务栏?

Java - 在重新绘制高分辨率图像时,paintComponent() 会大大减慢程序速度

java - 向 JTable 添加一列,UI 不会改变

java - 将单独的 JPanel/JInternalFrame 加载到单独的 JDesktopPane(在 JFrame 中)

java - 向 JFrame 添加图 block 的更简单方法