JAVA 因简洁而美丽,因有效而动人 善待JAVA这颗种子的人,必将得到她的福荫

在java中如何实现画图-Paint (转载)

上一篇 / 下一篇  2010-08-03 23:02:45 / 个人分类:java

java中如何实现画图-Paint

原创舞台2008-06-16 00:10:18阅读206评论0字号:

import java.awt.*;
import java.awt.event.*;

/**分析下例:我们只是new了一个对象并没有调用Paint()方法那为什么会画出图呢?
 * Graphics这个类的对象就是一只画笔,当某容器调用paint()时就会在该容器中画图。
 * 当窗口产生时本身就存在一只画笔,我们只需要拿到画笔重写Paint()便可以随心作画。
 *每次需要重画的时候就会自动调用paint(Graphics g)(什么时候需要重画呢?如当窗口被覆盖又重新置顶时,当窗口最小化又最大化时等等)
 */

总结:我们想要在容器中画图时只需要做的就是:  在该容器中重写Paint() 系统会自动传给我们画笔,自动调用paint方法按照我们的意愿进行作画。
public class TestGraphics extends Frame. {
 public static void main(String []args) {
  new TestGraphics("画图",100,100,200,200,Color.white);
 }
 public TestGraphics(String s,int x,int y,int w,int h,Color c) {
  super(s);
  this.setBounds(x, y, w, h);
  this.setBackground(c);
  this.setVisible(true);
 }
 public void paint(Graphics g) {
  Color c = g.getColor();
  g.setColor(Color.magenta);
  g.fillOval(100, 100, 50, 50);
  g.setColor(Color.green);
  g.fill3DRect(60, 100, 50, 50, false);
  g.setColor(c);
 }
}

………………………………………………………………………………………………………………………………

小例子2:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
原理是:在Frame中增加成员变量-容器ArrayList,用它来容纳点,每次点击鼠标就触发了事件:往容器中添加一个点,然后立即调用repaint方法强制画出容器中所有的点来
所以我们利用容器来"装"点,然后通过iterator来遍历画出所有的点。

适配器类:使用适配器类可以只重写我们需要的方法

因为这些适配器类都已经实现了相应的接口即把所有的方法都空实现了一遍 我们只需要重写我们需要的方法即可

repaint -调用- update() - 调用 - paint();

 */

public class MyFrame. extends Frame. {
 ArrayList<Point>al ;//泛型指定容器中只能放入Point
 public MyFrame(String s) {
  super(s);
  al =new ArrayList<Point>();
  this.setBounds(100, 100, 200, 200);
  this.setBackground(Color.darkGray);
  this.setVisible(true);
  this.addMouseListener(new MouseAdapter(){//匿名类
   @Override
   public void mousePressed(MouseEvent e) {
    MyFrame. f = (MyFrame)e.getSource();//e是事件,e.getSource()是获取事件源即窗口 f
    f.al.add(new Point(e.getX(),e.getY())); //而e.getX(),e.getY()则是获取事件发生的x,y坐标
    repaint();//每次点击鼠标触发事件时都有了新的点,所以强制要求重画,才能立刻显示出该点否则只有窗口被最小化又最大化后才能看到刚才的点
   }
  });

//匿名类:在参数处我们传递的是new WindowAdapter()  {匿名类体} );他没有名字,我们把它当成WindowAdapter来用,为什么可以这样呢?因为语法上规定了匿名类要么是实现了前面的接口,要么是从前面的类继承,就着前面父类的名字来写类体。当此类与其他类关系不大时可以用匿名类
  this.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e){
    System.exit(0);
   }
  });
 }
 public void paint(Graphics g) {
  Iterator <Point>it= al.iterator();//泛型指定取出元素时只能是point
  while(it.hasNext()) {
   Point p = it.next();//由于使用泛型这时候就不用强制转换了
   Color c = g.getColor();//保护原有颜色
   g.setColor(Color.pink);
   g.fillOval(p.x-6, p.y-6, 12, 12);
   g.setColor(c);
  }
 }
 public static void main(String []args) {
  new MyFrame("点击");
 }
 
}
 -----------------------------------------------------------------------------------------------------------------------------------------

小例子如下图:

import java.awt.*;
import java.awt.event.*;

public class Test2 {

 Frame. f ;
 Button b ;
 TextField tf ;
 public Test2 () {
  f = new Frame("匿名类测试");
  b = new Button("start");
  tf = new TextField(10);
  tf.setFont(new Font("",Font.BOLD,20));//设置字体
  b.addActionListener(new ActionListener() {//匿名类把它当成实现了ActionListener接口的对象来使用
   private int i = 0;
   public void actionPerformed(ActionEvent e) {
    tf.setText("     start"+(++i));
   }
  });
  f.setBounds(100, 100, 100, 100);
  f.setBackground(Color.darkGray);
  f.setLayout(new GridLayout(2,1));
  f.add(tf);
  f.add(b);
  f.setVisible(true);
  f.addWindowListener(new WindowAdapter(){//把它当成继承了前面类的子类对象来使用
   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });
 }
 public static void main(String []args) {
  new Test2();
 }
}

--------------------------------------------------------------------------------------------------------------------------------

import java.awt.*;
import java.awt.event.*;

public class Test2 {
 
 Frame. f;
 TextField tf;
 public Test2 () {
  f = new Frame("KeyEvent");
  tf = new TextField(10);
  tf.setEditable(false);
  tf.setFont(new Font("",Font.PLAIN,15));
  f.setBounds(100, 100, 100, 100);
  f.setBackground(Color.darkGray);
  f.add(tf,BorderLayout.NORTH);
  f.setVisible(true);
  f.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });
  tf.addKeyListener(new KeyAdapter(){
   private int i = 0;
   public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();
    if(keyCode == KeyEvent.VK_UP) {
     tf.setText("   正在按下向上键"+(++i)+"次");
    }
   }
  });
 }
 public static void main(String []args) {
  new Test2();
 }
}

 


TAG: java Java JAVA paint

 

评分:0

我来说两句

Open Toolbar