日期:2014-05-20  浏览次数:20633 次

怎么生成一个圆并保存为图片文件?
我需要后台程序(Action)自动生成一个半径为20圆,并保存为图片文件(如BMP文件)
请问怎么做?

注意是后台程序(SSH框架的ACtion类)中?

------解决方案--------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class SaveImage extends JFrame
{
 ImagePanel panel;
 public SaveImage()
 {
  panel = new ImagePanel();
  this.setContentPane(panel);
     this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
     this.setTitle("");
     this.setSize(400,400);
     this.setVisible(true);
 }
 
 public static void main(String args[])
    {
     new SaveImage();
    }
}

class ImagePanel extends JPanel
{
 BufferedImage bi;
 public ImagePanel()
 {
  bi = new BufferedImage(400,400,BufferedImage.TYPE_INT_RGB);
  Graphics gg = bi.getGraphics();
  gg.setColor(Color.red);
  gg.drawRect(10,10,50,50);
  gg.setColor(Color.blue);
  gg.fillOval(50,50,80,80);
  gg.dispose();  
 }
 public void paintComponent(Graphics g)
 {
  super.paintComponent(g);
  g.drawImage(bi,0,0,this);
  saveImage(); 
 }
 
 public void saveImage()
 {
  try
  {
       ImageIO.write(bi,"jpg",new File("test.jpg"));
     }
     catch(Exception e){e.printStackTrace();}
 }
}
------解决方案--------------------
下班,先不想了,给你个思路。。你可以参考,后台生成验证码,这个也是用画笔画出来的,同样的可以画个圆,接下来再保存成bmp文件就简单了
------解决方案--------------------

        int width = 60;
        int height = 60;
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        g2.setBackground(Color.WHITE);
        g2.clearRect(0, 0, width, height);
        g2.setPaint(Color.BLACK);
        g2.drawArc(10, 10, 40, 40, 0, 360);

        ImageIO.write(bi, "bmp", new File("输出路径/image.bmp"));