Java -- AWT 画图,图像处理
1. AWT画图 Graphics类 提供绘制简单图形的方法
更新图片时用到 repaint , update , 程序不应该主动调用paint和update, 这两个方法都应该是由AWT系统负责调用,用户重写paint 即可。
示例:
public class Main { private final String RECT_SHAPE = "rect"; private final String OVAL_SHAPE = "oval"; private Frame f = new Frame(); private Button rect = new Button("Rect"); private Button oval = new Button("Oval"); private String shape = ""; private MyCanvas drawArea = new MyCanvas(); void init() { Panel p = new Panel(); p.add(rect); p.add(oval); rect.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub shape = RECT_SHAPE; drawArea.repaint(); //重画, 会调用paint方法。。。 } }); oval.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub shape = OVAL_SHAPE; drawArea.repaint(); } }); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); drawArea.setPreferredSize(new Dimension(250, 180)); f.add(drawArea); f.add(p, BorderLayout.SOUTH); f.pack(); f.setVisible(true); } public static void main(String[] args) { new Main().init(); } class MyCanvas extends Canvas // 画布 { public void paint(Graphics g) //重写paint方法 { Random rand = new Random(); if(shape.equals(RECT_SHAPE)) { g.setColor(new Color(220, 100, 80)); g.drawRect(rand.nextInt(200), rand.nextInt(120), 40, 60); } if(shape.equals(OVAL_SHAPE)) { g.setColor(new Color(80, 100, 200)); g.fillOval(rand.nextInt(200), rand.nextInt(120), 50, 40); } } } }
2. 使用image类 BufferedImage 类处理位图
画板:
import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Frame; import java.awt.Graphics; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseMotionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; public class HandDraw { /** * @param args */ //画区大小 private final int AREA_WIDTH = 500; private final int AREA_HEIGHT = 400; //鼠标坐标 private int preX = -1; private int preY = -1; //右键惨淡 PopupMenu pop = new PopupMenu(); MenuItem redItem = new MenuItem("Red"); MenuItem greenItem = new MenuItem("Green"); MenuItem blueItem = new MenuItem("Blue"); //Bufferedimage 对象 BufferedImage image = new BufferedImage(AREA_WIDTH, AREA_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); private Frame f= new Frame("HandDraw"); private String shape = ""; private Color foreColor = new Color(255, 0, 0); private DrawCanvas drawArea = new DrawCanvas(); public void init() { ActionListener menuListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if( e.getActionCommand().equals("Green") ) { foreColor = new Color(0, 255, 0); System.out.println("Green"); } else if( e.getActionCommand().equals("Red") ) { foreColor = new Color(255, 0, 0); System.out.println("Red"); } else if( e.getActionCommand().equals("Blue") ) { foreColor = new Color(0, 0, 255); System.out.println("Blue"); } } }; redItem.addActionListener(menuListener); greenItem.addActionListener(menuListener); blueItem.addActionListener(menuListener); pop.add(redItem); pop.add(greenItem); pop.add(blueItem); drawArea.add(pop); drawArea.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { if(e.isPopupTrigger()) { pop.show(drawArea, e.getX(), e.getY()); } preX = -1; preY = -1; } }); g.fillRect(0, 0, AREA_WIDTH, AREA_HEIGHT); drawArea.setPreferredSize(new Dimension(AREA_WIDTH, AREA_HEIGHT)); drawArea.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { if( preX > 0 && preY > 0 ) { g.setColor(foreColor); g.drawLine(preX, preY, e.getX(), e.getY()); } preX = e.getX(); preY = e.getY(); drawArea.repaint(); } }); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent w) { System.exit(0); } }); f.add(drawArea); f.pack(); f.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub new HandDraw().init(); } class DrawCanvas extends Canvas { public void paint(Graphics g) { g.drawImage(image, 0, 0, null); } } }
3. 使用ImageIO 输入 输出位图
ImageIO有静态方法 getReaderFormatNames() 等获取 支持的读入写入的图片格式。
public class Main { private final int WIDTH = 800; private final int HEIGHT = 600; BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB ); Graphics g= image.getGraphics(); public void zoom() throws IOException { Image srcImage = ImageIO.read(new File("/home/test/test.png")); //读取图片文件 g.drawImage(srcImage, 0, 0, WIDTH, HEIGHT, null); //将原始图片 按固定大小绘制到image中 ImageIO.write(image, "jpeg", new File("/home/test/testCopy.jpg")); //写入磁盘 } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub new Main().zoom(); } }