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

for(int j=Edge;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));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值