Java 清除先前在图像上绘制的字符串

Java 清除先前在图像上绘制的字符串,java,image,swing,paint,Java,Image,Swing,Paint,我想知道如果不清除重叠的字符串,如何在图像上绘制新字符串之前清除先前绘制的字符串。我试过graphics#drawRect和重写paintComponent(graphics),但仍然没有评估 这是我的密码: 油漆组件(图形) 非常感谢您的帮助。:-) 谢谢。你不需要做定制绘画 以下是在带有图标的标签上绘制文本的几种不同方法: import java.awt.*; import javax.swing.*; import javax.swing.text.*; public class Lab

我想知道如果不清除重叠的字符串,如何在图像上绘制新字符串之前清除先前绘制的字符串。我试过graphics#drawRect和重写paintComponent(graphics),但仍然没有评估

这是我的密码: 油漆组件(图形)

非常感谢您的帮助。:-)


谢谢。

你不需要做定制绘画

以下是在带有图标的标签上绘制文本的几种不同方法:

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class LabelImageText extends JPanel
{
    public LabelImageText()
    {
        JLabel label1 = new JLabel( new ColorIcon(Color.ORANGE, 100, 100) );
        label1.setText( "Easy Way" );
        label1.setHorizontalTextPosition(JLabel.CENTER);
        label1.setVerticalTextPosition(JLabel.CENTER);
        add( label1 );

        //

        JLabel label2 = new JLabel( new ColorIcon(Color.YELLOW, 200, 150) );
        label2.setLayout( new BoxLayout(label2, BoxLayout.Y_AXIS) );
        add( label2 );

        JLabel text = new JLabel( "More Control" );
        text.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        label2.add( Box.createVerticalGlue() );
        label2.add( text );
        label2.add( Box.createVerticalStrut(10) );

        //

        JLabel label3 = new JLabel( new ColorIcon(Color.GREEN, 200, 150) );
        add( label3 );

        JLabel text3 = new JLabel();
        text3.setText("<html><center>Text<br>over<br>Image<center></html>");
        text3.setLocation(20, 20);
        text3.setSize(text3.getPreferredSize());
        label3.add( text3 );

        //

        JLabel label4 = new JLabel( new ColorIcon(Color.CYAN, 200, 150) );
        add( label4 );

        JTextPane textPane = new JTextPane();
        textPane.setText("Add some text that will wrap at your preferred width");
        textPane.setEditable( false );
        textPane.setOpaque(false);
        SimpleAttributeSet center = new SimpleAttributeSet();
        StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
        StyledDocument doc = textPane.getStyledDocument();
        doc.setParagraphAttributes(0, doc.getLength(), center, false);
        textPane.setBounds(20, 20, 75, 100);
        label4.add( textPane );
    }

    public static class ColorIcon implements Icon
    {
        private Color color;
        private int width;
        private int height;

        public ColorIcon(Color color, int width, int height)
        {
            this.color = color;
            this.width = width;
            this.height = height;
        }

        public int getIconWidth()
        {
            return width;
        }

        public int getIconHeight()
        {
            return height;
        }

        public void paintIcon(Component c, Graphics g, int x, int y)
        {
            g.setColor(color);
            g.fillRect(x, y, width, height);
        }
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("LabelImageText");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new LabelImageText() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
import java.awt.*;
导入javax.swing.*;
导入javax.swing.text.*;
公共类LabelImageText扩展了JPanel
{
公共LabelImageText()
{
JLabel label1=新的JLabel(新的颜色图标(Color.ORANGE,100100));
标签1.setText(“简易方式”);
标签1.设置水平文本位置(JLabel.中心);
标签1.设置垂直文本位置(JLabel.中心);
添加(标签1);
//
JLabel label2=新的JLabel(新的颜色图标(Color.YELLOW,200150));
label2.setLayout(新的BoxLayout(label2,BoxLayout.Y_轴));
添加(标签2);
JLabel text=新的JLabel(“更多控制”);
text.setAlignmentX(JLabel.CENTER\u对齐);
label2.add(Box.createVerticalGlue());
标签2.添加(文本);
标签2.添加(框。创建垂直支柱(10));
//
JLabel label3=新的JLabel(新的颜色图标(Color.GREEN,200150));
添加(标签3);
JLabel text3=新的JLabel();
text3.setText(“文本

图像上方”); text3.设置位置(20,20); text3.setSize(text3.getPreferredSize()); 标签3.添加(文本3); // JLabel label4=新的JLabel(新的ColorIcon(Color.CYAN,200150)); 添加(标签4); JTextPane textPane=新的JTextPane(); setText(“添加一些以您喜欢的宽度换行的文本”); textPane.setEditable(false); textPane.set不透明(false); SimpleAttributeSet center=新的SimpleAttributeSet(); StyleConstants.setAlignment(中心,StyleConstants.ALIGN\u中心); StyledDocument doc=textPane.getStyledDocument(); doc.setParagraphAttributes(0,doc.getLength(),居中,false); textPane.setBounds(20,20,75,100); 标签4.添加(文本窗格); } 公共静态类ColorIcon实现Icon { 私人色彩; 私有整数宽度; 私人内部高度; 公共颜色图标(颜色、整数宽度、整数高度) { 这个颜色=颜色; 这个。宽度=宽度; 高度=高度; } public int getIconWidth() { 返回宽度; } public int getIconHeight() { 返回高度; } 公共虚空绘制图标(组件c、图形g、整数x、整数y) { g、 设置颜色(颜色); g、 fillRect(x,y,宽度,高度); } } 私有静态void createAndShowUI() { JFrame=新的JFrame(“LabelImageText”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(新LabelImageText()); frame.pack(); frame.setLocationRelativeTo(空); frame.setVisible(true); } 公共静态void main(字符串[]args) { invokeLater(新的Runnable() { 公开募捐 { createAndShowUI(); } }); } }
然后,要更改文本,只需更改用于显示文本的组件中的文本

如果这些都没有帮助,那么进行自定义绘制的方法是重写JLabel的
paintcomponent()
方法。您不应该直接在BuffereImage上绘制文本。

基本上,您不能

您应该尝试做的是保留对原始背景图像的引用,当您需要更改文本时,将其复制到临时图像,并在那里绘制
字符串
,替换它(临时副本)作为标签的图标


更好的解决方案可能是直接在标签绘制过程中绘制文本,方法是覆盖
paintComponent
方法

有用的信息,谢谢。尽管@camickr提供的解决方案也在按我需要的方式工作。完美答案。非常感谢你!作为旁注,您的SplashScreen扩展了JLabel,但有一个成员是JLabel。据我所知,您根本没有将SplashScreen用作JLabel,只使用其成员。您的SplashScreen类具有JLabel,而不是JLabel。您可以很好地扩展JLabel,但是使用
这个
,而不是在其内部创建另一个(冗余)标签。看,哈!出色的捕获@Radiodef,老实说,我在上课时没有意识到这一点。(可能会解释为什么我的尝试没有正常工作)
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class LabelImageText extends JPanel
{
    public LabelImageText()
    {
        JLabel label1 = new JLabel( new ColorIcon(Color.ORANGE, 100, 100) );
        label1.setText( "Easy Way" );
        label1.setHorizontalTextPosition(JLabel.CENTER);
        label1.setVerticalTextPosition(JLabel.CENTER);
        add( label1 );

        //

        JLabel label2 = new JLabel( new ColorIcon(Color.YELLOW, 200, 150) );
        label2.setLayout( new BoxLayout(label2, BoxLayout.Y_AXIS) );
        add( label2 );

        JLabel text = new JLabel( "More Control" );
        text.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        label2.add( Box.createVerticalGlue() );
        label2.add( text );
        label2.add( Box.createVerticalStrut(10) );

        //

        JLabel label3 = new JLabel( new ColorIcon(Color.GREEN, 200, 150) );
        add( label3 );

        JLabel text3 = new JLabel();
        text3.setText("<html><center>Text<br>over<br>Image<center></html>");
        text3.setLocation(20, 20);
        text3.setSize(text3.getPreferredSize());
        label3.add( text3 );

        //

        JLabel label4 = new JLabel( new ColorIcon(Color.CYAN, 200, 150) );
        add( label4 );

        JTextPane textPane = new JTextPane();
        textPane.setText("Add some text that will wrap at your preferred width");
        textPane.setEditable( false );
        textPane.setOpaque(false);
        SimpleAttributeSet center = new SimpleAttributeSet();
        StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
        StyledDocument doc = textPane.getStyledDocument();
        doc.setParagraphAttributes(0, doc.getLength(), center, false);
        textPane.setBounds(20, 20, 75, 100);
        label4.add( textPane );
    }

    public static class ColorIcon implements Icon
    {
        private Color color;
        private int width;
        private int height;

        public ColorIcon(Color color, int width, int height)
        {
            this.color = color;
            this.width = width;
            this.height = height;
        }

        public int getIconWidth()
        {
            return width;
        }

        public int getIconHeight()
        {
            return height;
        }

        public void paintIcon(Component c, Graphics g, int x, int y)
        {
            g.setColor(color);
            g.fillRect(x, y, width, height);
        }
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("LabelImageText");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new LabelImageText() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}