Java Graphics Font - 如何确保字符位于特定区域?
我有一个图像。在图像的底部,我想创建一个高度为 100 的彩色条。我已经完成了条的创建,基本上可以在该部分写入字符串(例如图像的版权等)。以下是我的方法。
public static BufferedImage captionMyImage(BufferedImage sourceImage) {
int height=sourceImage.getHeight();
int width=sourceImage.getWidth();
System.out.println("Image Height: "+height);
System.out.println("Image Width: "+width);
int min=20; //fifty pixels
int newHeight=(int) (0.1*height>min ? 1.1*height : height+min);
int difference=newHeight-height;
System.out.println("new height:"+newHeight);
System.out.println("Difference:"+difference);
BufferedImage bgImage=new BufferedImage(width,newHeight,BufferedImage.TYPE_INT_RGB);
/**Create a Graphics from the background image**/
Graphics2D g = bgImage.createGraphics();
//g.drawRect(0, 0, width, ((int)0.1*height>min) ? (int)1.1*height : height+min);
g.setColor(Color.RED);
g.fillRect(0, 0, bgImage.getWidth(), bgImage.getHeight());
g.setColor(Color.YELLOW);
/**Set Antialias Rendering**/
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
/**
* Draw background image at location (0,0)
* You can change the (x,y) value as required
*/
g.drawImage(sourceImage, 0, 0, null);
int strX=sourceImage.getWidth()/2;
int strY=sourceImage.getHeight()+difference/2;
System.out.println("X: "+strX);
System.out.println("Y: "+strY);
g.setFont(new Font(g.getFont().getName(),Font.PLAIN,20));
g.drawString("(c)www.sanjaal.com",strX ,strY);
g.dispose();
return bgImage;
}
我知道我计算 drawString() 方法的 x 和 y 值的方法只是一种简单的方法,并且存在文本有时超出边界的问题(取决于图像大小和文本长度)
我想确保我定义的底部条带上图像中的文本始终与图像的右侧部分(边界)对齐,但不会超出边界。我怎样才能做到这一点?请记住,文本长度可以是动态的。
java 图形专家能否分享您关于如何实现这一目标的想法?
I have an Image. On the bottom portion of the image, I would like to create a colored strip say, of height 100. I am already done with creating the strip and I can basically write strings in that portion (e.g. copyright of the image etc). The following is my method.
public static BufferedImage captionMyImage(BufferedImage sourceImage) {
int height=sourceImage.getHeight();
int width=sourceImage.getWidth();
System.out.println("Image Height: "+height);
System.out.println("Image Width: "+width);
int min=20; //fifty pixels
int newHeight=(int) (0.1*height>min ? 1.1*height : height+min);
int difference=newHeight-height;
System.out.println("new height:"+newHeight);
System.out.println("Difference:"+difference);
BufferedImage bgImage=new BufferedImage(width,newHeight,BufferedImage.TYPE_INT_RGB);
/**Create a Graphics from the background image**/
Graphics2D g = bgImage.createGraphics();
//g.drawRect(0, 0, width, ((int)0.1*height>min) ? (int)1.1*height : height+min);
g.setColor(Color.RED);
g.fillRect(0, 0, bgImage.getWidth(), bgImage.getHeight());
g.setColor(Color.YELLOW);
/**Set Antialias Rendering**/
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
/**
* Draw background image at location (0,0)
* You can change the (x,y) value as required
*/
g.drawImage(sourceImage, 0, 0, null);
int strX=sourceImage.getWidth()/2;
int strY=sourceImage.getHeight()+difference/2;
System.out.println("X: "+strX);
System.out.println("Y: "+strY);
g.setFont(new Font(g.getFont().getName(),Font.PLAIN,20));
g.drawString("(c)www.sanjaal.com",strX ,strY);
g.dispose();
return bgImage;
}
I know the way I have calculated the values for x and y for drawString() method is just a simple one and has problem that the text goes outside the boundary sometimes (depending on image size and text length)
I would like to make sure that the text in the image on the bottom strip that I have defined always aligns to the right portion (boundary) of the image, but does not go out of boundary. How can I achieve that? Remember the text length can be dynamic.
Would the java Graphics experts out there share your ideas on how this can be achieved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)