这章我们主要来讨论下用awt包画图,位图处理,使用剪贴板等,由于这些东西比较有套路,我主要以代码形式讲解。
绘图主要是利用Component的三个方法,
paint(Graphics g):绘制组件的外观
update(Graphic g):调用paint方法刷新外观
repaint():调用update
- public class SimpleDraw
- {
- private final String RECT_SHAPE = "rect";
- private final String OVAL_SHAPE = "oval";
- private Frame f = new Frame("简单绘图");
- private Button rect = new Button("绘制矩形");
- private Button oval = new Button("绘制圆形");
- private MyCanvas drawArea = new MyCanvas();
-
- private String shape = "";
- public void init()
- {
- Panel p = new Panel();
- rect.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
-
- shape = RECT_SHAPE;
-
- drawArea.repaint();
- }
- });
- oval.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
-
- shape = OVAL_SHAPE;
-
- drawArea.repaint();
- }
- });
- p.add(rect);
- p.add(oval);
- 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 SimpleDraw().init();
- }
- class MyCanvas extends Canvas
- {
-
- public void paint(Graphics g)
- {
- 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);
- }
- }
- }
- }
下面讲个利用BufferedImage实现图形缓冲的例子,eg
- public class HandDraw
- {
-
- 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("红色");
- MenuItem greenItem = new MenuItem("绿色");
- MenuItem blueItem = new MenuItem("蓝色");
-
- BufferedImage image = new BufferedImage(AREA_WIDTH , AREA_HEIGHT ,
- BufferedImage.TYPE_INT_RGB);
-
- Graphics g = image.getGraphics();
- private Frame f = new Frame("简单手绘程序");
- private DrawCanvas drawArea = new DrawCanvas();
-
- private String shape = "";
-
- private Color foreColor = new Color(255, 0 ,0);
- public void init()
- {
-
- ActionListener menuListener = new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- if (e.getActionCommand().equals("绿色"))
- {
- foreColor = new Color(0 , 255 , 0);
- }
- if (e.getActionCommand().equals("红色"))
- {
- foreColor = new Color(255 , 0 , 0);
- }
- if (e.getActionCommand().equals("蓝色"))
- {
- foreColor = new Color(0 , 0 , 255);
- }
- }
- };
-
- redItem.addActionListener(menuListener);
- greenItem.addActionListener(menuListener);
- blueItem.addActionListener(menuListener);
-
- pop.add(redItem);
- pop.add(greenItem);
- pop.add(blueItem);
-
- drawArea.add(pop);
-
- 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();
- }
- });
-
- drawArea.addMouseListener(new MouseAdapter()
- {
-
- public void mouseReleased(MouseEvent e)
- {
-
- if (e.isPopupTrigger())
- {
- pop.show(drawArea , e.getX() , e.getY());
- }
-
- preX = -1;
- preY = -1;
- }
- });
- f.add(drawArea);
- f.pack();
- f.setVisible(true);
- }
- public static void main(String[] args)
- {
- new HandDraw().init();
- }
- class DrawCanvas extends Canvas
- {
-
- public void paint(Graphics g)
- {
-
- g.drawImage(image , 0 , 0 , null);
- }
- }
- }
由于以上每次画都刷全图,闪烁还是不能避免,其实只要刷画笔的周围就可以实现基本不闪的效果,利用Swing的JPanel组件等都已经实现了双缓冲了
下面再讲下利用ImageIO来对图片进行读写,在windows现在支持这几种图片格式
BMP
jpeg
bmp
wbmp
gif
JPG
png
jpg
JPEG
WBMP
常用的基本都支持了,举个使用实例eg
- public class ZoomImage
- {
-
- private final int WIDTH = 80;
- private final int HEIGHT = 60;
-
- BufferedImage image = new BufferedImage(WIDTH , HEIGHT ,
- BufferedImage.TYPE_INT_RGB);
- Graphics g = image.getGraphics();
- public void zoom()throws Exception
- {
-
- Image srcImage = ImageIO.read(new File("image/board.jpg"));
-
- g.drawImage(srcImage , 0 , 0 , WIDTH , HEIGHT , null);
-
- ImageIO.write(image , "jpeg" ,
- new File(System.currentTimeMillis() + ".jpg"));
- }
- public static void main(String[] args)throws Exception
- {
- new ZoomImage().zoom();
- }
- }
最后简单讲下剪贴板的使用,主要区别下系统剪贴板和本地剪贴板的区别,本地的复制只能在jvm里使用。eg
- public class SimpleClipboard
- {
- private Frame f = new Frame("简单的剪贴板程序");
-
- private Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
-
-
-
- private TextArea jtaCopyTo = new TextArea(5,20);
-
- private TextArea jtaPaste = new TextArea(5,20);
- private Button btCopy = new Button("复制");
- private Button btPaste = new Button("粘贴");
- public void init()
- {
- Panel p = new Panel();
- p.add(btCopy);
- p.add(btPaste);
- btCopy.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent event)
- {
-
- StringSelection contents=new StringSelection(jtaCopyTo.getText());
-
- clipboard.setContents(contents, null);
- }
- });
- btPaste.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent event)
- {
-
- if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor))
- {
- try
- {
-
- String content = (String)clipboard.getData(DataFlavor.stringFlavor);
- jtaPaste.append(content);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- });
-
- Box box = new Box(BoxLayout.X_AXIS);
-
- box.add(jtaCopyTo);
- box.add(jtaPaste);
-
- f.add(p,BorderLayout.SOUTH);
- f.add(box,BorderLayout.CENTER);
- f.pack();
- f.setVisible(true);
- }
- public static void main(String[] args)
- {
- new SimpleClipboard().init();
- }
- }