通过java2dAPI绘制报表
/*
*只画直线,矩形,椭圆,只能向右下角画
*
*PainterPanel extends JPanel implements MouseListener
*addMouseListener(this);
*
*JToggleButton
*
*hxz*/
import java.awt.*;
import java.awt.event.*;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.*;
class MyPainterPanel extends JPanel implements MouseListener{
Point[] point=new Point[2]; //记录鼠标拖动的起始点和终点
int max_width = 0;
int max_height = 0;
Map<String,Integer> girl_data;
Map<String,Integer> boy_data;
Color girl_color;
Color boy_color;
String title;
int title_height = 14;
int[] title_padding = {10,0,10,0};
public MyPainterPanel(){
super(); //调用父类构造函数
this.setBackground(Color.white); //设置背景颜色
point[0]=new Point(0,0); //初始化变量
point[1]=new Point(300,300);
addMouseListener(this); //增加鼠标事件
this.max_width = 600;
this.max_height = 500;
}
public MyPainterPanel(int width, int height){
super(); //调用父类构造函数
this.setBackground(Color.white); //设置背景颜色
point[0]=new Point(0,0); //初始化变量
point[1]=new Point(300,300);
addMouseListener(this); //增加鼠标事件
this.max_width = width;
this.max_height = height;
System.out.println(width+"="+height);
}
public void mouseReleased(MouseEvent e){ //鼠标释放事件
point[1]=new Point(e.getX(),e.getY()); //设置终点位置
//repaint(); //重绘屏幕
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){ //鼠标按下时事件
point[0]=new Point(e.getX(),e.getY()); //设置起始点位置
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
this.max_width = this.getWidth();
this.max_height = this.getHeight();
//MyGraphicsData m = new MyGraphicsData((Graphics2D) g);
//m.truemode(0, 0, 600, 500, 10.0, 10.0, 590.0,490.0);
g2d.setFont(new Font("DialogInput", Font.BOLD, 12));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
// g2d.drawLine(point[0].x,point[0].y,point[1].x,point[1].y); //绘线
// int width=point[1].x-point[0].x;
// int height=point[1].y-point[0].y;
// g2d.drawOval(point[0].x,point[0].y,width,height); //绘椭圆
// width=point[1].x-point[0].x;
// height=point[1].y-point[0].y;
// g2d.drawRect(point[0].x,point[0].y,width,height); //绘矩形
// g2d.drawLine(0,max_height-30,max_width,max_height-30);
// g2d.drawLine(0,0,max_width,max_height);
g2d.setColor (Color.GREEN);
Point p1 = new Point(0,max_height-30);
Point p2 = new Point(max_width,max_height-30);
g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
//g2d.drawString("我靠", 150, 150);
g2d.setColor (Color.yellow);
//g2d.fillArc (5, 15, 50, 75, 25, 165);
//g2d.fill3DRect(250, 250, 100, 100, true);
// 设置渐进色
GradientPaint gp = new GradientPaint(180, 190, Color.yellow, 220, 210,
Color.red, true);
g2d.setPaint(gp);
g2d.setStroke(new BasicStroke(2.0f)); //设定粗细
Line2D.Float line = new Line2D.Float(20, 300, 200, 300); //直线
g2d.draw(line);
// 显示文字
FontRenderContext frc = g2d.getFontRenderContext();
TextLayout tl = new TextLayout("测试文字", new Font("Modern", Font.BOLD
+ Font.ITALIC, 20), frc);
//线形
Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(50, 180));
g2d.setColor(Color.blue);//颜色
g2d.setStroke(new BasicStroke(2.0f));//粗细
g2d.draw(sha);
g2d.setColor(Color.white);
g2d.fill(sha);
g2d.setColor(Color.red);
//旋转
AffineTransform oldTransform = g2d.getTransform();
//旋转
AffineTransform tr = new AffineTransform();
//使用一个translation变换为要旋转的文本设置空间
tr.setToTranslation(100.0 , 100.0);
g2d.transform(tr);
//创建一个旋转变换来旋转文本
tr.setToRotation(Math.PI / 2.0);
//在90度角的位置渲染四个字符串"Java"的复件
for ( int i = 0; i < 4; i++){
g2d.drawString("Java", 0.0f , 0.0f);
g2d.transform(tr);
}
g2d.transform(AffineTransform.getRotateInstance(Math.PI * (0.50f)));
g2d.draw(sha);
g2d.drawString("12505555555555", 0 , 0);
g2d.setTransform(oldTransform);
float[] elements = { 0.0f, -1.0f, 0.0f, -1.0f, 4.f, -1.0f, 0.0f, -1.0f,
0.0f };
try{
//加载图片
Image img = Toolkit.getDefaultToolkit().getImage("");
int w = img.getWidth(this);
int h = img.getHeight(this);
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
//创建Graphics2D对象
Graphics2D big = bi.createGraphics();
big.drawImage(img, 0, 0, this);
//阴影处理
BufferedImageOp biop = null;
AffineTransform at = new AffineTransform();
BufferedImage bimg = new BufferedImage(img.getWidth(this), img.getHeight(this), BufferedImage.TYPE_INT_RGB);
Kernel kernel = new Kernel(3, 3, elements);
ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
cop.filter(bi, bimg);
biop = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
//显示图像
g2d.drawImage(bimg, biop, 0, 0);
}catch(Exception e){
System.out.println(e);
}
//add_backgroud_image(g2d);
this.draw_coord(g2d);
System.out.println(this.getWidth()+" "+this.getHeight());
//g.draw3DRect(50, 50, max_width, 5, false);
g.setColor(Color.BLUE);
g.fill3DRect(50, 50, max_width, 5, false);
}
public void draw_coord(Graphics2D g){
g.setColor(Color.BLACK);
//下面坐标
g.drawLine(0,max_height-30,max_width,max_height-30);
for(int i=0; i<=30; i++){
Stroke dash = new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND,
3.5f,new float[]{5,3},0f);
g.setStroke(dash);
if(i*(max_width/30)<(max_width/2-20) || i*(max_width/30)>(max_width/2+20))
g.drawLine(i*(max_width/30),0, i*(max_width/30), max_height-25);
Stroke dash2 = new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
g.setStroke(dash2);
AffineTransform oldTransform = g.getTransform();
//旋转
AffineTransform tr = new AffineTransform();
Font font = g.getFont();
Dimension d = font.createGlyphVector(g.getFontRenderContext(),""+(i*(max_width/30))).getVisualBounds().getBounds().getSize();
//使用一个translation变换为要旋转的文本设置空间
tr.setToTranslation(i*(max_width/30) , max_height-d.getWidth());
g.transform(tr);
g.transform(AffineTransform.getRotateInstance(Math.PI * (0.50f)));
g.drawString(""+(i*(max_width/30)), 0, 0);
//g.drawString(""+(i*(max_width/30)), i*(max_width/30), max_height-g.getFont().getSize());
g.setTransform(oldTransform);
}
g.setColor(Color.BLACK);
g.setFont(new Font("Dialog", Font.PLAIN, 12));
//中间坐标
g.drawLine(max_width/2-20, 0, max_width/2-20, max_height-30);
for(int i=0; i<10; i++){//中轴刻度
g.drawString(""+(i*10)+"-"+(i*10+10), max_width/2-20, i*(max_height-30)/10);
//绘制坐标虚线
//g.drawLine(0, i*(max_height-30)/10, max_width/2-20, i*(max_height-30)/10);
//g.drawLine(max_width/2+20, i*(max_height-30)/10, max_width, i*(max_height-30)/10);
g.setColor(Color.YELLOW);
int rand_left_x = new Random().nextInt(max_width/2-20);
g.fill3DRect(rand_left_x, i*(max_height-30)/10, max_width/2-20-rand_left_x, 20, true);
g.setColor(Color.GREEN);
GradientPaint gp = new GradientPaint(180,190,Color.GREEN,220,120,Color.YELLOW,true);
g.setPaint(gp);
g.fill3DRect(max_width/2+20, i*(max_height-30)/10, new Random().nextInt(max_width/2-20), 20, true);
g.setColor(Color.BLACK);
}
g.setColor(Color.BLACK);
g.drawLine(max_width/2+20, 0, max_width/2+20, max_height-30);
}
public void add_backgroud_image(Graphics2D g){
//设置为以透明方式绘制
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float).75);
g.setComposite(ac);
Image img = Toolkit.getDefaultToolkit().getImage("E:\\firefox\\后台模板\\admin-templates\\images\\login-wel.gif");
g.drawImage(img, 0, 0, this);
}
}
/*
*JToggleButton
*
*JToolBar
*
*hxz*/
public class MyPainterDemo extends JFrame{
JToggleButton[] button=new JToggleButton[3]; //按钮组
MyPainterPanel painter=new MyPainterPanel(); //绘图面板
public MyPainterDemo(){
super("Java画图程序"); //调用父类构造函数
String[] buttonName={"保存","重画","关闭"}; //按钮文字
DrawShapeListener buttonListener=new DrawShapeListener(); //按钮事件
JToolBar toolBar=new JToolBar(); //实例化工具栏
ButtonGroup buttonGroup=new ButtonGroup(); //实例化按钮组
for (int i=0;i<button.length;i++){
button[i]=new JToggleButton(buttonName[i]); //实例化按钮
button[i].addActionListener(buttonListener); //增加按钮事件处理
buttonGroup.add(button[i]); //增加按钮到按钮组
toolBar.add(button[i]); //增加按钮到工具栏
}
Container container=getContentPane(); //得到窗口容器
container.add(toolBar,BorderLayout.NORTH); //增加组件到容器上
//painter = new MyPainterPanel(container.getMaximumSize().width,container.getMaximumSize().height);
container.add(painter,BorderLayout.CENTER);
System.out.println(getContentPane().getBounds().height);
setSize(700,650); //设置窗口尺寸
//setResizable(false);
Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
int splash_x=(int)((screensize.width-getWidth())/2);
int splash_y=(int)((screensize.height-getHeight())/2);
setBounds(splash_x,splash_y,getWidth(),getHeight());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
setVisible(true); //设置窗口为可视
}
//内部类
class DrawShapeListener implements ActionListener{ //按钮事件处理
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button[0]) { // 判断来自于哪个按钮
Component c = painter;
BufferedImage bi = (BufferedImage) c.createImage(c.getWidth(),
c.getHeight());
c.paint(bi.getGraphics());
try {
File directory = new File(".");
javax.imageio.ImageIO.write(bi, "bmp", new File(directory
.getCanonicalPath()
+ "/gant.bmp"));
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (e.getSource() == button[1]) { // 判断来自于哪个按钮
painter.repaint();
}
if (e.getSource() == button[2]) { // 判断来自于哪个按钮
System.exit(0);
}
}
}
public static void main(String[] args){
new MyPainterDemo();
}
}
// 使用抗混叠技术,即平滑形状和文本的粗超像素边缘
// g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//
// int cx = getSize().width / 2;
// int cy = getSize().height / 2;
//
// //移动到画面中心
// g2.translate(cx,cy);
// //使画面旋转
// g2.rotate(theta * Math.PI / 180);
//
// //保留旧的剪裁形状
// Shape oldClip = g2.getClip();
// //创建一个椭圆形状
// Shape e = new Ellipse2D.Float(-cx, -cy, cx*2 ,cy*2);
// //设置剪裁形状为一个椭圆
// g2.clip(e);
// //画一个椭圆
// Shape c = new Ellipse2D.Float(-cx, -cy, cx*3/4 ,cy*2);
// g2.setPaint(new GradientPaint(40, 40, Color.blue, 60, 50, Color.white, true));
// g2.fill(c);
// //画一个椭圆
// g2.setPaint(Color.yellow);
// g2.fillOval(cx/4, 0, cx, cy);
// //设置剪裁形状为旧的剪裁形状
// g2.setClip(oldClip);
// //绘制文本
// g2.setFont(new Font("Times New Roman", Font.PLAIN, 64));
// g2.setPaint(new GradientPaint(-cx, 0, Color.red, cx, 0, Color.black, false));
// g2.drawString("Hello, 2D!", -cx*3/4, cy/4);
// //设置为以透明方式绘制
// AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float).75);
// g2.setComposite(ac);
//计算字体的长度
// private Dimension getBounds(Graphics2D g2 , String s) {
// Font font = g2.getFont();
// return font.createGlyphVector(g2.getFontRenderContext(),s).getVisualBounds().getBounds().getSize();
//}
// Font f = new Font( "楷体_GB2312 ", Font.PLAIN, 14);
// TextLayout tl = new TextLayout( "文本 ", f, new FontRenderContext(null, false, false));
// int width = (int) tl.getBounds().getWidth();
// int height = (int) (tl.getAscent()+tl.getDescent());
//图片处理
// 读入图片
// File _file = new File(file1);
// Image src = javax.imageio.ImageIO.read(_file); //构造Image对象
// int width = src.getWidth(null); //得到图宽
// int height = src.getHeight(null); //得到图长
//
// //图片放缩
// BufferedImage tag = new BufferedImage(width / 2, height / 2,
// BufferedImage.TYPE_INT_RGB);
// tag.getGraphics().drawImage(src, 0, 0, width / 2, height / 2, null); //绘制缩小后的图
通过java2dAPI绘制报表相关推荐
- [转] C# 绘制报表,使用Graphics.DrawString 方法
原文 Graphics.DrawString 方法 在指定位置并且用指定的 Brush 和Font 对象绘制指定的文本字符串. public void DrawString(string s,Font ...
- 基于wxcharts绘制报表
前言 微信小程序图表插件(wx-charts)是基于canvas绘制,体积小巧,支持图表类型饼图.线图.柱状图 .区域图等图表图形绘制,目前wx-charts是微信小程序图表插件中比较强大好使的一个. ...
- •Grid报表和Free From报表产品之比较
目前市场上面的报表产品从界面上分为两种:一种是以Grid界面展现的,另一种是Free Form控件形式展现的,超级报表使用的是Grid形式的.这两种类型的报表各自有各自的优点和缺点,下面是对他们的优缺 ...
- java实现续打功能_浅谈报表工具的打印方案
报表工具除了基本的绘制报表,完成数据呈现(也就是让终端用户能看到报表),打印也是报表的基本功能之一. 当前报表工具很多,可以按地域.开闭源等划分,包括有杰表.水晶.Style Report.birt. ...
- [转]简单介绍如何用Reporting Service制作报表
1) 在安装好Reporting Service后,打开Visual studio .net ,选择新建项目,选择建立商业智能项目,选择新建一个报表项目,命名为reportservice1,再选 ...
- 微软SQL 报表服务的研究
微软SQL 报表服务的研究 上次发布的关于国内报表设计器的分析,很多看客说遗漏了微软的报表服务,本人经过一段时间的补充学习,将本人得出的浅淡的研究说一下,本人是初学者,其中观点可能错误,望大家不吝赐教 ...
- matplotlib 横坐标只显示整数_面试题系列 (168) matplotlib条形图绘制
之前面试被问到的图表绘制问题,因为涉及项目的时候,经常会做一些报表之类的,那么这样的数据统计图表绘制报表该怎么做,需要用到哪些技术呢?接下来几篇面试文章将会讲解一些常见图表绘制工具与用法,比如matp ...
- 报表中如何实现不规则布局
报表,我们习惯于认为主要用来呈现数据汇总.排序.分组对比的效果,一般都是"横平竖直"的效果,也就是我们常说的二维表格,最多也就是通过一些斜线.合并表示一些特殊的数据关系.事实上,报 ...
- 水晶报表填充.Net Objects数据源
Crystal Reports(水晶报表)是一款商务智能(BI)软件,主要用于设计及产生报表.是业内最专业.功能最强的报表系统. 查看网络资料及课本图书,鲜有介绍通过.NET Objects作为数据源 ...
最新文章
- 分享一款jQuery全屏滚动页面特性案例
- Python 开源项目大集合,跨 15 个领域,181 个项目
- seaborn系列 (5) | 柱状图countplot()
- 『物理社工』五一回家的“小插曲”
- 将前端文件和Go程序打包
- 基于matlab地球物理,基于MATLAB的《地球物理资料处理和解释》教学研究
- Algorithm——1.排序.md
- Vue提供操作DOM的方法
- 奔驰宝马路虎等齐降价 最高降幅达8.5万!
- 试点高校网络教育部分公共基础课统一考试计算机应用基础答案,2019年6月试点高校网络教育部分公共基础课统一考试顺利结束...
- jmeter 计数器_jmeter函数(六)
- 洛谷 P2420 让我们异或吧 解题报告
- Matplotlib笔记(1)——文字、绘图、背景(颜色对照表)
- 3月20 Ubuntu里运行脚本示例
- rpm安装mysql指定数据仓库_linux(center OS7)安装JDK、tomcat、mysql 搭建java web项目运行环境-Go语言中文社区...
- 详细介绍如何在华为云调用SDK的Python代码(以文字识别OCR技术实现身份证识别为例)
- 音频硬件发展史,以及DSD是如何产生的 一起说一说音频
- STM32定时器周期任务函数编写
- 自然辩证法(研究生)期末考试题库
- 设计模式-Facade门面-Mediator调停者
热门文章
- 微医网爬虫(二) java实现
- Redis 大键值对 Big Key 懒惰删除机制
- 论文解读:Semantic Neural Machine Translation using AMR
- 物联网之STM32开发一(基础知识)
- 一句话木马 php asp,一句话木马源代码
- webgis技术在智慧城市综合治理网格化社会管理平台(综治平台)的应用
- 少年中国说——梁启超〔近现代〕
- 黑马程序员————高新技术————JDK1.5新特性
- 计算机无法识别硬盘怎么办,硬盘电脑不识别怎么办?硬盘数据怎么恢复?
- 一个程序员的日常书单(更新于2018/10/7)