gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:25:55 25 4
gpt4 key购买 nike

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

这是我的代码:PaintComponent(图形)

public class SplashScreen extends JLabel
{

private static final long serialVersionUID = 5515310205953670741L;

private static final String ROOT = "./assets/img/";

private static final SplashScreen INSTANCE = new SplashScreen( get( new File( ROOT, "splash.png" ) ), get( new File( ROOT, "splash-bar.png" ) ) );

private final BufferedImage background;

private final BufferedImage foreground;

private final JLabel label;


private SplashScreen( BufferedImage background, BufferedImage foreground )
{
this.background = background;
this.foreground = foreground;
label = new JLabel( new ImageIcon( background ) );

JWindow window = new JWindow();
window.setSize( background.getWidth(), background.getHeight() );
window.getContentPane().add( label );
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation( dimension.width / 2 - window.getSize().width / 2, dimension.height / 2 - window.getSize().height / 2 );
window.setVisible( true );
}


public void updateStatus( String status )
{
Graphics g = background.getGraphics();

g.drawString( status, 304, 301 );
g.dispose();

label.repaint();
}


public void updateBar( int width )
{
Graphics g = background.getGraphics();

g.drawImage( foreground, 73, 309, width, foreground.getHeight(), null );
g.dispose();

label.repaint();
}


private static BufferedImage get( File file )
{
try {
return ImageIO.read( file );
} catch( IOException e ) {
throw new RuntimeException( e.getMessage() );
}
}


public static SplashScreen getInstance()
{
return INSTANCE;
}

}

非常感谢任何帮助。 :-)

谢谢。

最佳答案

您不需要进行自定义绘画。

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

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();
}
});
}
}

然后,要更改文本,您只需更改用于显示文本的组件中的文本。

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

关于java - 清除先前在图像上绘制的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20534068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com