2、经过简单测试,发现上述方法果然只能实现基本的文字转图片功能,无法换行,然后搜到一篇博文java文字转换成图片输出,算是一个比较完整的demo了,笔者的最终代码就是在此文代码的基础上修改而成,此demo中的文字也就一行不到的文字,所以无法看到换行的效果
3、刚开始打出来的图片上文字是方块乱码,解决方法是先在jdk目录下添加相应字体,如果依然乱码,要修改ide编辑器bin目录下的配置文件
4、为了实现真正的换行效果,笔者在drawstring方法外围加了一个for循环,就可以实现换行效果
5、如果文字过多的话,可能一张图片放不下,之前的做法是图片的高度不固定,根据文字的行数来确定,发现那样做的话,文字非常多的话图片就非常长,所以还是要生成多张图片,图片的张数就通过文字的总行数除以每张图片(固定宽高)的行数来得到
6、如果一行文字过多的话,超过图片宽度的文字会被隐藏掉,所以需要提前对文字进行处理,使用集合list来处理,先获取每个字的宽度,再获取每行能放多少字,再判断如果一行文字大于每行能存放的字数时,就进行切割处理
import java.awt.Canvas import java.awt.Graphics import java.awt.Graphics2D import java.awt.RenderingHints import java.awt.image.BufferedImage /** * @author ZhengYesheng */ public class RotateImageCanvas extends Canvas implements Runnable { private static final long serialVersionUID = -1997487731464495923L BufferedImage img BufferedImage rotatedImg int degress = 0 public RotateImageCanvas(BufferedImage img) { super() this.img = img new Thread(this).start() } @Override public void run() { while (true) { //A,与B的代码配合决定旋转的速度 degress += 1 degress %= 360 repaint() try { if (degress == 0) { //绕一周后等待的时间在这里设置 Thread.sleep(3 * 1000) } else { //考虑到视觉平滑,这里不应大约40。 Thread.sleep(30) } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace() } } } @Override public void paint(Graphics graphics) { super.paint(graphics) //获取旋转指定角度后的图片。为了避免累计误差,这里是用原始图像旋转的 rotatedImg = rotateImage(img, degress) //绘制旋转后的图片 graphics.drawImage(rotatedImg, 0, 0, this) } /** * 旋转图片为指定角度。 * 注意:1、这个方法实现了图像的基于中点的旋转,要想绕指定点,需要配合Matrix类 * 2、为避免图像被裁切,结果图片的尺寸也需要动态计算 * 3、现在旋转后有黑色背景,如果不需要这个效果,需要设置结果图片的Alpha模式 * * @param bufferedimage * 目标图像 * @param degree * 旋转角度 * @return */ private BufferedImage rotateImage(BufferedImage bufferedimage, int degree) { int w = bufferedimage.getWidth() int h = bufferedimage.getHeight() int type = bufferedimage.getColorModel().getTransparency() BufferedImage img Graphics2D graphics2d (graphics2d = (img = new BufferedImage(w, h, type)).createGraphics()) .setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR) graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2) graphics2d.drawImage(bufferedimage, 0, 0, null) graphics2d.dispose() return img } }实现方式有很多如果你用我提供给你的估计到时候你的绘制或者碰撞可能也要改
我用的方法是用一个矩阵存旋转角度以及当前位置等相关信息 可能有点复杂
/** 设置朝向为指定弧度 */public void setOrientation(double orientation) {
if (this.orientation == orientation) {
return
}
if (points != null) {
double x, y
double sin = Math.sin(orientation - this.orientation)
double cos = Math.cos(orientation - this.orientation)
for (int i = 1 i < points.length i += 2) {
x = points[i - 1] - gravity.x
y = points[i] - gravity.y
points[i - 1] = x * cos - y * sin + gravity.x
points[i] = x * sin + y * cos + gravity.y
}
setBounds()
}
matrix.translate(center.x, center.y)
matrix.rotate(orientation - this.orientation)
matrix.translate(-center.x, -center.y)
this.orientation = orientation
}
public void paint(World world, Graphics2D g2d, AffineTransform at) {
at.setTransform(matrix.m00, matrix.m10, matrix.m01, matrix.m11, matrix.m02, matrix.m12)
g2d.drawRenderedImage(image, at)
at.setTransform(head.matrix.m00, head.matrix.m10, head.matrix.m01, head.matrix.m11, head.matrix.m02,
head.matrix.m12)
g2d.drawRenderedImage(head.image, at)
}
public void move() {
double x = Camera.getXOnWorld()
double y = Camera.getYOnWorld()
Vector.VECTOR.set(head.gravity.x, head.gravity.y)
head.matrix.transform(Vector.VECTOR)
head.setOrientation(Math.atan2(y - Vector.VECTOR.y, x - Vector.VECTOR.x))
move(SystemEvent.VK_W, SystemEvent.VK_S, SystemEvent.VK_A, SystemEvent.VK_D)
}
你如果只要求看起来是旋转的效果且炮台不再移动的话建议你用Graphics2D的下面这个方法用完之后麻烦再调用一次方向前面加个负号转回去就没啥问题了
public abstract void rotate(double theta,
double x,
double y)
将当前的 Graphics2D Transform 与平移后的旋转转换连接。后续呈现的变换如下:平移到指定位置,旋转指定弧度,然后向回平移相同的距离。这等同于以下调用序列: translate(x, y)
rotate(theta)
translate(-x, -y)
使用正角度 theta 进行旋转,可将正 x 轴上的点转向正 y 轴。
参数:theta - 旋转的角度,以弧度为单位x - 旋转原点的 x 坐标y - 旋转原点的 y 坐标