Java 画图

弄了一天终于弄明白怎么画图,保存图片了。

画出图片,然后保存

这种方法好处就是可以看到自己画的图片,不好的地方就是速度慢

public class Draw extends JFrame
{
    MyPanel mp = null ;

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        Draw qwe = new Draw();
       BufferedImage bi = new BufferedImage(qwe.getWidth(), qwe.getHeight(), BufferedImage.TYPE_INT_ARGB);
        BufferedImage bi1=bi.getSubimage(10,10,qwe.getWidth()-50, qwe.getHeight()-50);
        Graphics2D g2d =  bi1.createGraphics();
        qwe.paint(g2d);
        ImageIO.write(bi1, "PNG", new File("G:/Closed source Code/ClosedQr20171220.1/sysu.jpg"));
    }

    public Draw()
    {
        mp = new MyPanel();
        this.add(mp);
        this.setSize(1000,1000);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

class MyPanel extends JPanel    //我自己的面板,用于绘图和实现绘图区域
{
    //覆盖JPanel的paint方法
    //Graphics是绘图的重要类,可以理解成一支画笔
    public void paint(Graphics g)
       ...

直接在像素里画图

比如画圆

boolean [][]matrixBoo=new boolean[width][width];  //创建像素

int R=20;
        int r=5;
        int Edge=20;

        //画左上圆
        for(int i=Edge;i<Edge+R*2;i++){
            for(int j=Edge;j<Edge+R*2;j++){
                    matrixboo[i][j]=covers(i,j,Edge+R,Edge+R,R);
            }
        }

 //计算是否包括在圆内
    public static boolean covers( int x1, int y1,int x2,int y2,int radius) {
        double d = Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2));
      //  double hjkh=d-radius;
        return d-radius<0.0;
    }


// 将像素保存为图片
  BufferedImage image = new BufferedImage(matrixboo.length , matrixboo[0].length, BufferedImage.TYPE_BYTE_BINARY);
        for(int x = 0; x < matrixboo.length; ++x) {
            for(int y = 0; y < matrixboo[0].length; ++y) {
                image.setRGB(x, y, matrixboo[x][y]?-16777216:-1);
            }
        }
        ImageIO.write(image, "PNG", new File(pathname));

Learn ,Practice ,Summary !
原文地址:https://www.cnblogs.com/daminzhou/p/8252212.html