/**
* @(#)DrawLine.java
*
* DrawLine application
*
* @author
* @version 1.00 2007/1/17
*/
import java.awt.*;
import java.awt.event.*;
public class DrawLine extends Frame
{
private int begX;
private int begY;
private int endX;
private int endY;
Image oimg=null;
Graphics og=null;
public void paint(Graphics g)
{
/*
Image img=this.getToolkit().getImage("..\\我要学习啦.gif");
g.drawImage(img,100,100,this);
if(oimg!=null)
{
//g.drawImage(oimg,0,0,this);
}
*/
g.drawImage(oimg,0,0,this);
}
public DrawLine()
{
setTitle("DrawLines");
setSize(400,400);
setVisible(true);
Dimension d=this.getSize();
oimg=this.createImage(d.width,d.height);
og=oimg.getGraphics();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
e.getWindow().dispose();
}
});
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
begX=e.getX();
begY=e.getY();
}
public void mouseReleased(MouseEvent e)
{
endX=e.getX();
endY=e.getY();
Graphics g=getGraphics();
g.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
g.setColor(Color.RED);
g.drawString(begX+","+begY,begX,begY);
g.drawString(endX+","+endY,endX,endY);
g.drawLine(begX,begY,endX,endY);
og.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
og.setColor(Color.RED);
og.drawString(begX+","+begY,begX,begY);
og.drawString(endX+","+endY,endX,endY);
og.drawLine(begX,begY,endX,endY);
}
});
}
public static void main(String[] args) {
// TODO, add your application code
System.out.println("Hello World!");
DrawLine mainFrame=new DrawLine();
}
}