Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Graphics.drawString与上一个字符重叠_Java_Image_Graphics_Awt - Fatal编程技术网

Java Graphics.drawString与上一个字符重叠

Java Graphics.drawString与上一个字符重叠,java,image,graphics,awt,Java,Image,Graphics,Awt,我试图在图像的顶部和底部实现动态文本。下面是我现在能够生成的输出,但底线中的字符彼此重叠。请告诉我我的代码哪里出错了: 运行代码段以生成图像,如图像中所示: package com.logogenerator.util; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics2D; import j

我试图在图像的顶部和底部实现动态文本。下面是我现在能够生成的输出,但底线中的字符彼此重叠。请告诉我我的代码哪里出错了:

运行代码段以生成图像,如图像中所示:

package com.logogenerator.util;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.imageio.ImageIO;

public class CurvedText {

    public static void main(String[] args) throws NoninvertibleTransformException {
        try {

            final BufferedImage image = ImageIO.read(new File("E://xxx.png"));
            Graphics2D g;
            g = (Graphics2D) image.getGraphics();
            Font font = new Font("Serif", Font.PLAIN, 16);
            g.setFont(font);
            g.setColor(Color.GREEN);

            drawCircleTextTop("ABCDEFGH", image, g);
            drawCircleTextBottom("ABCDEFGH", image, g);

            g.dispose();
            ImageIO.write(image, "png", new File("E://Boathouse-WorkSpace//testboth.png"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Flip Down and writing the text 
     * @param s String to be written on the image
     * @param image Buffered image on which string to be written
     * @param g Graphics 2D object to access the Buffered Image
     * @throws IOException
     * @throws NoninvertibleTransformException
     */
    private static void drawCircleTextTop(String s, BufferedImage image, Graphics2D g) throws IOException, NoninvertibleTransformException {  

        if(image != null){

            Dimension cd = new Dimension(256,256);
            Point pt = new Point(cd.width / 2, cd.height / 2);
            int radius = 100;


            String st = s;
            Point center = pt;
            double r = radius;
            double charDegree = 5;
            double a1 = Math.toRadians(360 - (s.length()/2 * charDegree ));//-Math.PI/4;
            double af = 1.0;


            double curangle = a1;
            Point2D c = new Point2D.Double(center.x, center.y);
            char ch[] = st.toCharArray();
            FontMetrics fm = g.getFontMetrics();
            AffineTransform xform1, cxform;
            xform1 = AffineTransform.getTranslateInstance(c.getX(),c.getY());
            for(int i = 0; i < ch.length; i++) {
                double cwid = (double)(getWidth(ch[i],fm));
                if (!(ch[i] == ' ' || Character.isSpaceChar(ch[i]))) {
                    cwid = (double)(fm.charWidth(ch[i]));
                    cxform = new AffineTransform(xform1);
                    cxform.rotate(curangle, 0.0, 0.0);
                    String chstr = new String(ch, i, 1);
                    g.setTransform(cxform);
                    g.drawString(chstr, (float)(-cwid/2), (float)(-r));
                }

                // compute advance of angle assuming cwid<
                if (i < (ch.length - 1)) {
                    double adv = cwid/2.0 + fm.getLeading() + getWidth(ch[i + 1],fm)/2.0;
                    curangle += Math.sin(adv / r);
                }
            }

        }
    }

    /**
     * Flip Down and writing the text 
     * @param s String to be written on the image
     * @param image Buffered image on which string to be written
     * @param g Graphics 2D object to access the Buffered Image
     * @throws IOException 
     * @throws NoninvertibleTransformException
     */
    private static void drawCircleTextBottom(String s, BufferedImage image, Graphics2D g) throws IOException, NoninvertibleTransformException {  

        if(image != null){

            Dimension cd = new Dimension(256,256);
            Point pt = new Point(cd.width / 2, cd.height / 2);
            int radius = 100;

            String st = s;
            Point center = pt;
            double r = radius;
            double charDegree = 4;
            double a1 = Math.toRadians(360 - (s.length()/2 * charDegree ));//-Math.PI/4;
            double af = 1.0;



            double curangle = a1;
            Point2D c = new Point2D.Double(center.x, center.y);
            char ch[] = st.toCharArray();
            FontMetrics fm = g.getFontMetrics();
            AffineTransform xform1, cxform;
            xform1 = AffineTransform.getTranslateInstance(c.getX(),c.getY());
//            for(int i = ch.length-1; i > -1; i--) {
            for(int i = ch.length-1; i >-1; i--) {
                double cwid = (double)(getWidth(ch[i],fm));
                if (!(ch[i] == ' ' || Character.isSpaceChar(ch[i]))) {
                    cwid = (double)(fm.charWidth(ch[i]));
                    cxform = new AffineTransform(xform1); 
                    cxform.rotate(curangle, 0.0, 0.0);
                    String chstr = new String(ch, i, 1);
                    g.setTransform(cxform);
                    g.drawString(chstr, (float)(cwid/2), (float)(r));
//                    System.out.println("Curve Angle :"+curangle);
//                    System.out.println("Cwid : "+cwid);
                }

                // compute advance of angle assuming cwid<
                if (i < (ch.length - 1)) {
                    double adv = cwid/2.0 + fm.getLeading() + getWidth(ch[i + 1],fm)/2.0;
                    curangle += Math.sin((adv / r));
                }
            }

        }
    }

    static int getWidth(char c, FontMetrics fm) {
        if (c == ' ' || Character.isSpaceChar(c)) {
            return fm.charWidth('n');
        }
            else {
            return fm.charWidth(c);
        }
    }
}
package com.logogenerator.util;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Font;
导入java.awt.FontMetrics;
导入java.awt.Graphics2D;
导入java.awt.Point;
导入java.awt.geom.AffineTransform;
导入java.awt.geom.NoninvertibleTransformException;
导入java.awt.geom.Point2D;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入java.net.MalformedURLException;
导入javax.imageio.imageio;
公共类曲线文本{
公共静态void main(字符串[]args)引发不可逆的TransformException{
试一试{
final buffereImage image=ImageIO.read(新文件(“E://xxx.png”);
图形2dg;
g=(Graphics2D)image.getGraphics();
Font Font=新字体(“衬线”,Font.PLAIN,16);
g、 setFont(字体);
g、 setColor(Color.GREEN);
drawCircleTextTop(“ABCDEFGH”,图像,g);
drawCircleTextBottom(“ABCDEFGH”,图像,g);
g、 处置();
write(图像,“png”,新文件(“E://Boathouse WorkSpace//testtweach.png”);
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
/**
*翻转并写下文本
*要在图像上写入的@param s字符串
*@param image缓冲要写入字符串的图像
*@param g Graphics 2D对象访问缓冲图像
*@抛出异常
*@抛出不可逆的异常
*/
私有静态void drawCircleTextTop(字符串s、BuffereImage、Graphics2D g)引发IOException、NonVertibleTransformException{
如果(图像!=null){
尺寸cd=新尺寸(256256);
点pt=新点(cd.width/2,cd.height/2);
int半径=100;
字符串st=s;
点中心=pt;
双r=半径;
双焦度=5;
double a1=数学托拉迪安(360-(s.length()/2*charDegree));//-Math.PI/4;
双af=1.0;
双角=a1;
Point2D c=新的Point2D.Double(中心x,中心y);
char ch[]=st.toCharArray();
FontMetrics fm=g.getFontMetrics();
仿射变换xform1,cxform;
xform1=AffineTransform.getTranslateInstance(c.getX(),c.getY());
for(int i=0;i-1;i--){
对于(int i=ch.length-1;i>-1;i--){
double cwid=(double)(getWidth(ch[i],fm));
if(!(ch[i]=''Character.isSpaceChar(ch[i])){
cwid=(双)(调频字符宽度(ch[i]);
cxform=新的仿射变换形式(xform1);
cxform.旋转(库兰角,0.0,0.0);
字符串chstr=新字符串(ch,i,1);
g、 setTransform(cxform);
g、 抽绳(chstr,(浮子)(cwid/2),(浮子)(r));
//System.out.println(“曲线角度:+curangle”);
//System.out.println(“Cwid:+Cwid”);
}
//假设cwid计算角度提前<
如果(i<(通道长度-1)){
双adv=cwid/2.0+fm.getLeading()+getWidth(ch[i+1],fm)/2.0;
curangle+=数学sin((adv/r));
}
}
}
}
静态int getWidth(字符c、FontMetrics fm){
if(c=''|| Character.isSpaceChar(c)){
返回格式字符宽度('n');
}