当前位置: 技术问答>java相关
在application中使用canvas
来源: 互联网 发布时间:2015-11-08
本文导语: 哪位给一个在application中使用canvas的例子? | import java.awt.*; import java.io.*; import javax.swing.*; import javax.imageio.*; import java.awt.image.*; import java.awt.event.*; import java.awt.color.*; import java.util.*; import...
哪位给一个在application中使用canvas的例子?
|
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.color.*;
import java.util.*;
import javax.imageio.stream.*;
public class SaveCanvas extends Frame
{
ImageCanvas ic = new ImageCanvas();
public SaveCanvas()
{
Button b=new Button("save canvas");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
Iterator writers = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter)writers.next();
ImageOutputStream ios = null;
BufferedImage bufferedImage=new BufferedImage(ic.getWidth(),ic.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
ic.paint(g);
g.dispose();
try{ ios = ImageIO.createImageOutputStream(new FileOutputStream("myPicture.jpg"));}
catch(IOException ioe){}
writer.setOutput(ios);
try{ writer.write(bufferedImage);}
catch(IOException ioe){}
}
});
setLayout(new BorderLayout());
add(b,BorderLayout.NORTH);
add(ic,BorderLayout.CENTER);
setSize(new Dimension(400,400));
setVisible(true);
}
public static void main(String args[])
{
new SaveCanvas();
}
class ImageCanvas extends Canvas
{
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.red);
g.drawLine(0,0,this.getWidth(),this.getHeight());
}
}
}
import java.io.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.color.*;
import java.util.*;
import javax.imageio.stream.*;
public class SaveCanvas extends Frame
{
ImageCanvas ic = new ImageCanvas();
public SaveCanvas()
{
Button b=new Button("save canvas");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
Iterator writers = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter)writers.next();
ImageOutputStream ios = null;
BufferedImage bufferedImage=new BufferedImage(ic.getWidth(),ic.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
ic.paint(g);
g.dispose();
try{ ios = ImageIO.createImageOutputStream(new FileOutputStream("myPicture.jpg"));}
catch(IOException ioe){}
writer.setOutput(ios);
try{ writer.write(bufferedImage);}
catch(IOException ioe){}
}
});
setLayout(new BorderLayout());
add(b,BorderLayout.NORTH);
add(ic,BorderLayout.CENTER);
setSize(new Dimension(400,400));
setVisible(true);
}
public static void main(String args[])
{
new SaveCanvas();
}
class ImageCanvas extends Canvas
{
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.red);
g.drawLine(0,0,this.getWidth(),this.getHeight());
}
}
}