首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 企业软件 > 行业软件 >

透过java2dAPI绘制报表

2012-08-26 
通过java2dAPI绘制报表??? /* ??? *只画直线,矩形,椭圆,只能向右下角画 ??? * ??? *PainterPanel extends

通过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); //绘制缩小后的图

热点排行