zoukankan      html  css  js  c++  java
  • 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 !
  • 相关阅读:
    WPF快速指导10:WPF中的事件及冒泡事件和隧道事件(预览事件)的区别
    改善C#程序的建议1:非用ICloneable不可的理由
    WPF快速指导5:验证
    改善C#程序的建议4:C#中标准Dispose模式的实现
    我所入选的微软技术社区电子报
    C#中new, override, virtual的具体用法
    C#中FCL迭代器模式的一点问题
    WPF快速指导3:数据绑定
    WPF快速指导2:模板
    C#高效编程话题集2(每期10话题)
  • 原文地址:https://www.cnblogs.com/daminzhou/p/8252212.html
Copyright © 2011-2022 走看看