package gui_lesson_001;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class TestGraphics {
/**组件对象本身不能够提供绘图 打印文件操作, getGraphics()
* @param args
*/
public Frame f = new Frame("IT资讯");
public void init(){
f.setSize(600, 800);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
e.getWindow().setVisible(false);
e.getWindow().dispose();
System.exit(0);
}
});
f.addMouseListener(new MouseAdapter() {
int orgx1;
int orgy1;
int orgx2;
int orgy2;
public void mousePressed(MouseEvent e){
orgx1 = e.getX();
orgy1 = e.getY();
System.err.println("pressed");
}
public void mouseReleased(MouseEvent e){
orgx2 = e.getX();
orgy2 = e.getX();
System.err.println("released");
Graphics g = f.getGraphics();
System.err.print(g);
g.setColor(Color.red);
//在GUI上打印文字
g.drawString(new String(orgx1+","+orgy1), orgx1, orgy1);
g.drawString(new String(orgx2+","+orgy2), orgx2, orgy2);
g.drawLine(orgx1, orgy1, orgx2, orgy2);
}
});
}
public static void main(String[] args) {
new TestGraphics().init();
}
}