Java- Write Text onto Image, then Write to Output File

前端 未结 3 1439
萌比男神i
萌比男神i 2021-01-13 01:33

I have an image on top of which I would like to write text that has multiple lines, is center-aligned, and dynamic (variable width). I\'ve tried using the drawString

3条回答
  •  不要未来只要你来
    2021-01-13 01:54

    Use a JLabel to display the image and text:

    JLabel picture = new JLabel( new ImageIcon(...) );
    picture.setText("
    Text
    over
    Image
    "); picture.setHorizontalTextPosition(JLabel.CENTER); picture.setVerticalTextPosition(JLabel.CENTER);

    Then you can use the Screen Image class to create an image of any component.

    Edit:

    Missed your update that you don't want the text centered on the image. For your new requirement you can add other components to the label to do the formatting for you. The code below shows the original suggestion as will as some examples of using components:

    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("
    Text
    over
    Image
    "); 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(); } }); } }

提交回复
热议问题