「java中geom」java中geometry
本篇文章给大家谈谈java中geom,以及java中geometry对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、Java中字体的高度如何取得
- 2、java中如何实现向已有的PDF文件插入附件?
- 3、如何用Java编写一个绘制图形的小程序?
- 4、Java中pack和Dimension的用法
- 5、java中用户图形界面的创建需要导入那几个包,各个包的用处有哪些?
Java中字体的高度如何取得
可以使用:java.awt.Font类的getStringBounds函数,参考代码如下(57行):
package test;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Program {
public static void main(String[] args) {
try {
getImage(Font.BOLD);
System.out.println("finished.");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void getImage(int fontStyle)
throws FileNotFoundException, IOException {
// 得到图片缓冲区
int width = 100;
int height = 50;
int imageType = BufferedImage.TYPE_INT_BGR;
BufferedImage targetImage = new BufferedImage(width, height, imageType);
// 得到画笔
Graphics graphics = targetImage.getGraphics();
// 设置背景色为白色
graphics.setColor(Color.WHITE);
// 画出一个矩形
// 坐标x 坐标y 宽度100 长度50
graphics.fillRect(0, 0, width, height);
// 微软雅黑 粗体显示 大小25
Font font = new Font("微软雅黑", fontStyle, 25);
graphics.setFont(font);
// 设置字的颜色 和 背景的颜色 要不同的
graphics.setColor(Color.RED);
int x = 20, y = 25;
// 写字
graphics.drawString("中文", 20, 35);
// 获取到文字区域大小
Rectangle2D rect = font.getStringBounds("中文", ((Graphics2D)graphics).getFontRenderContext());
// 绘制方框将文字圈起来
graphics.drawRect(x, (int)(y - (rect.getHeight() / 2)), (int)rect.getWidth(), (int)rect.getHeight());
ImageIO.write(targetImage, "JPEG", new FileOutputStream("E:\\" + fontStyle + ".jpg"));
}
}
圈起来的文字如下:
java中如何实现向已有的PDF文件插入附件?
可以用Spire.Pdf for Java类库给PDF文档添加附件,下面的代码是插入Excel和Word附件给你参考:
import com.spire.pdf.annotations.*;
import com.spire.pdf.attachments.PdfAttachment;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class AttachFiles {
public static void main(String[] args) throws IOException {
//创建PdfDocument对象
PdfDocument doc = new PdfDocument();
//加载PDF文档
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//添加附件到PDF
PdfAttachment attachment = new PdfAttachment("C:\\Users\\Administrator\\Desktop\\使用说明书.docx");
doc.getAttachments().add(attachment);
//绘制标签
String label = "财务报表.xlsx";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,12),true);
double x = 35;
double y = doc.getPages().get(0).getActualSize().getHeight() - 200;
doc.getPages().get(0).getCanvas().drawString(label, font, PdfBrushes.getOrange(), x, y);
//添加注释附件到PDF
String filePath = "C:\\Users\\Administrator\\Desktop\\财务报表.xlsx";
byte[] data = toByteArray(filePath);
Dimension2D size = font.measureString(label);
Rectangle2D bound = new Rectangle2D.Float((float) (x + size.getWidth() + 2), (float) y, 10, 15);
PdfAttachmentAnnotation annotation = new PdfAttachmentAnnotation(bound, filePath, data);
annotation.setColor(new PdfRGBColor(new Color(0, 128, 128)));
annotation.setFlags(PdfAnnotationFlags.Default);
annotation.setIcon(PdfAttachmentIcon.Graph);
annotation.setText("点击打开财务报表.xlsx");
doc.getPages().get(0).getAnnotationsWidget().add(annotation);
//保存文档
doc.saveToFile("Attachments.pdf");
}
//读取文件到byte数组
public static byte[] toByteArray(String filePath) throws IOException {
File file = new File(filePath);
long fileSize = file.length();
if (fileSize Integer.MAX_VALUE) {
System.out.println("file too big...");
return null;
}
FileInputStream fi = new FileInputStream(file);
byte[] buffer = new byte[(int) fileSize];
int offset = 0;
int numRead = 0;
while (offset buffer.length (numRead = fi.read(buffer, offset, buffer.length - offset)) = 0) {
offset += numRead;
}
if (offset != buffer.length) {
throw new IOException("Could not completely read file "
+ file.getName());
}
fi.close();
return buffer;
}
}
效果:
如何用Java编写一个绘制图形的小程序?
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
//不规则图形的绘制
public class IrregularShapeDemo extends JFrame {
GeneralPath gPath= new GeneralPath(); //GeneralPath对象实例
Point aPoint;
//构造函数
public IrregularShapeDemo() {
super("不规则图形的绘制"); //调用父类构造函数
enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允许事件
setSize(300, 200); //设置窗口尺寸
setVisible(true); //设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
public void paint(Graphics g) { //重载窗口组件的paint()方法
Graphics2D g2D = (Graphics2D)g;//获取图形环境
g2D.draw(gPath); //绘制路径
}
public static void main(String[] args) {
new IrregularShapeDemo();
}
protected void processMouseEvent(MouseEvent e) { //鼠标事件处理
if(e.getID() == MouseEvent.MOUSE_PRESSED) {
aPoint = e.getPoint(); //得到当前鼠标点
gPath = new GeneralPath(); //重新实例化GeneralPath对象
gPath.moveTo(aPoint.x,aPoint.y); //设置路径点
}
}
protected void processMouseMotionEvent(MouseEvent e) { //鼠标运动事件处理
if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
aPoint = e.getPoint(); //得到当前鼠标点
gPath.lineTo(aPoint.x, aPoint.y); //设置路径
gPath.moveTo(aPoint.x, aPoint.y);
repaint(); //重绘组件
}
}
}
Java中pack和Dimension的用法
pack() - 类 java.awt.Window 中的方法;
用于调整此窗口的大小,以适合其子组件的首选大小和布局。
1. pack(JarFile, OutputStream) - 接口 java.util.jar.Pack200.Packer 中的方法
接收 JarFile 并将其转换为 Pack200 存档。
2. pack(JarInputStream, OutputStream) - 接口 java.util.jar.Pack200.Packer 中的方法
接收 JarInputStream 并将其转换成 Pack200 存档。
3.pack() - 类 javax.swing.JInternalFrame 中的方法
使此 JInternalFrame 的子组件按其首选大小进行布局。
pack() - 类 javax.swing.JPopupMenu 中的方法
布置容器,让它使用显示其内容所需的最小空间。
Dimension 类封装单个对象中组件的宽度和高度(精确到整数)。该类与组件的某个属性关联。由 Component 类和 LayoutManager 接口定义的一些方法将返回 Dimension 对象。通常,width 和 height 的值是非负整数。允许创建 dimension 的构造方法不会阻止您为这些属性设置负值。如果 width 或 height 的值为负,则由其他对象定义的一些方法的行为是不明确的。
java中用户图形界面的创建需要导入那几个包,各个包的用处有哪些?
java.awt 重量级组件,颜色,布局管理器等
java.awt.event 事件处理
javax.swing 轻量级组件
javax.swing.event 事件处理
javax.swing.table JTable相关
javax.swing.tree JTree相关
javax.swing.text 文本组件相关
java.awt.image 图形类
java.awt.geom Java2D相关
javax.imageio 图像
java中geom的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java中geometry、java中geom的信息别忘了在本站进行查找喔。