2011-09-06 89 views
4

我需要在图像中心写入文本。要写的文字并不总是相同的。Java - 在图像中心绘制文本

我正在使用的代码是在这里:

// Here I first draw the image 
g.drawImage(img, 22, 15, 280, 225, null); 
// I get the text 
String text = photoText.getText(); 
// Set the text color to black 
g.setColor(Color.black); 
// I draw the string 
g.drawString(text, 79.5F, 220.0F); 

的问题是,文字是不是在图像的中心,我该怎么办?

我只需要在水平中心绘制文本。

回答

5

一个可能的解决方案:在JPanel中绘制图像,确保将面板的首选大小设置为图像的大小,使JPanel使用GridBagLayout,并将文本放置到添加到JPanel的JLabel中,没有GridBagConstraints。这是将JLabel集中到JPanel中的一种方法。

+0

很容易和正确+1 – mKorbel

+0

+1让布局经理完成这项工作。 – trashgod

+0

我会试试这种方式。谢谢。 – GhzNcl

5

简单的方法是使用带有图标和文本的JLabel。然后将水平/垂直文本位置设置为CENTER,并将文本绘制在图像的中心。

从您的代码看起来好像您正在尝试在图像底部附近绘制文本。在这种情况下,您可以使用带有Icon的JLabel作为容器。然后,您可以将布局设置为BoxLayout之类的内容,并在文本中添加另一个标签。

两种方法都不需要自定义绘画。

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

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


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

我的意思是我必须写在图像的中心的文本,然后保存图像

您可以使用Screen Image创建任何成分的图像。这假定您正在GUI上显示图像和文本。或者,如果您只是在图像中添加文本并保存图像,那么您需要创建一个BufferedImage并在其上绘制图像,然后在其上绘制文本。您将需要使用Trashgod提到的FontMetrics类。我的建议无济于事。

+0

我将使用FontMetrics类,因为我需要保存图像,但我也使用GUI,我可以使用您的建议吗? 'ScreenImage'为+1; – GhzNcl

+0

;另见'ImageIO' [example。](http://stackoverflow.com/questions/2394192/java-imageio-write-completely-corrupted-png-and-or-bmp-with-incorrect-data/2394275#2394275 )。 – trashgod

+0

@GhzNcl,我不明白你的问题。我给你工作代码。只有你可以决定它是否做到了你想要的。我还给出了一个ScreenImage的链接,它显示了如何在可见或不可见组件上使用它。 – camickr

0

我以前TextLayout得到正确居中文本:

example of generated image

下面介绍如何创建和保存图片到文件:

int imgWidth = 250; 
int imgHeight = 250; 
BufferedImage img = new BufferedImage(imgWidth, imgHeight, 
     BufferedImage.TYPE_INT_RGB); 

Graphics2D g = img.createGraphics(); 

Color backgroundColor = new Color(0, 150, 100); 
g.setPaint(backgroundColor); 
g.fillRect(0, 0, imgWidth, imgHeight); 

Font font = new Font("Arial", Font.PLAIN, 80); 
g.setFont(font); 
g.setPaint(Color.WHITE); 

String text = "0"; 

TextLayout textLayout = new TextLayout(text, g.getFont(), 
     g.getFontRenderContext()); 
double textHeight = textLayout.getBounds().getHeight(); 
double textWidth = textLayout.getBounds().getWidth(); 

g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 
     RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 

// Draw the text in the center of the image 
g.drawString(text, imgWidth/2 - (int) textWidth/2, 
        imgHeight/2 + (int) textHeight/2); 

String imgFormat = "png"; 
ImageIO.write(img, imgFormat, new File("/home/me/new_image." + imgFormat));