Java究竟怎么玩?

天地程序已定棋,人间大数待变局

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 
   本来上周双休日没什么事情,准备干点遗留了很久的正经事,结果半途忍不住手贱看新闻,于是被11.28事件触动,随大流的针对棒子及棒子粉抽疯了两天。

   没想到到星期一上班脖子就不得劲,顺便查了点资料,发现个山寨版的颈椎矫正图,觉得挺有意思。

   如下图:

  

     于是回家后想到自己也做个玩。

     问题是,咱爷们不说程序员吧,好歹也是个垒码的,直接PS文字图未免有碍观瞻,于是抽空写了个Java自动生成版的。

     代码如下:

  1. package org.test;

  2. import java.awt.AlphaComposite;
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import java.awt.Frame;
  7. import java.awt.Graphics;
  8. import java.awt.Graphics2D;
  9. import java.awt.Image;
  10. import java.awt.RenderingHints;
  11. import java.awt.event.WindowAdapter;
  12. import java.awt.event.WindowEvent;
  13. import java.awt.image.BufferedImage;
  14. import java.io.File;
  15. import java.io.IOException;

  16. import javax.imageio.ImageIO;

  17. /**
  18.  * Copyright 2008
  19.  * 
  20.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  21.  * use this file except in compliance with the License. You may obtain a copy of
  22.  * the License at
  23.  * 
  24.  * http://www.apache.org/licenses/LICENSE-2.0
  25.  * 
  26.  * Unless required by applicable law or agreed to in writing, software
  27.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  28.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  29.  * License for the specific language governing permissions and limitations under
  30.  * the License.
  31.  * 
  32.  * @project loonframework
  33.  * @author chenpeng
  34.  * @email:ceponline@yahoo.com.cn
  35.  * @version 0.1
  36.  */
  37. public class MessageImage extends Canvas {

  38.     /**
  39.      * 
  40.      */
  41.     private static final long serialVersionUID = 1L;

  42.     private BufferedImage fontImage;

  43.     private Graphics2D g2d;
  44.     
  45.     private Image backImage;
  46.     
  47.     final static private int WIDTH = 600;

  48.     final static private int HEIGHT = 480;


  49.     public MessageImage(final String messages) {

  50.         fontImage = new BufferedImage(WIDTH, HEIGHT, 2);
  51.         g2d = fontImage.createGraphics();
  52.         try {
  53.             backImage=ImageIO.read(new File("back.png"));
  54.         } catch (IOException e) {
  55.             e.printStackTrace();
  56.         }
  57.         g2d.drawImage(backImage, 00null);
  58.         setAlpha(g2d, 0.7);
  59.         int size = 25;
  60.         int newLine = (WIDTH / size) - 10;
  61.         char[] messageChars = messages.toCharArray();
  62.         boolean d = true;
  63.         StringBuilder sbr = new StringBuilder();
  64.         int count = 0;
  65.         int len = messageChars.length - 1;
  66.         String fontStyle = "幼圆";
  67.         Color color = Color.white;
  68.         for (int i = 0, j = 0; i <= len; i++, j++) {
  69.             sbr.append(messageChars[i]);
  70.             if (j == newLine || (messageChars[i] == '/n')) {
  71.                 g2d.drawImage(createImageMessages(1false, sbr.toString(),
  72.                         color, fontStyle, 1, size, d), count += 30,
  73.                         (HEIGHT - (sbr.length() * size)) - (size * 4), null);
  74.                 d = !d;
  75.                 sbr.delete(0, sbr.length());
  76.                 j = 0;
  77.             } else if (i == len) {
  78.                 g2d.drawImage(createImageMessages(1false, sbr.toString(),
  79.                         color, fontStyle, 1, size, d), count += 30,
  80.                         (HEIGHT - (sbr.length() * size)) - (size * 4), null);
  81.             }
  82.         }
  83.         setAlpha(g2d, 0.6);
  84.         String mes = "Java版颈椎自动矫正图";
  85.         fontStyle = "华文新魏";
  86.         size = 20;
  87.         g2d.drawImage(createImageMessages(0true, mes, Color.red, fontStyle,
  88.                 1, size, false), WIDTH - (mes.length() * size) - (size * 2),
  89.                 HEIGHT - (size * 2), null);
  90.     }

  91.     /**
  92.      * 创建一组图片文字
  93.      * 
  94.      * @param aspect
  95.      * @param isRow
  96.      * @param messages
  97.      * @param color
  98.      * @param name
  99.      * @param style
  100.      * @param size
  101.      * @param d
  102.      * @return
  103.      */
  104.     public static BufferedImage createImageMessages(final int aspect,
  105.             final boolean isRow, final String messages, final Color color,
  106.             final String name, final int style, final int size, final boolean d) {
  107.         final int flength = messages.length();
  108.         final int nowSize = size * flength;
  109.         BufferedImage fontImages;
  110.         if (isRow)
  111.             fontImages = new BufferedImage(nowSize, size, 2);
  112.         else
  113.             fontImages = new BufferedImage(size, nowSize, 2);
  114.         Graphics2D graphics2d = fontImages.createGraphics();
  115.         char[] messageChars = messages.toCharArray();
  116.         if (d) {
  117.             char[] temp = new char[flength];
  118.             for (int i = 0, j = flength - 1; i < flength; i++, j--) {
  119.                 temp[j] = messageChars[i];
  120.             }
  121.             messageChars = temp;
  122.         }
  123.         if (isRow)
  124.             for (int i = 0; i < flength; i++) {
  125.                 graphics2d.drawImage(createImageMessage(aspect,
  126.                         messageChars[i], color, name, style, size, d),
  127.                         i * size, 0null);
  128.             }
  129.         else
  130.             for (int i = 0; i < flength; i++) {
  131.                 graphics2d.drawImage(createImageMessage(aspect,
  132.                         messageChars[i], color, name, style, size, d), 0, i
  133.                         * size, null);
  134.             }
  135.         graphics2d.dispose();
  136.         System.gc();
  137.         return fontImages;
  138.     }

  139.     /**
  140.      * 创建单独图片文字
  141.      * 
  142.      * @param aspect
  143.      * @param message
  144.      * @param color
  145.      * @param name
  146.      * @param style
  147.      * @param size
  148.      * @param d
  149.      * @return
  150.      */
  151.     public static BufferedImage createImageMessage(final int aspect,
  152.             final char message, final Color color, final String name,
  153.             final int style, final int size, final boolean d) {
  154.         final int nowSize = size + 1;
  155.         BufferedImage fontImage = new BufferedImage(nowSize, nowSize, 2);
  156.         Graphics2D graphics2d = fontImage.createGraphics();
  157.         graphics2d.setColor(color);
  158.         graphics2d.setFont(new Font(name, style, size));
  159.         // 设定图像显示状态
  160.         RenderingHints hints = new RenderingHints(
  161.                 RenderingHints.KEY_TEXT_ANTIALIASING,
  162.                 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  163.         hints.put(RenderingHints.KEY_DITHERING,
  164.                 RenderingHints.VALUE_DITHER_ENABLE);
  165.         hints.put(RenderingHints.KEY_RENDERING,
  166.                 RenderingHints.VALUE_RENDER_SPEED);
  167.         hints.put(RenderingHints.KEY_ANTIALIASING,
  168.                 RenderingHints.VALUE_ANTIALIAS_ON);
  169.         hints.put(RenderingHints.KEY_FRACTIONALMETRICS,
  170.                 RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  171.         hints.put(RenderingHints.KEY_COLOR_RENDERING,
  172.                 RenderingHints.VALUE_COLOR_RENDER_QUALITY);
  173.         hints.put(RenderingHints.KEY_DITHERING,
  174.                 RenderingHints.VALUE_DITHER_DISABLE);
  175.         graphics2d.setRenderingHints(hints);
  176.         graphics2d.drawString(String.valueOf(message), 0, size - 3);
  177.         graphics2d.dispose();
  178.         switch (aspect) {
  179.         case 0:
  180.             break;
  181.         case 1:
  182.             fontImage = MessageImage.rotateImage(fontImage, 90, d);
  183.             break;
  184.         case 2:
  185.             fontImage = MessageImage.rotateImage(fontImage, 180, d);
  186.             break;
  187.         case 3:
  188.             fontImage = MessageImage.rotateImage(fontImage, 360, d);
  189.             break;
  190.         }
  191.         return fontImage;
  192.     }

  193.     public void paint(Graphics g) {
  194.         g.drawImage(fontImage, 00null);
  195.     }

  196.     /**
  197.      * 水平翻转当前图像
  198.      * 
  199.      * @return
  200.      */
  201.     public static BufferedImage rotateImage(final BufferedImage image) {
  202.         int w = image.getWidth();
  203.         int h = image.getHeight();
  204.         BufferedImage img;
  205.         Graphics2D graphics2d;
  206.         (graphics2d = (img = new BufferedImage(w, h, image.getColorModel()
  207.                 .getTransparency())).createGraphics()).drawImage(image, 00,
  208.                 w, h, w, 00, h, null);
  209.         graphics2d.dispose();
  210.         return img;
  211.     }

  212.     /**
  213.      * 旋转图像为指定角度
  214.      * 
  215.      * @param degree
  216.      * @return
  217.      */
  218.     public static BufferedImage rotateImage(final BufferedImage image,
  219.             final int angdeg, final boolean d) {
  220.         int w = image.getWidth();
  221.         int h = image.getHeight();
  222.         int type = image.getColorModel().getTransparency();
  223.         BufferedImage img;
  224.         Graphics2D graphics2d;
  225.         (graphics2d = (img = new BufferedImage(w, h, type)).createGraphics())
  226.                 .setRenderingHint(RenderingHints.KEY_INTERPOLATION,
  227.                         RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  228.         graphics2d.rotate(d ? -Math.toRadians(angdeg) : Math.toRadians(angdeg),
  229.                 w / 2, h / 2);
  230.         graphics2d.drawImage(image, 00null);
  231.         graphics2d.dispose();
  232.         return img;
  233.     }

  234.     final static public void setAlpha(final Graphics2D g, final double d) {
  235.         AlphaComposite alphacomposite = AlphaComposite
  236.                 .getInstance(3, (float) d);
  237.         g.setComposite(alphacomposite);
  238.     }

  239.     public static void main(String[] args) {
  240.         java.awt.EventQueue.invokeLater(new Runnable() {
  241.             public void run() {
  242.                 Frame frame = new Frame();
  243.                 frame.addWindowListener(new WindowAdapter() {
  244.                     public void windowClosing(WindowEvent e) {
  245.                         System.exit(0);
  246.                     }
  247.                 });
  248.                 frame
  249.                         .add(new MessageImage(
  250.                                 "每天面对计算机/n不是垒码、就是泡论坛、再不然就是和脑残作斗争,爆个哈韩吧,攻击个棒子网什么的,再闲时就写写Blog,转转Google,"
  251.                                         + "总之24小时离不开电脑,离不开网络(已被纳入我国精神病症状|||),长时间不活动,这颈椎可怎么受的了?没办法,想点办法解决吧。"
  252.                                         + "最近有人说图片能治疗颈椎病,我却偏不信那个邪,就几句话能费人多大的力气去看?说到底还真能累死活人不成?"
  253.                                         + "这左转右绕上窜下跳的,除了费点眼睛,怎么可能把脖子一块运动起来?您说是不是这道理?"));
  254.                 frame.setSize(WIDTH + 5, HEIGHT + 25);
  255.                 frame.setResizable(false);
  256.                 frame.setTitle("Java版颈椎自动矫正图");
  257.                 frame.setLocationRelativeTo(null);
  258.                 frame.setVisible(true);
  259.             }
  260.         });
  261.     }

  262. }



     效果图如下:
      
    
         
     仔细说起来,这就和各大小说门户生成图片版小说的原理一般无二,毫无技术难度可言,不过毕竟写了,也就丢出来在Blog凑个数。

     那位有时间的话(或者等以后鄙人闲的没事),可以做个自动导出,弄套资治通鉴全文之类的图片做颈椎矫正,试验下连续的看完整本颈椎是轻松了,还是碎掉了……


    
   
posted on 2008-12-01 20:04  cping  阅读(310)  评论(0编辑  收藏  举报